Line data Source code
1 : // Copyright (c) 2006-2021 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 "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("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 : advgetopt::options_environment opt_env;
50 1 : opt_env.f_project_name = "async-unittest";
51 2 : advgetopt::getopt opts(opt_env);
52 1 : buffer->set_config(opts);
53 :
54 2 : snaplogger::format::pointer_t f(std::make_shared<snaplogger::format>("${progname}: ${severity}: ${message} (${version})"));
55 1 : buffer->set_format(f);
56 :
57 1 : l->add_appender(buffer);
58 :
59 1 : l->set_asynchronous(true);
60 :
61 : // the cppthread library generates messages "enter" and "exit"
62 : // we need to prevent those because we can't easily compare
63 : // the results
64 : //
65 1 : l->add_component_to_ignore(snaplogger::g_cppthread_component);
66 :
67 : //l->add_console_appender()->add_component(snaplogger::g_secure_component);
68 : //l->add_component_to_ignore(snaplogger::g_normal_component);
69 : //l->add_component_to_include(snaplogger::g_normal_component);
70 :
71 2 : SNAP_LOG_WARNING
72 1 : << "Sent through thread..."
73 : << SNAP_LOG_SEND;
74 :
75 : // this call blocks until the thread stopped and joined
76 : //
77 1 : l->set_asynchronous(false);
78 :
79 : // TODO: add the ${tid} as one of the message parameter and a way
80 : // to retrieve the tid of the async. thread
81 : //
82 1 : CATCH_REQUIRE(buffer->str() == "async-unittest: warning: Sent through thread... (1.0)\n");
83 : }
84 : CATCH_END_SECTION()
85 7 : }
86 :
87 :
88 : // vim: ts=4 sw=4 et
|