Line data Source code
1 : // Copyright (c) 2006-2022 Made to Order Software Corp. All Rights Reserved
2 : //
3 : // https://snapwebsites.org/project/snaplogger
4 : // contact@m2osw.com
5 : //
6 : // This program is free software; you can redistribute it and/or modify
7 : // it under the terms of the GNU General Public License as published by
8 : // the Free Software Foundation; either version 2 of the License, or
9 : // (at your option) any later version.
10 : //
11 : // This program is distributed in the hope that it will be useful,
12 : // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : // GNU General Public License for more details.
15 : //
16 : // You should have received a copy of the GNU General Public License along
17 : // with this program; if not, write to the Free Software Foundation, Inc.,
18 : // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 :
20 : // self
21 : //
22 : #include "catch_main.h"
23 :
24 :
25 : // snaplogger lib
26 : //
27 : #include <snaplogger/buffer_appender.h>
28 : #include <snaplogger/logger.h>
29 : #include <snaplogger/map_diagnostic.h>
30 : #include <snaplogger/message.h>
31 :
32 :
33 : // C lib
34 : //
35 : #include <unistd.h>
36 :
37 :
38 :
39 3 : CATCH_TEST_CASE("example", "[example]")
40 : {
41 2 : CATCH_START_SECTION("asynchronous: Simple logging")
42 : {
43 1 : snaplogger::set_diagnostic(snaplogger::DIAG_KEY_PROGNAME, "async-unittest");
44 1 : snaplogger::set_diagnostic(snaplogger::DIAG_KEY_VERSION, "1.0");
45 :
46 2 : snaplogger::logger::pointer_t l(snaplogger::logger::get_instance());
47 2 : snaplogger::buffer_appender::pointer_t buffer(std::make_shared<snaplogger::buffer_appender>("test-buffer"));
48 :
49 1 : char const * cargv[] =
50 : {
51 : "/usr/bin/daemon",
52 : nullptr
53 : };
54 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
55 1 : char ** argv = const_cast<char **>(cargv);
56 :
57 1 : advgetopt::options_environment environment_options;
58 1 : environment_options.f_project_name = "async-unittest";
59 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
60 2 : advgetopt::getopt opts(environment_options);
61 1 : opts.parse_program_name(argv);
62 1 : opts.parse_arguments(argc, argv, advgetopt::option_source_t::SOURCE_COMMAND_LINE);
63 :
64 1 : buffer->set_config(opts);
65 :
66 2 : snaplogger::format::pointer_t f(std::make_shared<snaplogger::format>("${progname}: ${severity}: ${message} (${version})"));
67 1 : buffer->set_format(f);
68 :
69 1 : l->add_appender(buffer);
70 :
71 1 : l->set_asynchronous(true);
72 :
73 : // the cppthread library generates messages "enter" and "exit"
74 : // we need to prevent those because we can't easily compare
75 : // the results
76 : //
77 1 : l->add_component_to_ignore(snaplogger::g_cppthread_component);
78 :
79 : //l->add_console_appender()->add_component(snaplogger::g_secure_component);
80 : //l->add_component_to_ignore(snaplogger::g_normal_component);
81 : //l->add_component_to_include(snaplogger::g_normal_component);
82 :
83 1 : SNAP_LOG_WARNING
84 : << "Sent through thread..."
85 : << SNAP_LOG_SEND;
86 :
87 : // this call blocks until the thread stopped and joined
88 : //
89 1 : l->set_asynchronous(false);
90 :
91 : // TODO: add the ${tid} as one of the message parameter and a way
92 : // to retrieve the tid of the async. thread
93 : //
94 1 : CATCH_REQUIRE(buffer->str() == "async-unittest: warning: Sent through thread... (1.0)\n");
95 : }
96 : CATCH_END_SECTION()
97 7 : }
98 :
99 :
100 : // vim: ts=4 sw=4 et
|