LCOV - code coverage report
Current view: top level - tests - catch_message.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 341 342 99.7 %
Date: 2021-10-14 20:12:47 Functions: 8 8 100.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13