LCOV - code coverage report
Current view: top level - tests - message.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 269 270 99.6 %
Date: 2021-05-29 11:58:38 Functions: 8 8 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2006-2021  Made to Order Software Corp.  All Rights Reserved
       3             :  *
       4             :  * https://snapwebsites.org/project/snaplogger
       5             :  * contact@m2osw.com
       6             :  *
       7             :  * This program is free software; you can redistribute it and/or modify
       8             :  * it under the terms of the GNU General Public License as published by
       9             :  * the Free Software Foundation; either version 2 of the License, or
      10             :  * (at your option) any later version.
      11             :  *
      12             :  * This program is distributed in the hope that it will be useful,
      13             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      14             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      15             :  * GNU General Public License for more details.
      16             :  *
      17             :  * You should have received a copy of the GNU General Public License along
      18             :  * with this program; if not, write to the Free Software Foundation, Inc.,
      19             :  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
      20             :  */
      21             : 
      22             : // self
      23             : //
      24             : #include    "main.h"
      25             : 
      26             : 
      27             : // snaplogger lib
      28             : //
      29             : #include    <snaplogger/buffer_appender.h>
      30             : #include    <snaplogger/exception.h>
      31             : #include    <snaplogger/format.h>
      32             : #include    <snaplogger/logger.h>
      33             : #include    <snaplogger/map_diagnostic.h>
      34             : #include    <snaplogger/message.h>
      35             : #include    <snaplogger/severity.h>
      36             : #include    <snaplogger/version.h>
      37             : 
      38             : 
      39             : // C lib
      40             : //
      41             : #include    <unistd.h>
      42             : 
      43             : 
      44             : 
      45           4 : CATCH_TEST_CASE("not_a_message", "[message]")
      46             : {
      47           4 :     CATCH_START_SECTION("Call send_message() with wrong ostream")
      48             :     {
      49           1 :         CATCH_REQUIRE_THROWS_MATCHES(
      50             :                   snaplogger::send_message(std::cout)
      51             :                 , snaplogger::not_a_message
      52             :                 , Catch::Matchers::ExceptionMessage(
      53             :                           "logger_error: the 'out' parameter to the send_message() function is expected to be a snaplogger::message object."));
      54             :     }
      55             :     CATCH_END_SECTION()
      56             : 
      57           4 :     CATCH_START_SECTION("Print snaplogger::secure to wrong ostream")
      58             :     {
      59           2 :         std::stringstream buffer;
      60           1 :         std::streambuf * old(std::cout.rdbuf(buffer.rdbuf()));
      61           1 :         std::cout << snaplogger::secure << std::endl;
      62           1 :         CATCH_REQUIRE(buffer.str() == "(section:secure)\n");
      63           1 :         std::cout.rdbuf(old);
      64             :     }
      65             :     CATCH_END_SECTION()
      66           2 : }
      67             : 
      68             : 
      69             : 
      70           4 : CATCH_TEST_CASE("message_capture", "[message]")
      71             : {
      72           4 :     CATCH_START_SECTION("Message Buffering")
      73             :     {
      74           1 :         snaplogger::set_diagnostic(snaplogger::DIAG_KEY_PROGNAME, "message-logging");
      75             : 
      76           2 :         snaplogger::logger::pointer_t l(snaplogger::logger::get_instance());
      77           2 :         snaplogger::buffer_appender::pointer_t buffer(std::make_shared<snaplogger::buffer_appender>("test-buffer"));
      78             : 
      79           1 :         CATCH_REQUIRE(buffer->get_type() == "buffer");
      80             : 
      81           1 :         advgetopt::options_environment opt_env;
      82           1 :         opt_env.f_project_name = "test-logger";
      83           2 :         advgetopt::getopt opts(opt_env);
      84           1 :         buffer->set_config(opts);
      85             : 
      86           2 :         snaplogger::format::pointer_t f(std::make_shared<snaplogger::format>("${severity}: ${message}"));
      87           1 :         buffer->set_format(f);
      88             : 
      89           1 :         l->add_appender(buffer);
      90             : 
      91           1 :         SNAP_LOG_ERROR << snaplogger::precise_time << "Logging this error" << SNAP_LOG_SEND;
      92           1 :         CATCH_REQUIRE(buffer->str() == "error: Logging this error\n");
      93             : 
      94             :         // test the other str() function too
      95             :         //
      96           1 :         buffer->str("Start: ");
      97             : 
      98             :         // show that the "\n" does not get duplicated
      99             :         //
     100           1 :         SNAP_LOG_ERROR << "Error with newline\n" << SNAP_LOG_SEND;
     101           1 :         CATCH_REQUIRE(buffer->str() == "Start: error: Error with newline\n");
     102           1 :         buffer->clear();
     103             : 
     104             :         // show that the "\r\n" gets replaced by "\n"
     105             :         //
     106           1 :         SNAP_LOG_ERROR << "Error with CRLF\r\n" << SNAP_LOG_SEND;
     107           1 :         CATCH_REQUIRE(buffer->str() == "error: Error with CRLF\n");
     108           1 :         buffer->clear();
     109             : 
     110             :         // severity too low, no change to buffer
     111             :         //
     112           1 :         SNAP_LOG_DEBUG << "Debug Message " << M_PI << " which does not make it at all...\n" << SNAP_LOG_SEND;
     113           1 :         CATCH_REQUIRE(buffer->empty());
     114             : 
     115           1 :         l->reset();
     116             :     }
     117             :     CATCH_END_SECTION()
     118             : 
     119           4 :     CATCH_START_SECTION("JSON Buffering")
     120             :     {
     121           1 :         snaplogger::set_diagnostic(snaplogger::DIAG_KEY_PROGNAME, "json-logging");
     122             : 
     123           2 :         snaplogger::logger::pointer_t l(snaplogger::logger::get_instance());
     124           2 :         snaplogger::buffer_appender::pointer_t buffer(std::make_shared<snaplogger::buffer_appender>("json-buffer"));
     125             : 
     126           1 :         CATCH_REQUIRE(buffer->get_type() == "buffer");
     127             : 
     128           1 :         advgetopt::options_environment opt_env;
     129           1 :         opt_env.f_project_name = "json-logger";
     130           2 :         advgetopt::getopt opts(opt_env);
     131           1 :         buffer->set_config(opts);
     132             : 
     133             :         snaplogger::format::pointer_t f(std::make_shared<snaplogger::format>(
     134             :                     "{\"version\":1,"
     135           2 :                     "\"message\":\"${message:escape='\\\r\n\t\"'}\"}"));
     136           1 :         buffer->set_format(f);
     137             : 
     138           1 :         l->add_appender(buffer);
     139             : 
     140           3 :         SNAP_LOG_ERROR
     141           2 :             << SNAP_LOG_FIELD(std::string("format"), std::string("json"))
     142             :             << "A JSON error message (format:${field:name=format})"
     143             :             << SNAP_LOG_SEND;
     144           1 :         CATCH_REQUIRE(buffer->str() == "{\"version\":1,\"message\":\"A JSON error message (format:json)\"}\n");
     145           1 :         buffer->clear();
     146             : 
     147             :         // show that the "\n" does not get duplicated
     148             :         //
     149           4 :         SNAP_LOG_ERROR
     150           1 :             << "See what happens with a \"quoted string\" within the message (${fields})\n"
     151           2 :             << SNAP_LOG_FIELD(std::string("format"), std::string("json"))
     152           2 :             << SNAP_LOG_FIELD(std::string("language"), std::string("js"))
     153             :             << SNAP_LOG_SEND;
     154           1 :         CATCH_REQUIRE(buffer->str() == "{\"version\":1,\"message\":\"See what happens with a \\\"quoted string\\\" within the message ({\\\"format\\\":\\\"json\\\",\\\"language\\\":\\\"js\\\"})\"}\n");
     155           1 :         buffer->clear();
     156             : 
     157           1 :         l->reset();
     158             :     }
     159             :     CATCH_END_SECTION()
     160           2 : }
     161             : 
     162             : 
     163           3 : CATCH_TEST_CASE("message_copy", "[message]")
     164             : {
     165           2 :     CATCH_START_SECTION("Copy Message")
     166             :     {
     167           1 :         snaplogger::set_diagnostic(snaplogger::DIAG_KEY_PROGNAME, "message-copying");
     168             : 
     169           2 :         snaplogger::logger::pointer_t l(snaplogger::logger::get_instance());
     170           2 :         snaplogger::buffer_appender::pointer_t buffer(std::make_shared<snaplogger::buffer_appender>("test-buffer"));
     171             : 
     172           1 :         CATCH_REQUIRE(buffer->get_type() == "buffer");
     173             : 
     174           1 :         advgetopt::options_environment opt_env;
     175           1 :         opt_env.f_project_name = "test-logger";
     176           2 :         advgetopt::getopt opts(opt_env);
     177           1 :         buffer->set_config(opts);
     178             : 
     179           2 :         snaplogger::format::pointer_t f(std::make_shared<snaplogger::format>("${message}"));
     180           1 :         buffer->set_format(f);
     181             : 
     182           1 :         l->add_appender(buffer);
     183             : 
     184           1 :         int const line_number = __LINE__;
     185             :         snaplogger::message::pointer_t msg(std::make_shared<snaplogger::message>
     186           2 :                         (::snaplogger::severity_t::SEVERITY_ERROR, __FILE__, __func__, line_number));
     187             : 
     188           1 :         CATCH_REQUIRE(msg->get_filename() == __FILE__);
     189           1 :         CATCH_REQUIRE(msg->get_function() == __func__);
     190           1 :         CATCH_REQUIRE(msg->get_line() == line_number);
     191             : 
     192           1 :         msg->set_filename("logger.cpp");
     193           1 :         msg->set_function("magical");
     194           1 :         msg->set_line(701);
     195             : 
     196           1 :         CATCH_REQUIRE(msg->get_filename() == "logger.cpp");
     197           1 :         CATCH_REQUIRE(msg->get_function() == "magical");
     198           1 :         CATCH_REQUIRE(msg->get_line() == 701);
     199             : 
     200           1 :         *msg << "Logging an error.";
     201             : 
     202           1 :         CATCH_REQUIRE(msg->str() == "Logging an error.");
     203             : 
     204           2 :         snaplogger::message::pointer_t copy(std::make_shared<snaplogger::message>(*msg, *msg));
     205             : 
     206           1 :         CATCH_REQUIRE(msg->str() == "Logging an error.");
     207           1 :         CATCH_REQUIRE(copy->str() == "Logging an error.");
     208             : 
     209             :         // no destructor called, the output is still empty
     210             :         //
     211           1 :         CATCH_REQUIRE(buffer->empty());
     212             : 
     213           1 :         copy.reset();
     214             : 
     215             :         // msg not lost
     216             :         //
     217           1 :         CATCH_REQUIRE(msg->str() == "Logging an error.");
     218             : 
     219             :         // destructor against copy does not trigger send_message()
     220             :         //
     221           1 :         CATCH_REQUIRE(buffer->empty());
     222             : 
     223           1 :         snaplogger::send_message(*msg);
     224             : 
     225             :         // now we get the message as expected!
     226             :         //
     227             :         // (note that internally we can skip receiving the message on the
     228             :         // original, but not as a client... we may want to have the ability
     229             :         // to cancel a message, though.)
     230             :         //
     231           1 :         CATCH_REQUIRE(buffer->str() == "Logging an error.\n");
     232             : 
     233           1 :         msg.reset();
     234             : 
     235           1 :         CATCH_REQUIRE(buffer->str() == "Logging an error.\n");
     236             : 
     237           1 :         l->reset();
     238             :     }
     239             :     CATCH_END_SECTION()
     240           1 : }
     241             : 
     242             : 
     243           4 : CATCH_TEST_CASE("message_severity", "[message][severity]")
     244             : {
     245           4 :     CATCH_START_SECTION("Appender vs Message severity")
     246             :     {
     247           1 :         snaplogger::set_diagnostic(snaplogger::DIAG_KEY_PROGNAME, "message-severity");
     248             : 
     249           2 :         snaplogger::logger::pointer_t l(snaplogger::logger::get_instance());
     250           2 :         snaplogger::buffer_appender::pointer_t buffer(std::make_shared<snaplogger::buffer_appender>("test-buffer"));
     251             : 
     252           1 :         advgetopt::options_environment opt_env;
     253           1 :         opt_env.f_project_name = "test-logger";
     254           2 :         advgetopt::getopt opts(opt_env);
     255           1 :         buffer->set_config(opts);
     256             : 
     257           2 :         snaplogger::format::pointer_t f(std::make_shared<snaplogger::format>("${message}"));
     258           1 :         buffer->set_format(f);
     259             : 
     260           1 :         l->add_appender(buffer);
     261             : 
     262           1 :         int const min_severity(static_cast<int>(snaplogger::severity_t::SEVERITY_MIN));
     263           1 :         int const max_severity(static_cast<int>(snaplogger::severity_t::SEVERITY_MAX));
     264         257 :         for(int i(min_severity); i <= max_severity; ++i)
     265             :         {
     266         256 :             buffer->set_severity(static_cast<snaplogger::severity_t>(i));
     267       65792 :             for(int j(min_severity); j <= max_severity; ++j)
     268             :             {
     269       65536 :                 snaplogger::send_message(
     270      131072 :                     ::snaplogger::message(
     271             :                               static_cast<::snaplogger::severity_t>(j)
     272             :                             , __FILE__
     273             :                             , __func__
     274             :                             , __LINE__
     275       65536 :                         ) << "The message itself");
     276             : 
     277       65536 :                 if(j >= i
     278       32896 :                 && i != static_cast<int>(snaplogger::severity_t::SEVERITY_OFF)
     279       32895 :                 && j != static_cast<int>(snaplogger::severity_t::SEVERITY_OFF))
     280             :                 {
     281       32640 :                     CATCH_REQUIRE(buffer->str() == "The message itself\n");
     282             :                 }
     283             :                 else
     284             :                 {
     285       32896 :                     CATCH_REQUIRE(buffer->empty());
     286             :                 }
     287       65536 :                 buffer->clear();
     288             :             }
     289             :         }
     290             : 
     291           1 :         l->reset();
     292             :     }
     293             :     CATCH_END_SECTION()
     294             : 
     295           4 :     CATCH_START_SECTION("Changing message severity (takes about 3.5min)")
     296             :     {
     297           1 :         snaplogger::set_diagnostic(snaplogger::DIAG_KEY_PROGNAME, "message-copying");
     298             : 
     299           2 :         snaplogger::logger::pointer_t l(snaplogger::logger::get_instance());
     300           2 :         snaplogger::buffer_appender::pointer_t buffer(std::make_shared<snaplogger::buffer_appender>("test-buffer"));
     301             : 
     302           1 :         advgetopt::options_environment opt_env;
     303           1 :         opt_env.f_project_name = "test-logger";
     304           2 :         advgetopt::getopt opts(opt_env);
     305           1 :         buffer->set_config(opts);
     306             : 
     307           2 :         snaplogger::format::pointer_t f(std::make_shared<snaplogger::format>("${message}"));
     308           1 :         buffer->set_format(f);
     309             : 
     310           1 :         l->add_appender(buffer);
     311             : 
     312           1 :         int const min_severity(static_cast<int>(snaplogger::severity_t::SEVERITY_MIN));
     313           1 :         int const max_severity(static_cast<int>(snaplogger::severity_t::SEVERITY_MAX));
     314          29 :         for(int i(min_severity); i <= max_severity; i += 1 + (rand() & 15))
     315             :         {
     316          28 :             buffer->set_severity(static_cast<snaplogger::severity_t>(i));
     317         882 :             for(int j(min_severity); j <= max_severity; j += 1 + (rand() & 15))
     318             :             {
     319       27001 :                 for(int k(min_severity); k <= max_severity; k += 1 + (rand() & 15))
     320             :                 {
     321             :                     ::snaplogger::message::pointer_t msg(std::make_shared<::snaplogger::message>(
     322       52294 :                               static_cast<::snaplogger::severity_t>(j)
     323             :                             , __FILE__
     324             :                             , __func__
     325             :                             , __LINE__
     326      104588 :                         ));
     327       26147 :                     *msg << "Start of message";
     328       26147 :                     msg->set_severity(static_cast<::snaplogger::severity_t>(k));
     329       26147 :                     *msg << " -- end of message";
     330       26147 :                     snaplogger::send_message(*msg);
     331             : //std::cerr << "checking with " << i << ", " << j << ", " << k << "\n";
     332             : 
     333       26147 :                     if(j >= i
     334       12292 :                     && k >= i
     335        8484 :                     && i != static_cast<int>(snaplogger::severity_t::SEVERITY_OFF)
     336        8484 :                     && j != static_cast<int>(snaplogger::severity_t::SEVERITY_OFF))
     337             :                     //&& k != static_cast<int>(snaplogger::severity_t::SEVERITY_OFF))
     338             :                     {
     339        8477 :                         if(j >= i)
     340             :                         {
     341        8477 :                             CATCH_REQUIRE(buffer->str() == "Start of message -- end of message\n");
     342             :                         }
     343             :                         else
     344             :                         {
     345           0 :                             CATCH_REQUIRE(buffer->str() == "Start of message\n");
     346        8477 :                         }
     347             :                     }
     348             :                     else
     349             :                     {
     350       17670 :                         CATCH_REQUIRE(buffer->empty());
     351             :                     }
     352       26147 :                     buffer->clear();
     353             :                 }
     354             :             }
     355             :         }
     356             : 
     357           1 :         l->reset();
     358             :     }
     359             :     CATCH_END_SECTION()
     360           2 : }
     361             : 
     362             : 
     363           6 : CATCH_TEST_CASE("message_format", "[message][format]")
     364             : {
     365           8 :     CATCH_START_SECTION("Message is Recursive")
     366             :     {
     367           1 :         snaplogger::set_diagnostic(snaplogger::DIAG_KEY_PROGNAME, "basic-format");
     368             : 
     369             :         // these two are not called in this test
     370             :         //
     371           1 :         snaplogger::set_diagnostic(snaplogger::DIAG_KEY_PROJECT_NAME, "test-logger");
     372           1 :         snaplogger::set_diagnostic(snaplogger::DIAG_KEY_VERSION, "5.32.1024");
     373             : 
     374           2 :         snaplogger::logger::pointer_t l(snaplogger::logger::get_instance());
     375           2 :         snaplogger::buffer_appender::pointer_t buffer(std::make_shared<snaplogger::buffer_appender>("test-buffer"));
     376             : 
     377           1 :         advgetopt::options_environment opt_env;
     378           1 :         opt_env.f_project_name = "test-logger";
     379           1 :         opt_env.f_version = "5.32.1024";
     380           2 :         advgetopt::getopt opts(opt_env);
     381           1 :         buffer->set_config(opts);
     382             : 
     383           2 :         snaplogger::format::pointer_t f(std::make_shared<snaplogger::format>("${project_name} ${message} v${version}"));
     384           1 :         buffer->set_format(f);
     385             : 
     386           1 :         l->add_appender(buffer);
     387             : 
     388           2 :         SNAP_LOG_WARNING
     389           1 :             << "Message Project Name = ${project_name} and Version = ${version} -- uses \"recursive\""
     390             :             << SNAP_LOG_SEND;
     391             : 
     392           1 :         CATCH_REQUIRE(buffer->str() ==
     393             :                 "test-logger Message Project Name = test-logger and"
     394             :                 " Version = 5.32.1024 -- uses \"recursive\" v5.32.1024"
     395             :                 "\n");
     396             : 
     397           1 :         l->reset();
     398             :     }
     399             :     CATCH_END_SECTION()
     400             : 
     401           8 :     CATCH_START_SECTION("${message} itself is not recursive")
     402             :     {
     403           1 :         snaplogger::set_diagnostic(snaplogger::DIAG_KEY_PROGNAME, "prevent-infinite-loop");
     404             : 
     405             :         // these two are not called in this test
     406             :         //
     407           1 :         snaplogger::set_diagnostic(snaplogger::DIAG_KEY_PROJECT_NAME, "test-logger");
     408           1 :         snaplogger::set_diagnostic(snaplogger::DIAG_KEY_VERSION, "5.32.1024");
     409             : 
     410           2 :         snaplogger::logger::pointer_t l(snaplogger::logger::get_instance());
     411           2 :         snaplogger::buffer_appender::pointer_t buffer(std::make_shared<snaplogger::buffer_appender>("test-buffer"));
     412             : 
     413           1 :         advgetopt::options_environment opt_env;
     414           1 :         opt_env.f_project_name = "test-logger";
     415           1 :         opt_env.f_version = "5.32.1024";
     416           2 :         advgetopt::getopt opts(opt_env);
     417           1 :         buffer->set_config(opts);
     418             : 
     419           2 :         snaplogger::format::pointer_t f(std::make_shared<snaplogger::format>("${project_name} ${message} v${version}"));
     420           1 :         buffer->set_format(f);
     421             : 
     422           1 :         l->add_appender(buffer);
     423             : 
     424           2 :         SNAP_LOG_WARNING
     425           1 :             << "Message ${message} says: Project Name = ${project_name} and Version = ${version} -- uses \"recursive\""
     426             :             << SNAP_LOG_SEND;
     427             : 
     428           1 :         CATCH_REQUIRE(buffer->str() ==
     429             :                 "test-logger Message  says: Project Name = test-logger and"
     430             :                 " Version = 5.32.1024 -- uses \"recursive\" v5.32.1024"
     431             :                 "\n");
     432             : 
     433           1 :         buffer->clear();
     434             : 
     435           1 :         snaplogger::unset_diagnostic(snaplogger::DIAG_KEY_VERSION);
     436             : 
     437           2 :         SNAP_LOG_WARNING
     438           1 :             << "Removed the version: ${message} says: Project Name = ${project_name} and Version = ${version} -- uses \"recursive\""
     439             :             << SNAP_LOG_SEND;
     440             : 
     441           1 :         CATCH_REQUIRE(buffer->str() ==
     442             :                 "test-logger Removed the version:  says: Project Name = test-logger and"
     443             :                 " Version =  -- uses \"recursive\" v"
     444             :                 "\n");
     445             : 
     446           1 :         l->reset();
     447             :     }
     448             :     CATCH_END_SECTION()
     449             : 
     450           8 :     CATCH_START_SECTION("${pid} uses the get_environment() function")
     451             :     {
     452           1 :         snaplogger::set_diagnostic(snaplogger::DIAG_KEY_PROGNAME, "get-environment");
     453             : 
     454           2 :         snaplogger::logger::pointer_t l(snaplogger::logger::get_instance());
     455           2 :         snaplogger::buffer_appender::pointer_t buffer(std::make_shared<snaplogger::buffer_appender>("test-buffer"));
     456             : 
     457           1 :         advgetopt::options_environment opt_env;
     458           2 :         advgetopt::getopt opts(opt_env);
     459           1 :         buffer->set_config(opts);
     460             : 
     461           2 :         snaplogger::format::pointer_t f(std::make_shared<snaplogger::format>("${message}"));
     462           1 :         buffer->set_format(f);
     463             : 
     464           1 :         l->add_appender(buffer);
     465             : 
     466           2 :         SNAP_LOG_WARNING
     467           1 :             << "Test PID = ${pid} == ${pid:running}"
     468             :             << SNAP_LOG_SEND;
     469             : 
     470           1 :         CATCH_REQUIRE(buffer->str() ==
     471             :                   "Test PID = " + std::to_string(getpid())
     472             :                 + " == " + std::to_string(getpid())
     473             :                 + "\n");
     474             : 
     475           1 :         l->reset();
     476             :     }
     477             :     CATCH_END_SECTION()
     478             : 
     479           8 :     CATCH_START_SECTION("Verify year")
     480             :     {
     481           1 :         snaplogger::set_diagnostic(snaplogger::DIAG_KEY_PROGNAME, "get-environment");
     482             : 
     483           2 :         snaplogger::logger::pointer_t l(snaplogger::logger::get_instance());
     484           1 :         CATCH_REQUIRE(l->get_appender("test-buffer") == nullptr);
     485             : 
     486           2 :         snaplogger::buffer_appender::pointer_t buffer(std::make_shared<snaplogger::buffer_appender>("test-buffer"));
     487             : 
     488           1 :         advgetopt::options_environment opt_env;
     489           2 :         advgetopt::getopt opts(opt_env);
     490           1 :         buffer->set_config(opts);
     491             : 
     492           2 :         snaplogger::format::pointer_t f(std::make_shared<snaplogger::format>("${message}"));
     493           1 :         buffer->set_format(f);
     494             : 
     495           1 :         l->add_appender(buffer);
     496             : 
     497           1 :         CATCH_REQUIRE(l->get_appender("test-buffer") == buffer);
     498             : 
     499             :         // we create a message so we can check the timestamp in our test
     500             :         //
     501             :         snaplogger::message::pointer_t msg(std::make_shared<snaplogger::message>
     502           2 :                         (::snaplogger::severity_t::SEVERITY_ERROR, __FILE__, __func__, __LINE__));
     503           1 :         *msg << "Created message on YYYY = ${date:year}, MM = ${date:month}, DD = ${date:day}";
     504             : 
     505           1 :         timespec const stamp(msg->get_timestamp());
     506             : 
     507           1 :         snaplogger::send_message(*msg);
     508             : 
     509             :         tm t;
     510           1 :         gmtime_r(&stamp.tv_sec, &t);
     511             :         char year[16];
     512             :         char month[16];
     513             :         char day[16];
     514           1 :         strftime(year,  16, "%Y", &t);
     515           1 :         strftime(month, 16, "%m", &t);
     516           1 :         strftime(day,   16, "%d", &t);
     517             : 
     518           1 :         CATCH_REQUIRE(buffer->str() ==
     519             :                   std::string("Created message on YYYY = ")
     520             :                 + year
     521             :                 + ", MM = "
     522             :                 + std::to_string(std::atoi(month))  // remove the leading '0' if necessary
     523             :                 + ", DD = "
     524             :                 + std::to_string(std::atoi(day))    // remove the leading '0' if necessary
     525             :                 + "\n");
     526             : 
     527           1 :         l->reset();
     528             :     }
     529             :     CATCH_END_SECTION()
     530           4 : }
     531             : 
     532             : 
     533           3 : CATCH_TEST_CASE("message_component_filter", "[message][component]")
     534             : {
     535           2 :     CATCH_START_SECTION("Filter Message with Component")
     536             :     {
     537           1 :         snaplogger::set_diagnostic(snaplogger::DIAG_KEY_PROGNAME, "component-filter");
     538             : 
     539           2 :         snaplogger::logger::pointer_t l(snaplogger::logger::get_instance());
     540           2 :         snaplogger::buffer_appender::pointer_t buffer(std::make_shared<snaplogger::buffer_appender>("test-buffer"));
     541             : 
     542           1 :         advgetopt::options_environment opt_env;
     543           2 :         advgetopt::getopt opts(opt_env);
     544           1 :         buffer->set_config(opts);
     545             : 
     546           2 :         snaplogger::format::pointer_t f(std::make_shared<snaplogger::format>("${message} (${severity})"));
     547           1 :         buffer->set_format(f);
     548             : 
     549           1 :         l->add_appender(buffer);
     550             : 
     551           2 :         SNAP_LOG_WARNING
     552           1 :             << snaplogger::secure       // mark as a secure message
     553             :             << "This message is secure but not the buffer"
     554             :             << SNAP_LOG_SEND;
     555             : 
     556           1 :         CATCH_REQUIRE(buffer->empty());
     557             : 
     558           2 :         SNAP_LOG_WARNING
     559           1 :             << "Test number: "
     560             :             << 2
     561           1 :             << " with buffer still unsecure..."
     562           1 :             << SNAP_LOG_SEND_SECURELY;  // mark at the end
     563             : 
     564           1 :         CATCH_REQUIRE(buffer->empty());
     565             : 
     566             :         // mark the buffer as a secure buffer now
     567             :         //
     568           1 :         buffer->add_component(snaplogger::g_secure_component);
     569             : 
     570           2 :         SNAP_LOG_WARNING
     571           1 :             << snaplogger::secure       // mark as a secure message
     572             :             << "This message is secure and so is the buffer"
     573             :             << SNAP_LOG_SEND;
     574             : 
     575           1 :         CATCH_REQUIRE(buffer->str() == "This message is secure and so is the buffer (warning)\n");
     576             : 
     577           1 :         buffer->clear();
     578             : 
     579           2 :         SNAP_LOG_WARNING
     580           1 :             << "Test number: "
     581             :             << 4
     582           1 :             << " with secure buffer...\r\n"
     583           1 :             << SNAP_LOG_SEND_SECURELY;  // mark at the end
     584             : 
     585           1 :         CATCH_REQUIRE(buffer->str() == "Test number: 4 with secure buffer... (warning)\n");
     586             : 
     587           1 :         l->reset();
     588             :     }
     589             :     CATCH_END_SECTION()
     590           7 : }
     591             : 
     592             : 
     593             : 
     594             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.13