Line data Source code
1 : /*
2 : * Files:
3 : * tests/logger.cpp
4 : *
5 : * License:
6 : * Copyright (c) 2006-2019 Made to Order Software Corp. All Rights Reserved
7 : *
8 : * https://snapwebsites.org/
9 : * contact@m2osw.com
10 : *
11 : * This program is free software; you can redistribute it and/or modify
12 : * it under the terms of the GNU General Public License as published by
13 : * the Free Software Foundation; either version 2 of the License, or
14 : * (at your option) any later version.
15 : *
16 : * This program is distributed in the hope that it will be useful,
17 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : * GNU General Public License for more details.
20 : *
21 : * You should have received a copy of the GNU General Public License along
22 : * with this program; if not, write to the Free Software Foundation, Inc.,
23 : * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 : *
25 : * Authors:
26 : * Alexis Wilke alexis@m2osw.com
27 : */
28 :
29 : // self
30 : //
31 : #include "main.h"
32 :
33 : // advgetopt lib
34 : //
35 : #include <advgetopt/exception.h>
36 :
37 : // snapdev lib
38 : //
39 : #include <snapdev/safe_setenv.h>
40 :
41 : // C++ lib
42 : //
43 : #include <fstream>
44 :
45 :
46 :
47 :
48 5 : CATCH_TEST_CASE("logger", "[logger][valid][log]")
49 : {
50 6 : CATCH_START_SECTION("Verify log levels")
51 1 : CATCH_REQUIRE(to_string(advgetopt::log_level_t::debug) == "debug");
52 1 : CATCH_REQUIRE(to_string(advgetopt::log_level_t::info) == "info");
53 1 : CATCH_REQUIRE(to_string(advgetopt::log_level_t::warning) == "warning");
54 1 : CATCH_REQUIRE(to_string(advgetopt::log_level_t::error) == "error");
55 1 : CATCH_REQUIRE(to_string(advgetopt::log_level_t::fatal) == "fatal");
56 : CATCH_END_SECTION()
57 :
58 6 : CATCH_START_SECTION("Verify log string")
59 1 : SNAP_CATCH2_NAMESPACE::push_expected_log("debug: Test a regular string.");
60 2 : advgetopt::log << advgetopt::log_level_t::debug
61 1 : << "Test a regular string."
62 1 : << advgetopt::end;
63 1 : SNAP_CATCH2_NAMESPACE::expected_logs_stack_is_empty();
64 :
65 1 : SNAP_CATCH2_NAMESPACE::push_expected_log("info: Test an std::string.");
66 2 : std::string const msg("Test an std::string.");
67 2 : advgetopt::log << advgetopt::log_level_t::info
68 1 : << msg
69 1 : << advgetopt::end;
70 1 : SNAP_CATCH2_NAMESPACE::expected_logs_stack_is_empty();
71 : CATCH_END_SECTION()
72 :
73 6 : CATCH_START_SECTION("Verify log integers")
74 : // gcc sees this one as a char
75 : {
76 1 : std::int8_t v(rand());
77 1 : SNAP_CATCH2_NAMESPACE::push_expected_log(std::string("warning: Test an int8_t: ") + static_cast<char>(v) + ".");
78 2 : advgetopt::log << advgetopt::log_level_t::warning
79 1 : << "Test an int8_t: "
80 1 : << v
81 1 : << "."
82 1 : << advgetopt::end;
83 1 : SNAP_CATCH2_NAMESPACE::expected_logs_stack_is_empty();
84 : }
85 :
86 : {
87 1 : std::int16_t v(rand());
88 1 : SNAP_CATCH2_NAMESPACE::push_expected_log("error: Test an int16_t: " + std::to_string(v) + ".");
89 2 : advgetopt::log << advgetopt::log_level_t::error
90 1 : << "Test an int16_t: "
91 1 : << v
92 1 : << "."
93 1 : << advgetopt::end;
94 1 : SNAP_CATCH2_NAMESPACE::expected_logs_stack_is_empty();
95 : }
96 :
97 : {
98 1 : std::int32_t v(rand());
99 1 : SNAP_CATCH2_NAMESPACE::push_expected_log("fatal: Test an int32_t: " + std::to_string(v) + ".");
100 2 : advgetopt::log << advgetopt::log_level_t::fatal
101 1 : << "Test an int32_t: "
102 1 : << v
103 1 : << "."
104 1 : << advgetopt::end;
105 1 : SNAP_CATCH2_NAMESPACE::expected_logs_stack_is_empty();
106 : }
107 :
108 : {
109 1 : std::int64_t v(rand());
110 1 : SNAP_CATCH2_NAMESPACE::push_expected_log("debug: Test an int64_t: " + std::to_string(v) + ".");
111 2 : advgetopt::log << advgetopt::log_level_t::debug
112 1 : << "Test an int64_t: "
113 1 : << v
114 1 : << "."
115 1 : << advgetopt::end;
116 1 : SNAP_CATCH2_NAMESPACE::expected_logs_stack_is_empty();
117 : }
118 :
119 : // gcc sees this one as a char
120 : {
121 1 : std::uint8_t v(rand());
122 1 : SNAP_CATCH2_NAMESPACE::push_expected_log(std::string("info: Test an uint8_t: ") + static_cast<char>(v) + ".");
123 2 : advgetopt::log << advgetopt::log_level_t::info
124 1 : << "Test an uint8_t: "
125 1 : << v
126 1 : << "."
127 1 : << advgetopt::end;
128 1 : SNAP_CATCH2_NAMESPACE::expected_logs_stack_is_empty();
129 : }
130 :
131 : {
132 1 : std::uint16_t v(rand());
133 1 : SNAP_CATCH2_NAMESPACE::push_expected_log("warning: Test an uint16_t: " + std::to_string(static_cast<int>(v)) + ".");
134 2 : advgetopt::log << advgetopt::log_level_t::warning
135 1 : << "Test an uint16_t: "
136 1 : << v
137 1 : << "."
138 1 : << advgetopt::end;
139 1 : SNAP_CATCH2_NAMESPACE::expected_logs_stack_is_empty();
140 : }
141 :
142 : {
143 1 : std::uint32_t v(rand());
144 1 : SNAP_CATCH2_NAMESPACE::push_expected_log("error: Test an uint32_t: " + std::to_string(v) + ".");
145 2 : advgetopt::log << advgetopt::log_level_t::error
146 1 : << "Test an uint32_t: "
147 1 : << v
148 1 : << "."
149 1 : << advgetopt::end;
150 1 : SNAP_CATCH2_NAMESPACE::expected_logs_stack_is_empty();
151 : }
152 :
153 : {
154 1 : std::uint64_t v(rand());
155 1 : SNAP_CATCH2_NAMESPACE::push_expected_log("fatal: Test an uint64_t: " + std::to_string(v) + ".");
156 2 : advgetopt::log << advgetopt::log_level_t::fatal
157 1 : << "Test an uint64_t: "
158 1 : << v
159 1 : << "."
160 1 : << advgetopt::end;
161 1 : SNAP_CATCH2_NAMESPACE::expected_logs_stack_is_empty();
162 : }
163 :
164 : CATCH_END_SECTION()
165 3 : }
166 :
167 :
168 3 : CATCH_TEST_CASE("logger_without_callback", "[logger][valid][log]")
169 : {
170 2 : CATCH_START_SECTION("Verify log string")
171 : // cancel the callback for one test
172 1 : advgetopt::set_log_callback(nullptr);
173 :
174 : //SNAP_CATCH2_NAMESPACE::push_expected_log("debug: Test without a callback."); -- not going to be registered!
175 2 : advgetopt::log << advgetopt::log_level_t::debug
176 1 : << "Test without a callback."
177 1 : << advgetopt::end;
178 :
179 : // restore the callback
180 1 : advgetopt::set_log_callback(SNAP_CATCH2_NAMESPACE::log_for_test);
181 : CATCH_END_SECTION()
182 1 : }
183 :
184 :
185 3 : CATCH_TEST_CASE("invalid_logger", "[logger][invalid][log]")
186 : {
187 2 : CATCH_START_SECTION("Verify invalid log levels")
188 101 : for(int i(0); i < 100; ++i)
189 : {
190 100 : advgetopt::log_level_t level(advgetopt::log_level_t::warning);
191 0 : for(;;)
192 : {
193 100 : level = static_cast<advgetopt::log_level_t>(rand());
194 100 : if(level != advgetopt::log_level_t::debug
195 100 : && level != advgetopt::log_level_t::info
196 100 : && level != advgetopt::log_level_t::warning
197 100 : && level != advgetopt::log_level_t::error
198 100 : && level != advgetopt::log_level_t::fatal)
199 : {
200 100 : break;
201 : }
202 : }
203 :
204 100 : CATCH_REQUIRE_THROWS_MATCHES(advgetopt::to_string(level)
205 : , advgetopt::getopt_exception_invalid
206 : , Catch::Matchers::ExceptionMessage(
207 : "unknown log level ("
208 : + std::to_string(static_cast<int>(level))
209 : + ")"));
210 : }
211 : CATCH_END_SECTION()
212 7 : }
213 :
214 :
215 : // vim: ts=4 sw=4 et
|