Line data Source code
1 : // Copyright (c) 2006-2025 Made to Order Software Corp. All Rights Reserved
2 : //
3 : // https://snapwebsites.org/project/advgetopt
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 : // advgetopt
21 : //
22 : #include <advgetopt/exception.h>
23 :
24 :
25 : // self
26 : //
27 : #include "catch_main.h"
28 :
29 :
30 : // cppthread
31 : //
32 : #include <cppthread/exception.h>
33 :
34 :
35 : // snapdev
36 : //
37 : #include <snapdev/safe_setenv.h>
38 :
39 :
40 : // C++
41 : //
42 : #include <fstream>
43 :
44 :
45 : // last include
46 : //
47 : #include <snapdev/poison.h>
48 :
49 :
50 :
51 :
52 3 : CATCH_TEST_CASE("logger", "[logger][valid][log]")
53 : {
54 3 : CATCH_START_SECTION("logger: verify log levels")
55 : {
56 1 : CATCH_REQUIRE(to_string(cppthread::log_level_t::debug) == "debug");
57 1 : CATCH_REQUIRE(to_string(cppthread::log_level_t::info) == "info");
58 1 : CATCH_REQUIRE(to_string(cppthread::log_level_t::warning) == "warning");
59 1 : CATCH_REQUIRE(to_string(cppthread::log_level_t::error) == "error");
60 1 : CATCH_REQUIRE(to_string(cppthread::log_level_t::fatal) == "fatal");
61 : }
62 3 : CATCH_END_SECTION()
63 :
64 3 : CATCH_START_SECTION("logger: verify log string")
65 : {
66 3 : SNAP_CATCH2_NAMESPACE::push_expected_log("debug: Test a regular string.");
67 2 : cppthread::log << cppthread::log_level_t::debug
68 1 : << "Test a regular string."
69 2 : << cppthread::end;
70 1 : SNAP_CATCH2_NAMESPACE::expected_logs_stack_is_empty();
71 :
72 3 : SNAP_CATCH2_NAMESPACE::push_expected_log("info: Test an std::string.");
73 3 : std::string const msg("Test an std::string.");
74 2 : cppthread::log << cppthread::log_level_t::info
75 1 : << msg
76 2 : << cppthread::end;
77 1 : SNAP_CATCH2_NAMESPACE::expected_logs_stack_is_empty();
78 1 : }
79 3 : CATCH_END_SECTION()
80 :
81 3 : CATCH_START_SECTION("logger: verify log integers")
82 : {
83 : // gcc sees this one as a char
84 : {
85 1 : std::int8_t v(rand());
86 3 : std::string msg(std::string("warning: Test an int8_t: "));
87 1 : msg += static_cast<char>(v);
88 1 : msg += '.';
89 1 : SNAP_CATCH2_NAMESPACE::push_expected_log(msg);
90 2 : cppthread::log << cppthread::log_level_t::warning
91 1 : << "Test an int8_t: "
92 1 : << v
93 1 : << "."
94 2 : << cppthread::end;
95 1 : SNAP_CATCH2_NAMESPACE::expected_logs_stack_is_empty();
96 1 : }
97 :
98 : {
99 1 : std::int16_t v(rand());
100 1 : SNAP_CATCH2_NAMESPACE::push_expected_log("error: Test an int16_t: " + std::to_string(v) + ".");
101 2 : cppthread::log << cppthread::log_level_t::error
102 1 : << "Test an int16_t: "
103 1 : << v
104 1 : << "."
105 2 : << cppthread::end;
106 1 : SNAP_CATCH2_NAMESPACE::expected_logs_stack_is_empty();
107 : }
108 :
109 : {
110 1 : std::int32_t v(rand());
111 1 : SNAP_CATCH2_NAMESPACE::push_expected_log("fatal: Test an int32_t: " + std::to_string(v) + ".");
112 2 : cppthread::log << cppthread::log_level_t::fatal
113 1 : << "Test an int32_t: "
114 1 : << v
115 1 : << "."
116 2 : << cppthread::end;
117 1 : SNAP_CATCH2_NAMESPACE::expected_logs_stack_is_empty();
118 : }
119 :
120 : {
121 1 : std::int64_t v(rand());
122 1 : SNAP_CATCH2_NAMESPACE::push_expected_log("debug: Test an int64_t: " + std::to_string(v) + ".");
123 2 : cppthread::log << cppthread::log_level_t::debug
124 1 : << "Test an int64_t: "
125 1 : << v
126 1 : << "."
127 2 : << cppthread::end;
128 1 : SNAP_CATCH2_NAMESPACE::expected_logs_stack_is_empty();
129 : }
130 :
131 : // gcc sees this one as a char
132 : {
133 1 : std::uint8_t v(rand());
134 3 : std::string msg("info: Test an uint8_t: ");
135 1 : msg += static_cast<char>(v);
136 1 : msg += '.';
137 1 : SNAP_CATCH2_NAMESPACE::push_expected_log(msg);
138 2 : cppthread::log << cppthread::log_level_t::info
139 1 : << "Test an uint8_t: "
140 1 : << v
141 1 : << "."
142 2 : << cppthread::end;
143 1 : SNAP_CATCH2_NAMESPACE::expected_logs_stack_is_empty();
144 1 : }
145 :
146 : {
147 1 : std::uint16_t v(rand());
148 1 : SNAP_CATCH2_NAMESPACE::push_expected_log("warning: Test an uint16_t: " + std::to_string(static_cast<int>(v)) + ".");
149 2 : cppthread::log << cppthread::log_level_t::warning
150 1 : << "Test an uint16_t: "
151 1 : << v
152 1 : << "."
153 2 : << cppthread::end;
154 1 : SNAP_CATCH2_NAMESPACE::expected_logs_stack_is_empty();
155 : }
156 :
157 : {
158 1 : std::uint32_t v(rand());
159 1 : SNAP_CATCH2_NAMESPACE::push_expected_log("error: Test an uint32_t: " + std::to_string(v) + ".");
160 2 : cppthread::log << cppthread::log_level_t::error
161 1 : << "Test an uint32_t: "
162 1 : << v
163 1 : << "."
164 2 : << cppthread::end;
165 1 : SNAP_CATCH2_NAMESPACE::expected_logs_stack_is_empty();
166 : }
167 :
168 : {
169 1 : std::uint64_t v(rand());
170 1 : SNAP_CATCH2_NAMESPACE::push_expected_log("fatal: Test an uint64_t: " + std::to_string(v) + ".");
171 2 : cppthread::log << cppthread::log_level_t::fatal
172 1 : << "Test an uint64_t: "
173 1 : << v
174 1 : << "."
175 2 : << cppthread::end;
176 1 : SNAP_CATCH2_NAMESPACE::expected_logs_stack_is_empty();
177 : }
178 : }
179 3 : CATCH_END_SECTION()
180 3 : }
181 :
182 :
183 1 : CATCH_TEST_CASE("logger_without_callback", "[logger][valid][log]")
184 : {
185 1 : CATCH_START_SECTION("logger_without_callback: verify log string")
186 : {
187 : // cancel the callback for one test
188 1 : cppthread::set_log_callback(nullptr);
189 :
190 : //SNAP_CATCH2_NAMESPACE::push_expected_log("debug: Test without a callback."); -- not going to be registered!
191 2 : cppthread::log << cppthread::log_level_t::debug
192 1 : << "Test without a callback."
193 2 : << cppthread::end;
194 :
195 : // restore the callback
196 1 : cppthread::set_log_callback(SNAP_CATCH2_NAMESPACE::log_for_test);
197 :
198 : // make sure we have at least one assertion otherwise the
199 : // coverage will fail with an error
200 : //
201 1 : CATCH_REQUIRE(true);
202 : }
203 1 : CATCH_END_SECTION()
204 1 : }
205 :
206 :
207 1 : CATCH_TEST_CASE("invalid_logger", "[logger][invalid][log]")
208 : {
209 1 : CATCH_START_SECTION("invalid_logger: verify invalid log levels")
210 : {
211 101 : for(int i(0); i < 100; ++i)
212 : {
213 100 : cppthread::log_level_t level(cppthread::log_level_t::warning);
214 : for(;;)
215 : {
216 100 : level = static_cast<cppthread::log_level_t>(rand());
217 100 : if(level != cppthread::log_level_t::debug
218 100 : && level != cppthread::log_level_t::info
219 100 : && level != cppthread::log_level_t::warning
220 100 : && level != cppthread::log_level_t::error
221 100 : && level != cppthread::log_level_t::fatal)
222 : {
223 100 : break;
224 : }
225 : }
226 :
227 200 : CATCH_REQUIRE_THROWS_MATCHES(cppthread::to_string(level)
228 : , cppthread::invalid_error
229 : , Catch::Matchers::ExceptionMessage(
230 : "cppthread_exception: unknown log level ("
231 : + std::to_string(static_cast<int>(level))
232 : + ")"));
233 : }
234 : }
235 1 : CATCH_END_SECTION()
236 1 : }
237 :
238 :
239 : // vim: ts=4 sw=4 et
|