Line data Source code
1 : /*
2 : * Copyright (c) 2006-2019 Made to Order Software Corp. All Rights Reserved
3 : *
4 : * https://snapwebsites.org/project/snaplogger
5 : * contact@m2osw.com
6 : *
7 : * This program is free software; you can redistribute it and/or modify
8 : * it under the terms of the GNU General Public License as published by
9 : * the Free Software Foundation; either version 2 of the License, or
10 : * (at your option) any later version.
11 : *
12 : * This program is distributed in the hope that it will be useful,
13 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 : * GNU General Public License for more details.
16 : *
17 : * You should have received a copy of the GNU General Public License along
18 : * with this program; if not, write to the Free Software Foundation, Inc.,
19 : * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 : */
21 :
22 : // self
23 : //
24 : #include "main.h"
25 :
26 :
27 : // snaplogger lib
28 : //
29 : #include <snaplogger/buffer_appender.h>
30 : #include <snaplogger/exception.h>
31 : #include <snaplogger/format.h>
32 : #include <snaplogger/logger.h>
33 : #include <snaplogger/map_diagnostic.h>
34 : #include <snaplogger/nested_diagnostic.h>
35 : #include <snaplogger/message.h>
36 : #include <snaplogger/severity.h>
37 : #include <snaplogger/version.h>
38 :
39 :
40 : // C lib
41 : //
42 : #include <unistd.h>
43 :
44 :
45 :
46 3 : CATCH_TEST_CASE("diagnostic", "[message][diagnostic]")
47 : {
48 2 : CATCH_START_SECTION("Map based and nested diagnostics")
49 : {
50 1 : snaplogger::set_diagnostic(snaplogger::DIAG_KEY_PROGNAME, "basic-format");
51 :
52 : // these two are not called in this test
53 : //
54 1 : snaplogger::set_diagnostic(snaplogger::DIAG_KEY_PROJECT_NAME, "test-logger");
55 1 : snaplogger::set_diagnostic(snaplogger::DIAG_KEY_VERSION, "5.32.1024");
56 :
57 : // test with our own diagnostics too
58 : //
59 1 : snaplogger::set_diagnostic("test_diag", "X-66-Q");
60 :
61 2 : snaplogger::logger::pointer_t l(snaplogger::logger::get_instance());
62 2 : snaplogger::buffer_appender::pointer_t buffer(std::make_shared<snaplogger::buffer_appender>("test-buffer"));
63 :
64 1 : advgetopt::options_environment opt_env;
65 1 : opt_env.f_project_name = "test-logger";
66 1 : opt_env.f_version = "5.32.1024";
67 2 : advgetopt::getopt opts(opt_env);
68 1 : buffer->set_config(opts);
69 :
70 2 : snaplogger::format::pointer_t f(std::make_shared<snaplogger::format>("${project_name} ${message} v${version}"));
71 1 : buffer->set_format(f);
72 :
73 1 : l->add_appender(buffer);
74 :
75 : SNAP_LOG_WARNING
76 2 : << "{${diagnostic:map=test_diag}}"
77 1 : << SNAP_LOG_SEND;
78 :
79 1 : CATCH_REQUIRE(buffer->str() == "test-logger {<test_diag=X-66-Q>} v5.32.1024"
80 : "\n");
81 :
82 1 : buffer->clear();
83 :
84 : {
85 2 : snaplogger::nested_diagnostic l1("level-I");
86 :
87 : SNAP_LOG_WARNING
88 2 : << "$${diagnostic:map=test_diag}$ & [${diagnostic:nested=10}]"
89 1 : << SNAP_LOG_SEND;
90 :
91 1 : CATCH_REQUIRE(buffer->str() ==
92 : "test-logger $<test_diag=X-66-Q>$ & [{level-I}] v5.32.1024"
93 : "\n");
94 :
95 1 : buffer->clear();
96 :
97 : {
98 2 : snaplogger::nested_diagnostic l2("sub-level-II");
99 :
100 : SNAP_LOG_WARNING
101 2 : << "$${diagnostic:map=test_diag}$ & [${diagnostic:nested=10}]"
102 1 : << SNAP_LOG_SEND;
103 :
104 1 : CATCH_REQUIRE(buffer->str() ==
105 : "test-logger $<test_diag=X-66-Q>$ & [{level-I/sub-level-II}] v5.32.1024"
106 : "\n");
107 :
108 1 : buffer->clear();
109 :
110 : {
111 2 : snaplogger::nested_diagnostic l3("under-level-III");
112 :
113 : SNAP_LOG_WARNING
114 2 : << "$${diagnostic:map=test_diag}$ & [${diagnostic:nested=10}]"
115 1 : << SNAP_LOG_SEND;
116 :
117 1 : CATCH_REQUIRE(buffer->str() ==
118 : "test-logger $<test_diag=X-66-Q>$ & [{level-I/sub-level-II/under-level-III}] v5.32.1024"
119 : "\n");
120 :
121 1 : buffer->clear();
122 :
123 : SNAP_LOG_WARNING
124 2 : << "$${diagnostic:map=test_diag}$ & [${diagnostic:nested=2}]"
125 1 : << SNAP_LOG_SEND;
126 :
127 1 : CATCH_REQUIRE(buffer->str() ==
128 : "test-logger $<test_diag=X-66-Q>$ & [{.../sub-level-II/under-level-III}] v5.32.1024"
129 : "\n");
130 :
131 1 : buffer->clear();
132 : }
133 : }
134 : }
135 :
136 1 : l->reset();
137 : }
138 : CATCH_END_SECTION()
139 7 : }
140 :
141 :
142 :
143 : // vim: ts=4 sw=4 et
|