LCOV - code coverage report
Current view: top level - tests - catch_message.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 336 337 99.7 %
Date: 2022-01-29 21:11:29 Functions: 8 8 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : // Copyright (c) 2006-2022  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 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             : // self
      21             : //
      22             : #include    "catch_main.h"
      23             : 
      24             : 
      25             : // snaplogger lib
      26             : //
      27             : #include    <snaplogger/buffer_appender.h>
      28             : #include    <snaplogger/exception.h>
      29             : #include    <snaplogger/format.h>
      30             : #include    <snaplogger/logger.h>
      31             : #include    <snaplogger/map_diagnostic.h>
      32             : #include    <snaplogger/message.h>
      33             : #include    <snaplogger/severity.h>
      34             : #include    <snaplogger/version.h>
      35             : 
      36             : 
      37             : // C lib
      38             : //
      39             : #include    <math.h>
      40             : #include    <unistd.h>
      41             : 
      42             : 
      43             : 
      44           4 : CATCH_TEST_CASE("not_a_message", "[message]")
      45             : {
      46           4 :     CATCH_START_SECTION("message: Call send_message() with wrong ostream")
      47             :     {
      48           1 :         CATCH_REQUIRE_THROWS_MATCHES(
      49             :                   snaplogger::send_message(std::cout)
      50             :                 , snaplogger::not_a_message
      51             :                 , Catch::Matchers::ExceptionMessage(
      52             :                           "logger_error: the 'out' parameter to the send_message() function is expected to be a snaplogger::message object."));
      53             :     }
      54             :     CATCH_END_SECTION()
      55             : 
      56           4 :     CATCH_START_SECTION("message: Print snaplogger::secure to wrong ostream")
      57             :     {
      58           2 :         std::stringstream buffer;
      59           1 :         std::streambuf * old(std::cout.rdbuf(buffer.rdbuf()));
      60           1 :         std::cout << snaplogger::secure << std::endl;
      61           1 :         CATCH_REQUIRE(buffer.str() == "(section:secure)\n");
      62           1 :         std::cout.rdbuf(old);
      63             :     }
      64             :     CATCH_END_SECTION()
      65           2 : }
      66             : 
      67             : 
      68             : 
      69           4 : CATCH_TEST_CASE("message_capture", "[message]")
      70             : {
      71           4 :     CATCH_START_SECTION("message: Buffering")
      72             :     {
      73           1 :         snaplogger::set_diagnostic(snaplogger::DIAG_KEY_PROGNAME, "message-logging");
      74             : 
      75           2 :         snaplogger::logger::pointer_t l(snaplogger::logger::get_instance());
      76           2 :         snaplogger::buffer_appender::pointer_t buffer(std::make_shared<snaplogger::buffer_appender>("test-buffer"));
      77             : 
      78           1 :         CATCH_REQUIRE(buffer->get_type() == "buffer");
      79             : 
      80           1 :         char const * cargv[] =
      81             :         {
      82             :             "/usr/bin/daemon",
      83             :             nullptr
      84             :         };
      85           1 :         int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
      86           1 :         char ** argv = const_cast<char **>(cargv);
      87             : 
      88           1 :         advgetopt::options_environment environment_options;
      89           1 :         environment_options.f_project_name = "test-logger";
      90           1 :         environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
      91           2 :         advgetopt::getopt opts(environment_options);
      92           1 :         opts.parse_program_name(argv);
      93           1 :         opts.parse_arguments(argc, argv, advgetopt::option_source_t::SOURCE_COMMAND_LINE);
      94             : 
      95           1 :         buffer->set_config(opts);
      96             : 
      97           2 :         snaplogger::format::pointer_t f(std::make_shared<snaplogger::format>("${severity}: ${message}"));
      98           1 :         buffer->set_format(f);
      99             : 
     100           1 :         l->add_appender(buffer);
     101             : 
     102           1 :         SNAP_LOG_ERROR << snaplogger::precise_time << "Logging this error" << SNAP_LOG_SEND;
     103           1 :         CATCH_REQUIRE(buffer->str() == "error: Logging this error\n");
     104             : 
     105             :         // test the other str() function too
     106             :         //
     107           1 :         buffer->str("Start: ");
     108             : 
     109             :         // show that the "\n" does not get duplicated
     110             :         //
     111           1 :         SNAP_LOG_ERROR << "Error with newline\n" << SNAP_LOG_SEND;
     112           1 :         CATCH_REQUIRE(buffer->str() == "Start: error: Error with newline\n");
     113           1 :         buffer->clear();
     114             : 
     115             :         // show that the "\r\n" gets replaced by "\n"
     116             :         //
     117           1 :         SNAP_LOG_ERROR << "Error with CRLF\r\n" << SNAP_LOG_SEND;
     118           1 :         CATCH_REQUIRE(buffer->str() == "error: Error with CRLF\n");
     119           1 :         buffer->clear();
     120             : 
     121             :         // severity too low, no change to buffer
     122             :         //
     123           1 :         SNAP_LOG_DEBUG << "Debug Message " << M_PI << " which does not make it at all...\n" << SNAP_LOG_SEND;
     124           1 :         CATCH_REQUIRE(buffer->empty());
     125             : 
     126           1 :         l->reset();
     127             :     }
     128             :     CATCH_END_SECTION()
     129             : 
     130           4 :     CATCH_START_SECTION("message: JSON Buffering")
     131             :     {
     132           1 :         snaplogger::set_diagnostic(snaplogger::DIAG_KEY_PROGNAME, "json-logging");
     133             : 
     134           2 :         snaplogger::logger::pointer_t l(snaplogger::logger::get_instance());
     135           2 :         snaplogger::buffer_appender::pointer_t buffer(std::make_shared<snaplogger::buffer_appender>("json-buffer"));
     136             : 
     137           1 :         CATCH_REQUIRE(buffer->get_type() == "buffer");
     138             : 
     139           1 :         char const * cargv[] =
     140             :         {
     141             :             "/usr/bin/daemon",
     142             :             nullptr
     143             :         };
     144           1 :         int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
     145           1 :         char ** argv = const_cast<char **>(cargv);
     146             : 
     147           1 :         advgetopt::options_environment environment_options;
     148           1 :         environment_options.f_project_name = "json-logger";
     149           1 :         environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
     150           2 :         advgetopt::getopt opts(environment_options);
     151           1 :         opts.parse_program_name(argv);
     152           1 :         opts.parse_arguments(argc, argv, advgetopt::option_source_t::SOURCE_COMMAND_LINE);
     153             : 
     154           1 :         buffer->set_config(opts);
     155             : 
     156           1 :         snaplogger::format::pointer_t f(std::make_shared<snaplogger::format>(
     157             :                     "{\"version\":1,"
     158           2 :                     "\"message\":\"${message:escape='\\\r\n\t\"'}\"}"));
     159           1 :         buffer->set_format(f);
     160             : 
     161           1 :         l->add_appender(buffer);
     162             : 
     163           3 :         SNAP_LOG_ERROR
     164           2 :             << SNAP_LOG_FIELD(std::string("format"), std::string("json"))
     165             :             << "A JSON error message (format:${field:name=format})"
     166             :             << SNAP_LOG_SEND;
     167           1 :         CATCH_REQUIRE(buffer->str() == "{\"version\":1,\"message\":\"A JSON error message (format:json)\"}\n");
     168           1 :         buffer->clear();
     169             : 
     170             :         // show that the "\n" does not get duplicated
     171             :         //
     172           4 :         SNAP_LOG_ERROR
     173             :             << "See what happens with a \"quoted string\" within the message (${fields})\n"
     174           2 :             << SNAP_LOG_FIELD(std::string("format"), std::string("json"))
     175           2 :             << SNAP_LOG_FIELD(std::string("language"), std::string("js"))
     176             :             << SNAP_LOG_SEND;
     177           2 :         std::string const expected(std::regex_replace(buffer->str(), std::regex("\\\\\"id\\\\\":\\\\\"[0-9]+\\\\\","), ""));
     178           1 :         CATCH_REQUIRE(expected == "{\"version\":1,\"message\":\"See what happens with a \\\"quoted string\\\" within the message ({\\\"format\\\":\\\"json\\\",\\\"language\\\":\\\"js\\\"})\"}\n");
     179           1 :         buffer->clear();
     180             : 
     181           1 :         l->reset();
     182             :     }
     183             :     CATCH_END_SECTION()
     184           2 : }
     185             : 
     186             : 
     187           3 : CATCH_TEST_CASE("message_copy", "[message]")
     188             : {
     189           2 :     CATCH_START_SECTION("message: Copy")
     190             :     {
     191           1 :         snaplogger::set_diagnostic(snaplogger::DIAG_KEY_PROGNAME, "message-copying");
     192             : 
     193           2 :         snaplogger::logger::pointer_t l(snaplogger::logger::get_instance());
     194           2 :         snaplogger::buffer_appender::pointer_t buffer(std::make_shared<snaplogger::buffer_appender>("test-buffer"));
     195             : 
     196           1 :         CATCH_REQUIRE(buffer->get_type() == "buffer");
     197             : 
     198           1 :         char const * cargv[] =
     199             :         {
     200             :             "/usr/bin/daemon",
     201             :             nullptr
     202             :         };
     203           1 :         int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
     204           1 :         char ** argv = const_cast<char **>(cargv);
     205             : 
     206           1 :         advgetopt::options_environment environment_options;
     207           1 :         environment_options.f_project_name = "test-logger";
     208           1 :         environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
     209           2 :         advgetopt::getopt opts(environment_options);
     210           1 :         opts.parse_program_name(argv);
     211           1 :         opts.parse_arguments(argc, argv, advgetopt::option_source_t::SOURCE_COMMAND_LINE);
     212             : 
     213           1 :         buffer->set_config(opts);
     214             : 
     215           2 :         snaplogger::format::pointer_t f(std::make_shared<snaplogger::format>("${message}"));
     216           1 :         buffer->set_format(f);
     217             : 
     218           1 :         l->add_appender(buffer);
     219             : 
     220           1 :         int const line_number = __LINE__;
     221           1 :         snaplogger::message::pointer_t msg(std::make_shared<snaplogger::message>
     222           2 :                         (::snaplogger::severity_t::SEVERITY_ERROR, __FILE__, __func__, line_number));
     223             : 
     224           1 :         CATCH_REQUIRE(msg->get_filename() == __FILE__);
     225           1 :         CATCH_REQUIRE(msg->get_function() == __func__);
     226           1 :         CATCH_REQUIRE(msg->get_line() == line_number);
     227             : 
     228           1 :         msg->set_filename("logger.cpp");
     229           1 :         msg->set_function("magical");
     230           1 :         msg->set_line(701);
     231             : 
     232           1 :         CATCH_REQUIRE(msg->get_filename() == "logger.cpp");
     233           1 :         CATCH_REQUIRE(msg->get_function() == "magical");
     234           1 :         CATCH_REQUIRE(msg->get_line() == 701);
     235             : 
     236           1 :         *msg << "Logging an error.";
     237             : 
     238           1 :         CATCH_REQUIRE(msg->str() == "Logging an error.");
     239             : 
     240           2 :         snaplogger::message::pointer_t copy(std::make_shared<snaplogger::message>(*msg, *msg));
     241             : 
     242           1 :         CATCH_REQUIRE(msg->str() == "Logging an error.");
     243           1 :         CATCH_REQUIRE(copy->str() == "Logging an error.");
     244             : 
     245             :         // no destructor called, the output is still empty
     246             :         //
     247           1 :         CATCH_REQUIRE(buffer->empty());
     248             : 
     249           1 :         copy.reset();
     250             : 
     251             :         // msg not lost
     252             :         //
     253           1 :         CATCH_REQUIRE(msg->str() == "Logging an error.");
     254             : 
     255             :         // destructor against copy does not trigger send_message()
     256             :         //
     257           1 :         CATCH_REQUIRE(buffer->empty());
     258             : 
     259           1 :         snaplogger::send_message(*msg);
     260             : 
     261             :         // now we get the message as expected!
     262             :         //
     263             :         // (note that internally we can skip receiving the message on the
     264             :         // original, but not as a client... we may want to have the ability
     265             :         // to cancel a message, though.)
     266             :         //
     267           1 :         CATCH_REQUIRE(buffer->str() == "Logging an error.\n");
     268             : 
     269           1 :         msg.reset();
     270             : 
     271           1 :         CATCH_REQUIRE(buffer->str() == "Logging an error.\n");
     272             : 
     273           1 :         l->reset();
     274             :     }
     275             :     CATCH_END_SECTION()
     276           1 : }
     277             : 
     278             : 
     279           4 : CATCH_TEST_CASE("message_severity", "[message][severity]")
     280             : {
     281           4 :     CATCH_START_SECTION("message: Appender vs Message severity")
     282             :     {
     283           1 :         snaplogger::set_diagnostic(snaplogger::DIAG_KEY_PROGNAME, "message-severity");
     284             : 
     285           2 :         snaplogger::logger::pointer_t l(snaplogger::logger::get_instance());
     286           2 :         snaplogger::buffer_appender::pointer_t buffer(std::make_shared<snaplogger::buffer_appender>("test-buffer"));
     287             : 
     288           1 :         char const * cargv[] =
     289             :         {
     290             :             "/usr/bin/daemon",
     291             :             nullptr
     292             :         };
     293           1 :         int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
     294           1 :         char ** argv = const_cast<char **>(cargv);
     295             : 
     296           1 :         advgetopt::options_environment environment_options;
     297           1 :         environment_options.f_project_name = "test-logger";
     298           1 :         environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
     299           2 :         advgetopt::getopt opts(environment_options);
     300           1 :         opts.parse_program_name(argv);
     301           1 :         opts.parse_arguments(argc, argv, advgetopt::option_source_t::SOURCE_COMMAND_LINE);
     302             : 
     303           1 :         buffer->set_config(opts);
     304             : 
     305           2 :         snaplogger::format::pointer_t f(std::make_shared<snaplogger::format>("${message}"));
     306           1 :         buffer->set_format(f);
     307             : 
     308           1 :         l->add_appender(buffer);
     309             : 
     310           1 :         int const min_severity(static_cast<int>(snaplogger::severity_t::SEVERITY_MIN));
     311           1 :         int const max_severity(static_cast<int>(snaplogger::severity_t::SEVERITY_MAX));
     312         257 :         for(int i(min_severity); i <= max_severity; ++i)
     313             :         {
     314         256 :             buffer->set_severity(static_cast<snaplogger::severity_t>(i));
     315       65792 :             for(int j(min_severity); j <= max_severity; ++j)
     316             :             {
     317      131072 :                 snaplogger::send_message(
     318      131072 :                     *::snaplogger::create_message(
     319             :                               static_cast<::snaplogger::severity_t>(j)
     320             :                             , __FILE__
     321             :                             , __func__
     322             :                             , __LINE__
     323       65536 :                         ) << "The message itself");
     324             : 
     325       65536 :                 if(j >= i
     326       32896 :                 && i != static_cast<int>(snaplogger::severity_t::SEVERITY_OFF)
     327       32895 :                 && j != static_cast<int>(snaplogger::severity_t::SEVERITY_OFF))
     328             :                 {
     329       32640 :                     CATCH_REQUIRE(buffer->str() == "The message itself\n");
     330             :                 }
     331             :                 else
     332             :                 {
     333       32896 :                     CATCH_REQUIRE(buffer->empty());
     334             :                 }
     335       65536 :                 buffer->clear();
     336             :             }
     337             :         }
     338             : 
     339           1 :         l->reset();
     340             :     }
     341             :     CATCH_END_SECTION()
     342             : 
     343           4 :     CATCH_START_SECTION("message: Changing message severity (takes about 3.5min)")
     344             :     {
     345           1 :         snaplogger::set_diagnostic(snaplogger::DIAG_KEY_PROGNAME, "message-copying");
     346             : 
     347           2 :         snaplogger::logger::pointer_t l(snaplogger::logger::get_instance());
     348           2 :         snaplogger::buffer_appender::pointer_t buffer(std::make_shared<snaplogger::buffer_appender>("test-buffer"));
     349             : 
     350           1 :         char const * cargv[] =
     351             :         {
     352             :             "/usr/bin/daemon",
     353             :             nullptr
     354             :         };
     355           1 :         int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
     356           1 :         char ** argv = const_cast<char **>(cargv);
     357             : 
     358           1 :         advgetopt::options_environment environment_options;
     359           1 :         environment_options.f_project_name = "test-logger";
     360           1 :         environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
     361           2 :         advgetopt::getopt opts(environment_options);
     362           1 :         opts.parse_program_name(argv);
     363           1 :         opts.parse_arguments(argc, argv, advgetopt::option_source_t::SOURCE_COMMAND_LINE);
     364             : 
     365           1 :         buffer->set_config(opts);
     366             : 
     367           2 :         snaplogger::format::pointer_t f(std::make_shared<snaplogger::format>("${message}"));
     368           1 :         buffer->set_format(f);
     369             : 
     370           1 :         l->add_appender(buffer);
     371             : 
     372           1 :         int const min_severity(static_cast<int>(snaplogger::severity_t::SEVERITY_MIN));
     373           1 :         int const max_severity(static_cast<int>(snaplogger::severity_t::SEVERITY_MAX));
     374          34 :         for(int i(min_severity); i <= max_severity; i += 1 + (rand() & 15))
     375             :         {
     376          33 :             buffer->set_severity(static_cast<snaplogger::severity_t>(i));
     377        1085 :             for(int j(min_severity); j <= max_severity; j += 1 + (rand() & 15))
     378             :             {
     379       33346 :                 for(int k(min_severity); k <= max_severity; k += 1 + (rand() & 15))
     380             :                 {
     381       32294 :                     ::snaplogger::message::pointer_t msg(std::make_shared<::snaplogger::message>(
     382       64588 :                               static_cast<::snaplogger::severity_t>(j)
     383             :                             , __FILE__
     384             :                             , __func__
     385             :                             , __LINE__
     386      129176 :                         ));
     387       32294 :                     *msg << "Start of message";
     388       32294 :                     msg->set_severity(static_cast<::snaplogger::severity_t>(k));
     389       32294 :                     *msg << " -- end of message";
     390       32294 :                     snaplogger::send_message(*msg);
     391             : //std::cerr << "checking with " << i << ", " << j << ", " << k << "\n";
     392             : 
     393       32294 :                     if(j >= i
     394       16358 :                     && k >= i
     395       11076 :                     && i != static_cast<int>(snaplogger::severity_t::SEVERITY_OFF)
     396       11076 :                     && j != static_cast<int>(snaplogger::severity_t::SEVERITY_OFF))
     397             :                     //&& k != static_cast<int>(snaplogger::severity_t::SEVERITY_OFF))
     398             :                     {
     399       11049 :                         if(j >= i)
     400             :                         {
     401       11049 :                             CATCH_REQUIRE(buffer->str() == "Start of message -- end of message\n");
     402             :                         }
     403             :                         else
     404             :                         {
     405           0 :                             CATCH_REQUIRE(buffer->str() == "Start of message\n");
     406       11049 :                         }
     407             :                     }
     408             :                     else
     409             :                     {
     410       21245 :                         CATCH_REQUIRE(buffer->empty());
     411             :                     }
     412       32294 :                     buffer->clear();
     413             :                 }
     414             :             }
     415             :         }
     416             : 
     417           1 :         l->reset();
     418             :     }
     419             :     CATCH_END_SECTION()
     420           2 : }
     421             : 
     422             : 
     423           6 : CATCH_TEST_CASE("message_format", "[message][format]")
     424             : {
     425           8 :     CATCH_START_SECTION("message: Recursivity")
     426             :     {
     427           1 :         snaplogger::set_diagnostic(snaplogger::DIAG_KEY_PROGNAME, "basic-format");
     428             : 
     429             :         // these two are not called in this test
     430             :         //
     431           1 :         snaplogger::set_diagnostic(snaplogger::DIAG_KEY_PROJECT_NAME, "test-logger");
     432           1 :         snaplogger::set_diagnostic(snaplogger::DIAG_KEY_VERSION, "5.32.1024");
     433             : 
     434           2 :         snaplogger::logger::pointer_t l(snaplogger::logger::get_instance());
     435           2 :         snaplogger::buffer_appender::pointer_t buffer(std::make_shared<snaplogger::buffer_appender>("test-buffer"));
     436             : 
     437           1 :         char const * cargv[] =
     438             :         {
     439             :             "/usr/bin/daemon",
     440             :             nullptr
     441             :         };
     442           1 :         int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
     443           1 :         char ** argv = const_cast<char **>(cargv);
     444             : 
     445           1 :         advgetopt::options_environment environment_options;
     446           1 :         environment_options.f_project_name = "test-logger";
     447           1 :         environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
     448           1 :         environment_options.f_version = "5.32.1024";
     449           2 :         advgetopt::getopt opts(environment_options);
     450           1 :         opts.parse_program_name(argv);
     451           1 :         opts.parse_arguments(argc, argv, advgetopt::option_source_t::SOURCE_COMMAND_LINE);
     452             : 
     453           1 :         buffer->set_config(opts);
     454             : 
     455           2 :         snaplogger::format::pointer_t f(std::make_shared<snaplogger::format>("${project_name} ${message} v${version}"));
     456           1 :         buffer->set_format(f);
     457             : 
     458           1 :         l->add_appender(buffer);
     459             : 
     460           1 :         SNAP_LOG_WARNING
     461             :             << "Message Project Name = ${project_name} and Version = ${version} -- uses \"recursive\""
     462             :             << SNAP_LOG_SEND;
     463             : 
     464           1 :         CATCH_REQUIRE(buffer->str() ==
     465             :                 "test-logger Message Project Name = test-logger and"
     466             :                 " Version = 5.32.1024 -- uses \"recursive\" v5.32.1024"
     467             :                 "\n");
     468             : 
     469           1 :         l->reset();
     470             :     }
     471             :     CATCH_END_SECTION()
     472             : 
     473           8 :     CATCH_START_SECTION("message: ${message} itself is not recursive")
     474             :     {
     475           1 :         snaplogger::set_diagnostic(snaplogger::DIAG_KEY_PROGNAME, "prevent-infinite-loop");
     476             : 
     477             :         // these two are not called in this test
     478             :         //
     479           1 :         snaplogger::set_diagnostic(snaplogger::DIAG_KEY_PROJECT_NAME, "test-logger");
     480           1 :         snaplogger::set_diagnostic(snaplogger::DIAG_KEY_VERSION, "5.32.1024");
     481             : 
     482           2 :         snaplogger::logger::pointer_t l(snaplogger::logger::get_instance());
     483           2 :         snaplogger::buffer_appender::pointer_t buffer(std::make_shared<snaplogger::buffer_appender>("test-buffer"));
     484             : 
     485           1 :         char const * cargv[] =
     486             :         {
     487             :             "/usr/bin/daemon",
     488             :             nullptr
     489             :         };
     490           1 :         int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
     491           1 :         char ** argv = const_cast<char **>(cargv);
     492             : 
     493           1 :         advgetopt::options_environment environment_options;
     494           1 :         environment_options.f_project_name = "test-logger";
     495           1 :         environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
     496           1 :         environment_options.f_version = "5.32.1024";
     497           2 :         advgetopt::getopt opts(environment_options);
     498           1 :         opts.parse_program_name(argv);
     499           1 :         opts.parse_arguments(argc, argv, advgetopt::option_source_t::SOURCE_COMMAND_LINE);
     500             : 
     501           1 :         buffer->set_config(opts);
     502             : 
     503           2 :         snaplogger::format::pointer_t f(std::make_shared<snaplogger::format>("${project_name} ${message} v${version}"));
     504           1 :         buffer->set_format(f);
     505             : 
     506           1 :         l->add_appender(buffer);
     507             : 
     508           1 :         SNAP_LOG_WARNING
     509             :             << "Message ${message} says: Project Name = ${project_name} and Version = ${version} -- uses \"recursive\""
     510             :             << SNAP_LOG_SEND;
     511             : 
     512           1 :         CATCH_REQUIRE(buffer->str() ==
     513             :                 "test-logger Message  says: Project Name = test-logger and"
     514             :                 " Version = 5.32.1024 -- uses \"recursive\" v5.32.1024"
     515             :                 "\n");
     516             : 
     517           1 :         buffer->clear();
     518             : 
     519           1 :         snaplogger::unset_diagnostic(snaplogger::DIAG_KEY_VERSION);
     520             : 
     521           1 :         SNAP_LOG_WARNING
     522             :             << "Removed the version: ${message} says: Project Name = ${project_name} and Version = ${version} -- uses \"recursive\""
     523             :             << SNAP_LOG_SEND;
     524             : 
     525           1 :         CATCH_REQUIRE(buffer->str() ==
     526             :                 "test-logger Removed the version:  says: Project Name = test-logger and"
     527             :                 " Version =  -- uses \"recursive\" v"
     528             :                 "\n");
     529             : 
     530           1 :         l->reset();
     531             :     }
     532             :     CATCH_END_SECTION()
     533             : 
     534           8 :     CATCH_START_SECTION("message: ${pid} uses the get_environment() function")
     535             :     {
     536           1 :         snaplogger::set_diagnostic(snaplogger::DIAG_KEY_PROGNAME, "get-environment");
     537             : 
     538           2 :         snaplogger::logger::pointer_t l(snaplogger::logger::get_instance());
     539           2 :         snaplogger::buffer_appender::pointer_t buffer(std::make_shared<snaplogger::buffer_appender>("test-buffer"));
     540             : 
     541           1 :         char const * cargv[] =
     542             :         {
     543             :             "/usr/bin/daemon",
     544             :             nullptr
     545             :         };
     546           1 :         int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
     547           1 :         char ** argv = const_cast<char **>(cargv);
     548             : 
     549           1 :         advgetopt::options_environment environment_options;
     550           1 :         environment_options.f_project_name = "test-logger";
     551           1 :         environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
     552           2 :         advgetopt::getopt opts(environment_options);
     553           1 :         opts.parse_program_name(argv);
     554           1 :         opts.parse_arguments(argc, argv, advgetopt::option_source_t::SOURCE_COMMAND_LINE);
     555             : 
     556           1 :         buffer->set_config(opts);
     557             : 
     558           2 :         snaplogger::format::pointer_t f(std::make_shared<snaplogger::format>("${message}"));
     559           1 :         buffer->set_format(f);
     560             : 
     561           1 :         l->add_appender(buffer);
     562             : 
     563           1 :         SNAP_LOG_WARNING
     564             :             << "Test PID = ${pid} == ${pid:running}"
     565             :             << SNAP_LOG_SEND;
     566             : 
     567           1 :         CATCH_REQUIRE(buffer->str() ==
     568             :                   "Test PID = " + std::to_string(getpid())
     569             :                 + " == " + std::to_string(getpid())
     570             :                 + "\n");
     571             : 
     572           1 :         l->reset();
     573             :     }
     574             :     CATCH_END_SECTION()
     575             : 
     576           8 :     CATCH_START_SECTION("message: Verify year")
     577             :     {
     578           1 :         snaplogger::set_diagnostic(snaplogger::DIAG_KEY_PROGNAME, "get-environment");
     579             : 
     580           2 :         snaplogger::logger::pointer_t l(snaplogger::logger::get_instance());
     581           1 :         CATCH_REQUIRE(l->get_appender("test-buffer") == nullptr);
     582             : 
     583           2 :         snaplogger::buffer_appender::pointer_t buffer(std::make_shared<snaplogger::buffer_appender>("test-buffer"));
     584             : 
     585           1 :         char const * cargv[] =
     586             :         {
     587             :             "/usr/bin/daemon",
     588             :             nullptr
     589             :         };
     590           1 :         int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
     591           1 :         char ** argv = const_cast<char **>(cargv);
     592             : 
     593           1 :         advgetopt::options_environment environment_options;
     594           1 :         environment_options.f_project_name = "test-logger";
     595           1 :         environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
     596           2 :         advgetopt::getopt opts(environment_options);
     597           1 :         opts.parse_program_name(argv);
     598           1 :         opts.parse_arguments(argc, argv, advgetopt::option_source_t::SOURCE_COMMAND_LINE);
     599             : 
     600           1 :         buffer->set_config(opts);
     601             : 
     602           2 :         snaplogger::format::pointer_t f(std::make_shared<snaplogger::format>("${message}"));
     603           1 :         buffer->set_format(f);
     604             : 
     605           1 :         l->add_appender(buffer);
     606             : 
     607           1 :         CATCH_REQUIRE(l->get_appender("test-buffer") == buffer);
     608             : 
     609             :         // we create a message so we can check the timestamp in our test
     610             :         //
     611           1 :         snaplogger::message::pointer_t msg(std::make_shared<snaplogger::message>
     612           2 :                         (::snaplogger::severity_t::SEVERITY_ERROR, __FILE__, __func__, __LINE__));
     613           1 :         *msg << "Created message on YYYY = ${date:year}, MM = ${date:month}, DD = ${date:day}";
     614             : 
     615           1 :         timespec const stamp(msg->get_timestamp());
     616             : 
     617           1 :         snaplogger::send_message(*msg);
     618             : 
     619           1 :         tm t;
     620           1 :         gmtime_r(&stamp.tv_sec, &t);
     621           1 :         char year[16];
     622           1 :         char month[16];
     623           1 :         char day[16];
     624           1 :         strftime(year,  16, "%Y", &t);
     625           1 :         strftime(month, 16, "%m", &t);
     626           1 :         strftime(day,   16, "%d", &t);
     627             : 
     628           1 :         CATCH_REQUIRE(buffer->str() ==
     629             :                   std::string("Created message on YYYY = ")
     630             :                 + year
     631             :                 + ", MM = "
     632             :                 + std::to_string(std::atoi(month))  // remove the leading '0' if necessary
     633             :                 + ", DD = "
     634             :                 + std::to_string(std::atoi(day))    // remove the leading '0' if necessary
     635             :                 + "\n");
     636             : 
     637           1 :         l->reset();
     638             :     }
     639             :     CATCH_END_SECTION()
     640           4 : }
     641             : 
     642             : 
     643           3 : CATCH_TEST_CASE("message_component_filter", "[message][component]")
     644             : {
     645           2 :     CATCH_START_SECTION("message: Filter Message with Component")
     646             :     {
     647           1 :         snaplogger::set_diagnostic(snaplogger::DIAG_KEY_PROGNAME, "component-filter");
     648             : 
     649           2 :         snaplogger::logger::pointer_t l(snaplogger::logger::get_instance());
     650           2 :         snaplogger::buffer_appender::pointer_t buffer(std::make_shared<snaplogger::buffer_appender>("test-buffer"));
     651             : 
     652           1 :         char const * cargv[] =
     653             :         {
     654             :             "/usr/bin/daemon",
     655             :             nullptr
     656             :         };
     657           1 :         int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
     658           1 :         char ** argv = const_cast<char **>(cargv);
     659             : 
     660           1 :         advgetopt::options_environment environment_options;
     661           1 :         environment_options.f_project_name = "test-logger";
     662           1 :         environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
     663           2 :         advgetopt::getopt opts(environment_options);
     664           1 :         opts.parse_program_name(argv);
     665           1 :         opts.parse_arguments(argc, argv, advgetopt::option_source_t::SOURCE_COMMAND_LINE);
     666             : 
     667           1 :         buffer->set_config(opts);
     668             : 
     669           2 :         snaplogger::format::pointer_t f(std::make_shared<snaplogger::format>("${message} (${severity:format=number})"));
     670           1 :         buffer->set_format(f);
     671             : 
     672           1 :         l->add_appender(buffer);
     673             : 
     674           2 :         SNAP_LOG_WARNING
     675           1 :             << snaplogger::secure       // mark as a secure message
     676             :             << "This message is secure but not the buffer"
     677             :             << SNAP_LOG_SEND;
     678             : 
     679           1 :         CATCH_REQUIRE(buffer->empty());
     680             : 
     681           2 :         SNAP_LOG_WARNING
     682           1 :             << "Test number: "
     683             :             << 2
     684           1 :             << " with buffer still unsecure..."
     685           1 :             << SNAP_LOG_SEND_SECURELY;  // mark at the end
     686             : 
     687           1 :         CATCH_REQUIRE(buffer->empty());
     688             : 
     689             :         // mark the buffer as a secure buffer now
     690             :         //
     691           1 :         buffer->add_component(snaplogger::g_secure_component);
     692             : 
     693           2 :         SNAP_LOG_WARNING
     694           1 :             << snaplogger::secure       // mark as a secure message
     695             :             << "This message is secure and so is the buffer"
     696             :             << SNAP_LOG_SEND;
     697             : 
     698             :         // TODO: get the WARNING severity level dynamically
     699           1 :         CATCH_REQUIRE(buffer->str() == "This message is secure and so is the buffer (100)\n");
     700             : 
     701           1 :         buffer->clear();
     702             : 
     703           2 :         SNAP_LOG_WARNING
     704           1 :             << "Test number: "
     705             :             << 4
     706           1 :             << " with secure buffer...\r\n"
     707           1 :             << SNAP_LOG_SEND_SECURELY;  // mark at the end
     708             : 
     709             :         // TODO: get the WARNING severity level dynamically
     710           1 :         CATCH_REQUIRE(buffer->str() == "Test number: 4 with secure buffer... (100)\n");
     711             : 
     712           1 :         l->reset();
     713             :     }
     714             :     CATCH_END_SECTION()
     715           7 : }
     716             : 
     717             : 
     718             : 
     719             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.13