Line data Source code
1 : // Copyright (c) 2006-2024 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("Verify log levels") 55 1 : CATCH_REQUIRE(to_string(cppthread::log_level_t::debug) == "debug"); 56 1 : CATCH_REQUIRE(to_string(cppthread::log_level_t::info) == "info"); 57 1 : CATCH_REQUIRE(to_string(cppthread::log_level_t::warning) == "warning"); 58 1 : CATCH_REQUIRE(to_string(cppthread::log_level_t::error) == "error"); 59 1 : CATCH_REQUIRE(to_string(cppthread::log_level_t::fatal) == "fatal"); 60 3 : CATCH_END_SECTION() 61 : 62 3 : CATCH_START_SECTION("Verify log string") 63 1 : SNAP_CATCH2_NAMESPACE::push_expected_log("debug: Test a regular string."); 64 2 : cppthread::log << cppthread::log_level_t::debug 65 1 : << "Test a regular string." 66 2 : << cppthread::end; 67 1 : SNAP_CATCH2_NAMESPACE::expected_logs_stack_is_empty(); 68 : 69 1 : SNAP_CATCH2_NAMESPACE::push_expected_log("info: Test an std::string."); 70 2 : std::string const msg("Test an std::string."); 71 2 : cppthread::log << cppthread::log_level_t::info 72 1 : << msg 73 2 : << cppthread::end; 74 1 : SNAP_CATCH2_NAMESPACE::expected_logs_stack_is_empty(); 75 4 : CATCH_END_SECTION() 76 : 77 3 : CATCH_START_SECTION("Verify log integers") 78 : // gcc sees this one as a char 79 : { 80 1 : std::int8_t v(rand()); 81 1 : SNAP_CATCH2_NAMESPACE::push_expected_log(std::string("warning: Test an int8_t: ") + static_cast<char>(v) + "."); 82 2 : cppthread::log << cppthread::log_level_t::warning 83 1 : << "Test an int8_t: " 84 1 : << v 85 1 : << "." 86 2 : << cppthread::end; 87 1 : SNAP_CATCH2_NAMESPACE::expected_logs_stack_is_empty(); 88 : } 89 : 90 : { 91 1 : std::int16_t v(rand()); 92 1 : SNAP_CATCH2_NAMESPACE::push_expected_log("error: Test an int16_t: " + std::to_string(v) + "."); 93 2 : cppthread::log << cppthread::log_level_t::error 94 1 : << "Test an int16_t: " 95 1 : << v 96 1 : << "." 97 2 : << cppthread::end; 98 1 : SNAP_CATCH2_NAMESPACE::expected_logs_stack_is_empty(); 99 : } 100 : 101 : { 102 1 : std::int32_t v(rand()); 103 1 : SNAP_CATCH2_NAMESPACE::push_expected_log("fatal: Test an int32_t: " + std::to_string(v) + "."); 104 2 : cppthread::log << cppthread::log_level_t::fatal 105 1 : << "Test an int32_t: " 106 1 : << v 107 1 : << "." 108 2 : << cppthread::end; 109 1 : SNAP_CATCH2_NAMESPACE::expected_logs_stack_is_empty(); 110 : } 111 : 112 : { 113 1 : std::int64_t v(rand()); 114 1 : SNAP_CATCH2_NAMESPACE::push_expected_log("debug: Test an int64_t: " + std::to_string(v) + "."); 115 2 : cppthread::log << cppthread::log_level_t::debug 116 1 : << "Test an int64_t: " 117 1 : << v 118 1 : << "." 119 2 : << cppthread::end; 120 1 : SNAP_CATCH2_NAMESPACE::expected_logs_stack_is_empty(); 121 : } 122 : 123 : // gcc sees this one as a char 124 : { 125 1 : std::uint8_t v(rand()); 126 1 : SNAP_CATCH2_NAMESPACE::push_expected_log(std::string("info: Test an uint8_t: ") + static_cast<char>(v) + "."); 127 2 : cppthread::log << cppthread::log_level_t::info 128 1 : << "Test an uint8_t: " 129 1 : << v 130 1 : << "." 131 2 : << cppthread::end; 132 1 : SNAP_CATCH2_NAMESPACE::expected_logs_stack_is_empty(); 133 : } 134 : 135 : { 136 1 : std::uint16_t v(rand()); 137 1 : SNAP_CATCH2_NAMESPACE::push_expected_log("warning: Test an uint16_t: " + std::to_string(static_cast<int>(v)) + "."); 138 2 : cppthread::log << cppthread::log_level_t::warning 139 1 : << "Test an uint16_t: " 140 1 : << v 141 1 : << "." 142 2 : << cppthread::end; 143 1 : SNAP_CATCH2_NAMESPACE::expected_logs_stack_is_empty(); 144 : } 145 : 146 : { 147 1 : std::uint32_t v(rand()); 148 1 : SNAP_CATCH2_NAMESPACE::push_expected_log("error: Test an uint32_t: " + std::to_string(v) + "."); 149 2 : cppthread::log << cppthread::log_level_t::error 150 1 : << "Test an uint32_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::uint64_t v(rand()); 159 1 : SNAP_CATCH2_NAMESPACE::push_expected_log("fatal: Test an uint64_t: " + std::to_string(v) + "."); 160 2 : cppthread::log << cppthread::log_level_t::fatal 161 1 : << "Test an uint64_t: " 162 1 : << v 163 1 : << "." 164 2 : << cppthread::end; 165 1 : SNAP_CATCH2_NAMESPACE::expected_logs_stack_is_empty(); 166 : } 167 : 168 3 : CATCH_END_SECTION() 169 3 : } 170 : 171 : 172 1 : CATCH_TEST_CASE("logger_without_callback", "[logger][valid][log]") 173 : { 174 1 : CATCH_START_SECTION("Verify log string") 175 : // cancel the callback for one test 176 1 : cppthread::set_log_callback(nullptr); 177 : 178 : //SNAP_CATCH2_NAMESPACE::push_expected_log("debug: Test without a callback."); -- not going to be registered! 179 2 : cppthread::log << cppthread::log_level_t::debug 180 1 : << "Test without a callback." 181 2 : << cppthread::end; 182 : 183 : // restore the callback 184 1 : cppthread::set_log_callback(SNAP_CATCH2_NAMESPACE::log_for_test); 185 : 186 : // make sure we have at least one assertion otherwise the 187 : // coverage will fail with an error 188 : // 189 1 : CATCH_REQUIRE(true); 190 1 : CATCH_END_SECTION() 191 1 : } 192 : 193 : 194 1 : CATCH_TEST_CASE("invalid_logger", "[logger][invalid][log]") 195 : { 196 1 : CATCH_START_SECTION("Verify invalid log levels") 197 101 : for(int i(0); i < 100; ++i) 198 : { 199 100 : cppthread::log_level_t level(cppthread::log_level_t::warning); 200 : for(;;) 201 : { 202 100 : level = static_cast<cppthread::log_level_t>(rand()); 203 100 : if(level != cppthread::log_level_t::debug 204 100 : && level != cppthread::log_level_t::info 205 100 : && level != cppthread::log_level_t::warning 206 100 : && level != cppthread::log_level_t::error 207 100 : && level != cppthread::log_level_t::fatal) 208 : { 209 100 : break; 210 : } 211 : } 212 : 213 100 : CATCH_REQUIRE_THROWS_MATCHES(cppthread::to_string(level) 214 : , cppthread::cppthread_invalid_error 215 : , Catch::Matchers::ExceptionMessage( 216 : "cppthread_exception: unknown log level (" 217 : + std::to_string(static_cast<int>(level)) 218 : + ")")); 219 : } 220 1 : CATCH_END_SECTION() 221 1 : } 222 : 223 : 224 : // vim: ts=4 sw=4 et