LCOV - code coverage report
Current view: top level - tests - message.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 270 271 99.6 %
Date: 2021-06-01 17:16:42 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           2 :         std::string const expected(std::regex_replace(buffer->str(), std::regex("\\\\\"id\\\\\":\\\\\"[0-9]+\\\\\","), ""));
     155           1 :         CATCH_REQUIRE(expected == "{\"version\":1,\"message\":\"See what happens with a \\\"quoted string\\\" within the message ({\\\"format\\\":\\\"json\\\",\\\"language\\\":\\\"js\\\"})\"}\n");
     156           1 :         buffer->clear();
     157             : 
     158           1 :         l->reset();
     159             :     }
     160             :     CATCH_END_SECTION()
     161           2 : }
     162             : 
     163             : 
     164           3 : CATCH_TEST_CASE("message_copy", "[message]")
     165             : {
     166           2 :     CATCH_START_SECTION("Copy Message")
     167             :     {
     168           1 :         snaplogger::set_diagnostic(snaplogger::DIAG_KEY_PROGNAME, "message-copying");
     169             : 
     170           2 :         snaplogger::logger::pointer_t l(snaplogger::logger::get_instance());
     171           2 :         snaplogger::buffer_appender::pointer_t buffer(std::make_shared<snaplogger::buffer_appender>("test-buffer"));
     172             : 
     173           1 :         CATCH_REQUIRE(buffer->get_type() == "buffer");
     174             : 
     175           1 :         advgetopt::options_environment opt_env;
     176           1 :         opt_env.f_project_name = "test-logger";
     177           2 :         advgetopt::getopt opts(opt_env);
     178           1 :         buffer->set_config(opts);
     179             : 
     180           2 :         snaplogger::format::pointer_t f(std::make_shared<snaplogger::format>("${message}"));
     181           1 :         buffer->set_format(f);
     182             : 
     183           1 :         l->add_appender(buffer);
     184             : 
     185           1 :         int const line_number = __LINE__;
     186             :         snaplogger::message::pointer_t msg(std::make_shared<snaplogger::message>
     187           2 :                         (::snaplogger::severity_t::SEVERITY_ERROR, __FILE__, __func__, line_number));
     188             : 
     189           1 :         CATCH_REQUIRE(msg->get_filename() == __FILE__);
     190           1 :         CATCH_REQUIRE(msg->get_function() == __func__);
     191           1 :         CATCH_REQUIRE(msg->get_line() == line_number);
     192             : 
     193           1 :         msg->set_filename("logger.cpp");
     194           1 :         msg->set_function("magical");
     195           1 :         msg->set_line(701);
     196             : 
     197           1 :         CATCH_REQUIRE(msg->get_filename() == "logger.cpp");
     198           1 :         CATCH_REQUIRE(msg->get_function() == "magical");
     199           1 :         CATCH_REQUIRE(msg->get_line() == 701);
     200             : 
     201           1 :         *msg << "Logging an error.";
     202             : 
     203           1 :         CATCH_REQUIRE(msg->str() == "Logging an error.");
     204             : 
     205           2 :         snaplogger::message::pointer_t copy(std::make_shared<snaplogger::message>(*msg, *msg));
     206             : 
     207           1 :         CATCH_REQUIRE(msg->str() == "Logging an error.");
     208           1 :         CATCH_REQUIRE(copy->str() == "Logging an error.");
     209             : 
     210             :         // no destructor called, the output is still empty
     211             :         //
     212           1 :         CATCH_REQUIRE(buffer->empty());
     213             : 
     214           1 :         copy.reset();
     215             : 
     216             :         // msg not lost
     217             :         //
     218           1 :         CATCH_REQUIRE(msg->str() == "Logging an error.");
     219             : 
     220             :         // destructor against copy does not trigger send_message()
     221             :         //
     222           1 :         CATCH_REQUIRE(buffer->empty());
     223             : 
     224           1 :         snaplogger::send_message(*msg);
     225             : 
     226             :         // now we get the message as expected!
     227             :         //
     228             :         // (note that internally we can skip receiving the message on the
     229             :         // original, but not as a client... we may want to have the ability
     230             :         // to cancel a message, though.)
     231             :         //
     232           1 :         CATCH_REQUIRE(buffer->str() == "Logging an error.\n");
     233             : 
     234           1 :         msg.reset();
     235             : 
     236           1 :         CATCH_REQUIRE(buffer->str() == "Logging an error.\n");
     237             : 
     238           1 :         l->reset();
     239             :     }
     240             :     CATCH_END_SECTION()
     241           1 : }
     242             : 
     243             : 
     244           4 : CATCH_TEST_CASE("message_severity", "[message][severity]")
     245             : {
     246           4 :     CATCH_START_SECTION("Appender vs Message severity")
     247             :     {
     248           1 :         snaplogger::set_diagnostic(snaplogger::DIAG_KEY_PROGNAME, "message-severity");
     249             : 
     250           2 :         snaplogger::logger::pointer_t l(snaplogger::logger::get_instance());
     251           2 :         snaplogger::buffer_appender::pointer_t buffer(std::make_shared<snaplogger::buffer_appender>("test-buffer"));
     252             : 
     253           1 :         advgetopt::options_environment opt_env;
     254           1 :         opt_env.f_project_name = "test-logger";
     255           2 :         advgetopt::getopt opts(opt_env);
     256           1 :         buffer->set_config(opts);
     257             : 
     258           2 :         snaplogger::format::pointer_t f(std::make_shared<snaplogger::format>("${message}"));
     259           1 :         buffer->set_format(f);
     260             : 
     261           1 :         l->add_appender(buffer);
     262             : 
     263           1 :         int const min_severity(static_cast<int>(snaplogger::severity_t::SEVERITY_MIN));
     264           1 :         int const max_severity(static_cast<int>(snaplogger::severity_t::SEVERITY_MAX));
     265         257 :         for(int i(min_severity); i <= max_severity; ++i)
     266             :         {
     267         256 :             buffer->set_severity(static_cast<snaplogger::severity_t>(i));
     268       65792 :             for(int j(min_severity); j <= max_severity; ++j)
     269             :             {
     270       65536 :                 snaplogger::send_message(
     271      131072 :                     ::snaplogger::message(
     272             :                               static_cast<::snaplogger::severity_t>(j)
     273             :                             , __FILE__
     274             :                             , __func__
     275             :                             , __LINE__
     276       65536 :                         ) << "The message itself");
     277             : 
     278       65536 :                 if(j >= i
     279       32896 :                 && i != static_cast<int>(snaplogger::severity_t::SEVERITY_OFF)
     280       32895 :                 && j != static_cast<int>(snaplogger::severity_t::SEVERITY_OFF))
     281             :                 {
     282       32640 :                     CATCH_REQUIRE(buffer->str() == "The message itself\n");
     283             :                 }
     284             :                 else
     285             :                 {
     286       32896 :                     CATCH_REQUIRE(buffer->empty());
     287             :                 }
     288       65536 :                 buffer->clear();
     289             :             }
     290             :         }
     291             : 
     292           1 :         l->reset();
     293             :     }
     294             :     CATCH_END_SECTION()
     295             : 
     296           4 :     CATCH_START_SECTION("Changing message severity (takes about 3.5min)")
     297             :     {
     298           1 :         snaplogger::set_diagnostic(snaplogger::DIAG_KEY_PROGNAME, "message-copying");
     299             : 
     300           2 :         snaplogger::logger::pointer_t l(snaplogger::logger::get_instance());
     301           2 :         snaplogger::buffer_appender::pointer_t buffer(std::make_shared<snaplogger::buffer_appender>("test-buffer"));
     302             : 
     303           1 :         advgetopt::options_environment opt_env;
     304           1 :         opt_env.f_project_name = "test-logger";
     305           2 :         advgetopt::getopt opts(opt_env);
     306           1 :         buffer->set_config(opts);
     307             : 
     308           2 :         snaplogger::format::pointer_t f(std::make_shared<snaplogger::format>("${message}"));
     309           1 :         buffer->set_format(f);
     310             : 
     311           1 :         l->add_appender(buffer);
     312             : 
     313           1 :         int const min_severity(static_cast<int>(snaplogger::severity_t::SEVERITY_MIN));
     314           1 :         int const max_severity(static_cast<int>(snaplogger::severity_t::SEVERITY_MAX));
     315          31 :         for(int i(min_severity); i <= max_severity; i += 1 + (rand() & 15))
     316             :         {
     317          30 :             buffer->set_severity(static_cast<snaplogger::severity_t>(i));
     318         940 :             for(int j(min_severity); j <= max_severity; j += 1 + (rand() & 15))
     319             :             {
     320       28884 :                 for(int k(min_severity); k <= max_severity; k += 1 + (rand() & 15))
     321             :                 {
     322             :                     ::snaplogger::message::pointer_t msg(std::make_shared<::snaplogger::message>(
     323       55948 :                               static_cast<::snaplogger::severity_t>(j)
     324             :                             , __FILE__
     325             :                             , __func__
     326             :                             , __LINE__
     327      111896 :                         ));
     328       27974 :                     *msg << "Start of message";
     329       27974 :                     msg->set_severity(static_cast<::snaplogger::severity_t>(k));
     330       27974 :                     *msg << " -- end of message";
     331       27974 :                     snaplogger::send_message(*msg);
     332             : //std::cerr << "checking with " << i << ", " << j << ", " << k << "\n";
     333             : 
     334       27974 :                     if(j >= i
     335       14266 :                     && k >= i
     336        9634 :                     && i != static_cast<int>(snaplogger::severity_t::SEVERITY_OFF)
     337        9634 :                     && j != static_cast<int>(snaplogger::severity_t::SEVERITY_OFF))
     338             :                     //&& k != static_cast<int>(snaplogger::severity_t::SEVERITY_OFF))
     339             :                     {
     340        9533 :                         if(j >= i)
     341             :                         {
     342        9533 :                             CATCH_REQUIRE(buffer->str() == "Start of message -- end of message\n");
     343             :                         }
     344             :                         else
     345             :                         {
     346           0 :                             CATCH_REQUIRE(buffer->str() == "Start of message\n");
     347        9533 :                         }
     348             :                     }
     349             :                     else
     350             :                     {
     351       18441 :                         CATCH_REQUIRE(buffer->empty());
     352             :                     }
     353       27974 :                     buffer->clear();
     354             :                 }
     355             :             }
     356             :         }
     357             : 
     358           1 :         l->reset();
     359             :     }
     360             :     CATCH_END_SECTION()
     361           2 : }
     362             : 
     363             : 
     364           6 : CATCH_TEST_CASE("message_format", "[message][format]")
     365             : {
     366           8 :     CATCH_START_SECTION("Message is Recursive")
     367             :     {
     368           1 :         snaplogger::set_diagnostic(snaplogger::DIAG_KEY_PROGNAME, "basic-format");
     369             : 
     370             :         // these two are not called in this test
     371             :         //
     372           1 :         snaplogger::set_diagnostic(snaplogger::DIAG_KEY_PROJECT_NAME, "test-logger");
     373           1 :         snaplogger::set_diagnostic(snaplogger::DIAG_KEY_VERSION, "5.32.1024");
     374             : 
     375           2 :         snaplogger::logger::pointer_t l(snaplogger::logger::get_instance());
     376           2 :         snaplogger::buffer_appender::pointer_t buffer(std::make_shared<snaplogger::buffer_appender>("test-buffer"));
     377             : 
     378           1 :         advgetopt::options_environment opt_env;
     379           1 :         opt_env.f_project_name = "test-logger";
     380           1 :         opt_env.f_version = "5.32.1024";
     381           2 :         advgetopt::getopt opts(opt_env);
     382           1 :         buffer->set_config(opts);
     383             : 
     384           2 :         snaplogger::format::pointer_t f(std::make_shared<snaplogger::format>("${project_name} ${message} v${version}"));
     385           1 :         buffer->set_format(f);
     386             : 
     387           1 :         l->add_appender(buffer);
     388             : 
     389           2 :         SNAP_LOG_WARNING
     390           1 :             << "Message Project Name = ${project_name} and Version = ${version} -- uses \"recursive\""
     391             :             << SNAP_LOG_SEND;
     392             : 
     393           1 :         CATCH_REQUIRE(buffer->str() ==
     394             :                 "test-logger Message Project Name = test-logger and"
     395             :                 " Version = 5.32.1024 -- uses \"recursive\" v5.32.1024"
     396             :                 "\n");
     397             : 
     398           1 :         l->reset();
     399             :     }
     400             :     CATCH_END_SECTION()
     401             : 
     402           8 :     CATCH_START_SECTION("${message} itself is not recursive")
     403             :     {
     404           1 :         snaplogger::set_diagnostic(snaplogger::DIAG_KEY_PROGNAME, "prevent-infinite-loop");
     405             : 
     406             :         // these two are not called in this test
     407             :         //
     408           1 :         snaplogger::set_diagnostic(snaplogger::DIAG_KEY_PROJECT_NAME, "test-logger");
     409           1 :         snaplogger::set_diagnostic(snaplogger::DIAG_KEY_VERSION, "5.32.1024");
     410             : 
     411           2 :         snaplogger::logger::pointer_t l(snaplogger::logger::get_instance());
     412           2 :         snaplogger::buffer_appender::pointer_t buffer(std::make_shared<snaplogger::buffer_appender>("test-buffer"));
     413             : 
     414           1 :         advgetopt::options_environment opt_env;
     415           1 :         opt_env.f_project_name = "test-logger";
     416           1 :         opt_env.f_version = "5.32.1024";
     417           2 :         advgetopt::getopt opts(opt_env);
     418           1 :         buffer->set_config(opts);
     419             : 
     420           2 :         snaplogger::format::pointer_t f(std::make_shared<snaplogger::format>("${project_name} ${message} v${version}"));
     421           1 :         buffer->set_format(f);
     422             : 
     423           1 :         l->add_appender(buffer);
     424             : 
     425           2 :         SNAP_LOG_WARNING
     426           1 :             << "Message ${message} says: Project Name = ${project_name} and Version = ${version} -- uses \"recursive\""
     427             :             << SNAP_LOG_SEND;
     428             : 
     429           1 :         CATCH_REQUIRE(buffer->str() ==
     430             :                 "test-logger Message  says: Project Name = test-logger and"
     431             :                 " Version = 5.32.1024 -- uses \"recursive\" v5.32.1024"
     432             :                 "\n");
     433             : 
     434           1 :         buffer->clear();
     435             : 
     436           1 :         snaplogger::unset_diagnostic(snaplogger::DIAG_KEY_VERSION);
     437             : 
     438           2 :         SNAP_LOG_WARNING
     439           1 :             << "Removed the version: ${message} says: Project Name = ${project_name} and Version = ${version} -- uses \"recursive\""
     440             :             << SNAP_LOG_SEND;
     441             : 
     442           1 :         CATCH_REQUIRE(buffer->str() ==
     443             :                 "test-logger Removed the version:  says: Project Name = test-logger and"
     444             :                 " Version =  -- uses \"recursive\" v"
     445             :                 "\n");
     446             : 
     447           1 :         l->reset();
     448             :     }
     449             :     CATCH_END_SECTION()
     450             : 
     451           8 :     CATCH_START_SECTION("${pid} uses the get_environment() function")
     452             :     {
     453           1 :         snaplogger::set_diagnostic(snaplogger::DIAG_KEY_PROGNAME, "get-environment");
     454             : 
     455           2 :         snaplogger::logger::pointer_t l(snaplogger::logger::get_instance());
     456           2 :         snaplogger::buffer_appender::pointer_t buffer(std::make_shared<snaplogger::buffer_appender>("test-buffer"));
     457             : 
     458           1 :         advgetopt::options_environment opt_env;
     459           2 :         advgetopt::getopt opts(opt_env);
     460           1 :         buffer->set_config(opts);
     461             : 
     462           2 :         snaplogger::format::pointer_t f(std::make_shared<snaplogger::format>("${message}"));
     463           1 :         buffer->set_format(f);
     464             : 
     465           1 :         l->add_appender(buffer);
     466             : 
     467           2 :         SNAP_LOG_WARNING
     468           1 :             << "Test PID = ${pid} == ${pid:running}"
     469             :             << SNAP_LOG_SEND;
     470             : 
     471           1 :         CATCH_REQUIRE(buffer->str() ==
     472             :                   "Test PID = " + std::to_string(getpid())
     473             :                 + " == " + std::to_string(getpid())
     474             :                 + "\n");
     475             : 
     476           1 :         l->reset();
     477             :     }
     478             :     CATCH_END_SECTION()
     479             : 
     480           8 :     CATCH_START_SECTION("Verify year")
     481             :     {
     482           1 :         snaplogger::set_diagnostic(snaplogger::DIAG_KEY_PROGNAME, "get-environment");
     483             : 
     484           2 :         snaplogger::logger::pointer_t l(snaplogger::logger::get_instance());
     485           1 :         CATCH_REQUIRE(l->get_appender("test-buffer") == nullptr);
     486             : 
     487           2 :         snaplogger::buffer_appender::pointer_t buffer(std::make_shared<snaplogger::buffer_appender>("test-buffer"));
     488             : 
     489           1 :         advgetopt::options_environment opt_env;
     490           2 :         advgetopt::getopt opts(opt_env);
     491           1 :         buffer->set_config(opts);
     492             : 
     493           2 :         snaplogger::format::pointer_t f(std::make_shared<snaplogger::format>("${message}"));
     494           1 :         buffer->set_format(f);
     495             : 
     496           1 :         l->add_appender(buffer);
     497             : 
     498           1 :         CATCH_REQUIRE(l->get_appender("test-buffer") == buffer);
     499             : 
     500             :         // we create a message so we can check the timestamp in our test
     501             :         //
     502             :         snaplogger::message::pointer_t msg(std::make_shared<snaplogger::message>
     503           2 :                         (::snaplogger::severity_t::SEVERITY_ERROR, __FILE__, __func__, __LINE__));
     504           1 :         *msg << "Created message on YYYY = ${date:year}, MM = ${date:month}, DD = ${date:day}";
     505             : 
     506           1 :         timespec const stamp(msg->get_timestamp());
     507             : 
     508           1 :         snaplogger::send_message(*msg);
     509             : 
     510             :         tm t;
     511           1 :         gmtime_r(&stamp.tv_sec, &t);
     512             :         char year[16];
     513             :         char month[16];
     514             :         char day[16];
     515           1 :         strftime(year,  16, "%Y", &t);
     516           1 :         strftime(month, 16, "%m", &t);
     517           1 :         strftime(day,   16, "%d", &t);
     518             : 
     519           1 :         CATCH_REQUIRE(buffer->str() ==
     520             :                   std::string("Created message on YYYY = ")
     521             :                 + year
     522             :                 + ", MM = "
     523             :                 + std::to_string(std::atoi(month))  // remove the leading '0' if necessary
     524             :                 + ", DD = "
     525             :                 + std::to_string(std::atoi(day))    // remove the leading '0' if necessary
     526             :                 + "\n");
     527             : 
     528           1 :         l->reset();
     529             :     }
     530             :     CATCH_END_SECTION()
     531           4 : }
     532             : 
     533             : 
     534           3 : CATCH_TEST_CASE("message_component_filter", "[message][component]")
     535             : {
     536           2 :     CATCH_START_SECTION("Filter Message with Component")
     537             :     {
     538           1 :         snaplogger::set_diagnostic(snaplogger::DIAG_KEY_PROGNAME, "component-filter");
     539             : 
     540           2 :         snaplogger::logger::pointer_t l(snaplogger::logger::get_instance());
     541           2 :         snaplogger::buffer_appender::pointer_t buffer(std::make_shared<snaplogger::buffer_appender>("test-buffer"));
     542             : 
     543           1 :         advgetopt::options_environment opt_env;
     544           2 :         advgetopt::getopt opts(opt_env);
     545           1 :         buffer->set_config(opts);
     546             : 
     547           2 :         snaplogger::format::pointer_t f(std::make_shared<snaplogger::format>("${message} (${severity})"));
     548           1 :         buffer->set_format(f);
     549             : 
     550           1 :         l->add_appender(buffer);
     551             : 
     552           2 :         SNAP_LOG_WARNING
     553           1 :             << snaplogger::secure       // mark as a secure message
     554             :             << "This message is secure but not the buffer"
     555             :             << SNAP_LOG_SEND;
     556             : 
     557           1 :         CATCH_REQUIRE(buffer->empty());
     558             : 
     559           2 :         SNAP_LOG_WARNING
     560           1 :             << "Test number: "
     561             :             << 2
     562           1 :             << " with buffer still unsecure..."
     563           1 :             << SNAP_LOG_SEND_SECURELY;  // mark at the end
     564             : 
     565           1 :         CATCH_REQUIRE(buffer->empty());
     566             : 
     567             :         // mark the buffer as a secure buffer now
     568             :         //
     569           1 :         buffer->add_component(snaplogger::g_secure_component);
     570             : 
     571           2 :         SNAP_LOG_WARNING
     572           1 :             << snaplogger::secure       // mark as a secure message
     573             :             << "This message is secure and so is the buffer"
     574             :             << SNAP_LOG_SEND;
     575             : 
     576           1 :         CATCH_REQUIRE(buffer->str() == "This message is secure and so is the buffer (warning)\n");
     577             : 
     578           1 :         buffer->clear();
     579             : 
     580           2 :         SNAP_LOG_WARNING
     581           1 :             << "Test number: "
     582             :             << 4
     583           1 :             << " with secure buffer...\r\n"
     584           1 :             << SNAP_LOG_SEND_SECURELY;  // mark at the end
     585             : 
     586           1 :         CATCH_REQUIRE(buffer->str() == "Test number: 4 with secure buffer... (warning)\n");
     587             : 
     588           1 :         l->reset();
     589             :     }
     590             :     CATCH_END_SECTION()
     591           7 : }
     592             : 
     593             : 
     594             : 
     595             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.13