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