Line data Source code
1 : // Copyright (c) 2006-2025 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 3 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
17 : // along with this program. If not, see <https://www.gnu.org/licenses/>.
18 :
19 : // self
20 : //
21 : #include "catch_main.h"
22 :
23 :
24 : // snaplogger
25 : //
26 : #include <snaplogger/buffer_appender.h>
27 : #include <snaplogger/logger.h>
28 : #include <snaplogger/map_diagnostic.h>
29 : #include <snaplogger/message.h>
30 :
31 :
32 : // C
33 : //
34 : #include <unistd.h>
35 :
36 :
37 :
38 1 : CATCH_TEST_CASE("example", "[example]")
39 : {
40 1 : CATCH_START_SECTION("asynchronous: Simple logging")
41 : {
42 5 : snaplogger::set_diagnostic(snaplogger::DIAG_KEY_PROGNAME, "async-unittest");
43 5 : snaplogger::set_diagnostic(snaplogger::DIAG_KEY_VERSION, "1.0");
44 :
45 1 : snaplogger::logger::pointer_t l(snaplogger::logger::get_instance());
46 1 : snaplogger::buffer_appender::pointer_t buffer(std::make_shared<snaplogger::buffer_appender>("test-buffer"));
47 :
48 1 : char const * cargv[] =
49 : {
50 : "/usr/bin/daemon",
51 : nullptr
52 : };
53 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
54 1 : char ** argv = const_cast<char **>(cargv);
55 :
56 1 : advgetopt::options_environment environment_options;
57 1 : environment_options.f_project_name = "async-unittest";
58 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
59 1 : advgetopt::getopt opts(environment_options);
60 1 : opts.parse_program_name(argv);
61 1 : opts.parse_arguments(argc, argv, advgetopt::option_source_t::SOURCE_COMMAND_LINE);
62 :
63 1 : buffer->set_config(opts);
64 :
65 1 : snaplogger::format::pointer_t f(std::make_shared<snaplogger::format>("${progname}: ${severity}: ${message} (${version})"));
66 1 : buffer->set_format(f);
67 :
68 1 : l->add_appender(buffer);
69 :
70 1 : l->set_asynchronous(true);
71 :
72 : // the cppthread library generates messages "enter" and "exit"
73 : // we need to prevent those because we can't easily compare
74 : // the results
75 : //
76 1 : l->add_component_to_ignore(snaplogger::g_cppthread_component);
77 :
78 : //l->add_console_appender()->add_component(snaplogger::g_secure_component);
79 : //l->add_component_to_ignore(snaplogger::g_normal_component);
80 : //l->add_component_to_include(snaplogger::g_normal_component);
81 :
82 2 : SNAP_LOG_WARNING
83 : << "Sent through thread..."
84 : << SNAP_LOG_SEND;
85 :
86 : // this call blocks until the thread stopped and joined
87 : //
88 1 : l->set_asynchronous(false);
89 :
90 : // TODO: add the ${tid} as one of the message parameter and a way
91 : // to retrieve the tid of the async. thread
92 : //
93 1 : CATCH_REQUIRE(buffer->str() == "async-unittest: warning: Sent through thread... (1.0)\n");
94 :
95 1 : l->remove_component_to_ignore(snaplogger::g_cppthread_component);
96 1 : }
97 1 : CATCH_END_SECTION()
98 1 : }
99 :
100 :
101 : // vim: ts=4 sw=4 et
|