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/exception.h>
29 : #include <snaplogger/format.h>
30 : #include <snaplogger/logger.h>
31 : #include <snaplogger/map_diagnostic.h>
32 : #include <snaplogger/nested_diagnostic.h>
33 : #include <snaplogger/message.h>
34 : #include <snaplogger/severity.h>
35 : #include <snaplogger/version.h>
36 :
37 :
38 : // C lib
39 : //
40 : #include <unistd.h>
41 :
42 :
43 :
44 3 : CATCH_TEST_CASE("diagnostic", "[message][diagnostic]")
45 : {
46 2 : CATCH_START_SECTION("diagnostic: Map based and nested diagnostics")
47 : {
48 1 : snaplogger::set_diagnostic(snaplogger::DIAG_KEY_PROGNAME, "basic-format");
49 :
50 : // these two are not called in this test
51 : //
52 1 : snaplogger::set_diagnostic(snaplogger::DIAG_KEY_PROJECT_NAME, "test-logger");
53 1 : snaplogger::set_diagnostic(snaplogger::DIAG_KEY_VERSION, "5.32.1024");
54 :
55 : // test with our own diagnostics too
56 : //
57 1 : snaplogger::set_diagnostic("test_diag", "X-66-Q");
58 :
59 2 : snaplogger::logger::pointer_t l(snaplogger::logger::get_instance());
60 2 : snaplogger::buffer_appender::pointer_t buffer(std::make_shared<snaplogger::buffer_appender>("test-buffer"));
61 :
62 1 : char const * cargv[] =
63 : {
64 : "/usr/bin/daemon",
65 : nullptr
66 : };
67 1 : int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
68 1 : char ** argv = const_cast<char **>(cargv);
69 :
70 1 : advgetopt::options_environment environment_options;
71 1 : environment_options.f_project_name = "test-logger";
72 1 : environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
73 1 : environment_options.f_version = "5.32.1024";
74 2 : advgetopt::getopt opts(environment_options);
75 1 : opts.parse_program_name(argv);
76 1 : opts.parse_arguments(argc, argv, advgetopt::option_source_t::SOURCE_COMMAND_LINE);
77 :
78 1 : buffer->set_config(opts);
79 :
80 2 : snaplogger::format::pointer_t f(std::make_shared<snaplogger::format>("${project_name} ${message} v${version}"));
81 1 : buffer->set_format(f);
82 :
83 1 : l->add_appender(buffer);
84 :
85 1 : SNAP_LOG_WARNING
86 : << "{${diagnostic:map=test_diag}}"
87 : << SNAP_LOG_SEND;
88 :
89 1 : CATCH_REQUIRE(buffer->str() == "test-logger {<test_diag=X-66-Q>} v5.32.1024"
90 : "\n");
91 :
92 1 : buffer->clear();
93 :
94 : {
95 2 : snaplogger::nested_diagnostic l1("level-I");
96 :
97 1 : SNAP_LOG_WARNING
98 : << "$${diagnostic:map=test_diag}$ & [${diagnostic:nested=10}]"
99 : << SNAP_LOG_SEND;
100 :
101 1 : CATCH_REQUIRE(buffer->str() ==
102 : "test-logger $<test_diag=X-66-Q>$ & [{level-I}] v5.32.1024"
103 : "\n");
104 :
105 1 : buffer->clear();
106 :
107 : {
108 2 : snaplogger::nested_diagnostic l2("sub-level-II");
109 :
110 1 : SNAP_LOG_WARNING
111 : << "$${diagnostic:map=test_diag}$ & [${diagnostic:nested=10}]"
112 : << SNAP_LOG_SEND;
113 :
114 1 : CATCH_REQUIRE(buffer->str() ==
115 : "test-logger $<test_diag=X-66-Q>$ & [{level-I/sub-level-II}] v5.32.1024"
116 : "\n");
117 :
118 1 : buffer->clear();
119 :
120 : {
121 2 : snaplogger::nested_diagnostic l3("under-level-III");
122 :
123 1 : SNAP_LOG_WARNING
124 : << "$${diagnostic:map=test_diag}$ & [${diagnostic:nested=10}]"
125 : << SNAP_LOG_SEND;
126 :
127 1 : CATCH_REQUIRE(buffer->str() ==
128 : "test-logger $<test_diag=X-66-Q>$ & [{level-I/sub-level-II/under-level-III}] v5.32.1024"
129 : "\n");
130 :
131 1 : buffer->clear();
132 :
133 1 : SNAP_LOG_WARNING
134 : << "$${diagnostic:map=test_diag}$ & [${diagnostic:nested=2}]"
135 : << SNAP_LOG_SEND;
136 :
137 1 : CATCH_REQUIRE(buffer->str() ==
138 : "test-logger $<test_diag=X-66-Q>$ & [{.../sub-level-II/under-level-III}] v5.32.1024"
139 : "\n");
140 :
141 1 : buffer->clear();
142 : }
143 : }
144 : }
145 :
146 1 : l->reset();
147 : }
148 : CATCH_END_SECTION()
149 7 : }
150 :
151 :
152 :
153 : // vim: ts=4 sw=4 et
|