LCOV - code coverage report
Current view: top level - tests - message.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 89 89 100.0 %
Date: 2019-08-13 00:35:33 Functions: 5 5 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * License:
       3             :  *    Copyright (c) 2006-2019  Made to Order Software Corp.  All Rights Reserved
       4             :  *
       5             :  *    https://snapwebsites.org/
       6             :  *    contact@m2osw.com
       7             :  *
       8             :  *    This program is free software; you can redistribute it and/or modify
       9             :  *    it under the terms of the GNU General Public License as published by
      10             :  *    the Free Software Foundation; either version 2 of the License, or
      11             :  *    (at your option) any later version.
      12             :  *
      13             :  *    This program is distributed in the hope that it will be useful,
      14             :  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
      15             :  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      16             :  *    GNU General Public License for more details.
      17             :  *
      18             :  *    You should have received a copy of the GNU General Public License along
      19             :  *    with this program; if not, write to the Free Software Foundation, Inc.,
      20             :  *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
      21             :  *
      22             :  * Authors:
      23             :  *    Alexis Wilke   alexis@m2osw.com
      24             :  */
      25             : 
      26             : // self
      27             : //
      28             : #include    "main.h"
      29             : 
      30             : 
      31             : // snaplogger lib
      32             : //
      33             : #include    <snaplogger/buffer_appender.h>
      34             : #include    <snaplogger/format.h>
      35             : #include    <snaplogger/logger.h>
      36             : #include    <snaplogger/map_diagnostic.h>
      37             : #include    <snaplogger/message.h>
      38             : #include    <snaplogger/severity.h>
      39             : 
      40             : 
      41             : // C lib
      42             : //
      43             : #include    <unistd.h>
      44             : 
      45             : 
      46             : 
      47           3 : CATCH_TEST_CASE("message_capture", "[message]")
      48             : {
      49           2 :     CATCH_START_SECTION("Message Buffering")
      50             :     {
      51           1 :         snaplogger::set_diagnostic(snaplogger::DIAG_KEY_PROGNAME, "message-logging");
      52             : 
      53           2 :         snaplogger::logger::pointer_t l(snaplogger::logger::get_instance());
      54           2 :         snaplogger::buffer_appender::pointer_t buffer(std::make_shared<snaplogger::buffer_appender>("test-buffer"));
      55             : 
      56           1 :         CATCH_REQUIRE(buffer->get_type() == "buffer");
      57             : 
      58           1 :         advgetopt::options_environment opt_env;
      59           1 :         opt_env.f_project_name = "test-logger";
      60           2 :         advgetopt::getopt opts(opt_env);
      61           1 :         buffer->set_config(opts);
      62             : 
      63           2 :         snaplogger::format::pointer_t f(std::make_shared<snaplogger::format>("${message}"));
      64           1 :         buffer->set_format(f);
      65             : 
      66           1 :         l->add_appender(buffer);
      67             : 
      68           1 :         SNAP_LOG_ERROR << "Logging this error";
      69           1 :         CATCH_REQUIRE(buffer->str() == "Logging this error\n");
      70           1 :         buffer->str(std::string());
      71             : 
      72             :         // show that the "\n" does not get duplicated
      73             :         //
      74           1 :         SNAP_LOG_ERROR << "Error with newline\n";
      75           1 :         CATCH_REQUIRE(buffer->str() == "Error with newline\n");
      76           1 :         buffer->str(std::string());
      77             : 
      78             :         // show that the "\r\n" gets replaced by "\n"
      79             :         //
      80           1 :         SNAP_LOG_ERROR << "Error with CRLF\r\n";
      81           1 :         CATCH_REQUIRE(buffer->str() == "Error with CRLF\n");
      82           1 :         buffer->str(std::string());
      83             : 
      84             :         // severity too low, no change to buffer
      85             :         //
      86           1 :         SNAP_LOG_DEBUG << "Debug Message " << M_PI << " which does not make it at all...\n";
      87           1 :         CATCH_REQUIRE(buffer->str().empty());
      88             : 
      89           1 :         l->reset();
      90             :     }
      91             :     CATCH_END_SECTION()
      92           1 : }
      93             : 
      94             : 
      95           3 : CATCH_TEST_CASE("message_copy", "[message]")
      96             : {
      97           2 :     CATCH_START_SECTION("Copy Message")
      98             :     {
      99           1 :         snaplogger::set_diagnostic(snaplogger::DIAG_KEY_PROGNAME, "message-copying");
     100             : 
     101           2 :         snaplogger::logger::pointer_t l(snaplogger::logger::get_instance());
     102           2 :         snaplogger::buffer_appender::pointer_t buffer(std::make_shared<snaplogger::buffer_appender>("test-buffer"));
     103             : 
     104           1 :         CATCH_REQUIRE(buffer->get_type() == "buffer");
     105             : 
     106           1 :         advgetopt::options_environment opt_env;
     107           1 :         opt_env.f_project_name = "test-logger";
     108           2 :         advgetopt::getopt opts(opt_env);
     109           1 :         buffer->set_config(opts);
     110             : 
     111           2 :         snaplogger::format::pointer_t f(std::make_shared<snaplogger::format>("${message}"));
     112           1 :         buffer->set_format(f);
     113             : 
     114           1 :         l->add_appender(buffer);
     115             : 
     116           1 :         int const line_number = __LINE__;
     117             :         snaplogger::message::pointer_t msg(std::make_shared<snaplogger::message>
     118           2 :                         (::snaplogger::severity_t::SEVERITY_ERROR, __FILE__, __func__, line_number));
     119             : 
     120           1 :         CATCH_REQUIRE(msg->get_filename() == __FILE__);
     121           1 :         CATCH_REQUIRE(msg->get_function() == __func__);
     122           1 :         CATCH_REQUIRE(msg->get_line() == line_number);
     123             : 
     124           1 :         msg->set_filename("logger.cpp");
     125           1 :         msg->set_function("magical");
     126           1 :         msg->set_line(701);
     127             : 
     128           1 :         CATCH_REQUIRE(msg->get_filename() == "logger.cpp");
     129           1 :         CATCH_REQUIRE(msg->get_function() == "magical");
     130           1 :         CATCH_REQUIRE(msg->get_line() == 701);
     131             : 
     132           1 :         *msg << "Logging an error.";
     133             : 
     134           1 :         CATCH_REQUIRE(msg->str() == "Logging an error.");
     135             : 
     136           2 :         snaplogger::message::pointer_t copy(std::make_shared<snaplogger::message>(*msg, *msg));
     137             : 
     138           1 :         CATCH_REQUIRE(msg->str() == "Logging an error.");
     139           1 :         CATCH_REQUIRE(copy->str() == "Logging an error.");
     140             : 
     141             :         // no destructor called, the output is still empty
     142             :         //
     143           1 :         CATCH_REQUIRE(buffer->str().empty());
     144             : 
     145           1 :         copy.reset();
     146             : 
     147             :         // msg not lost
     148             :         //
     149           1 :         CATCH_REQUIRE(msg->str() == "Logging an error.");
     150             : 
     151             :         // destructor against copy does not trigger send_message()
     152             :         //
     153           1 :         CATCH_REQUIRE(buffer->str().empty());
     154             : 
     155           1 :         msg.reset();
     156             : 
     157             :         // now we get the message as expected!
     158             :         //
     159             :         // (note that internally we can skip receiving the message on the
     160             :         // original, but not as a client... we may want to have the ability
     161             :         // to cancel a message, though.)
     162             :         //
     163           1 :         CATCH_REQUIRE(buffer->str() == "Logging an error.\n");
     164             : 
     165           1 :         l->reset();
     166             :     }
     167             :     CATCH_END_SECTION()
     168           1 : }
     169             : 
     170             : 
     171           3 : CATCH_TEST_CASE("message_severity", "[message][severity]")
     172             : {
     173           2 :     CATCH_START_SECTION("Appender vs Message severity")
     174             :     {
     175           1 :         snaplogger::set_diagnostic(snaplogger::DIAG_KEY_PROGNAME, "message-copying");
     176             : 
     177           2 :         snaplogger::logger::pointer_t l(snaplogger::logger::get_instance());
     178           2 :         snaplogger::buffer_appender::pointer_t buffer(std::make_shared<snaplogger::buffer_appender>("test-buffer"));
     179             : 
     180           1 :         advgetopt::options_environment opt_env;
     181           1 :         opt_env.f_project_name = "test-logger";
     182           2 :         advgetopt::getopt opts(opt_env);
     183           1 :         buffer->set_config(opts);
     184             : 
     185           2 :         snaplogger::format::pointer_t f(std::make_shared<snaplogger::format>("${message}"));
     186           1 :         buffer->set_format(f);
     187             : 
     188           1 :         l->add_appender(buffer);
     189             : 
     190           1 :         int const min_severity(static_cast<int>(snaplogger::severity_t::SEVERITY_MIN));
     191           1 :         int const max_severity(static_cast<int>(snaplogger::severity_t::SEVERITY_MAX));
     192         257 :         for(int i(min_severity); i <= max_severity; ++i)
     193             :         {
     194         256 :             buffer->set_severity(static_cast<snaplogger::severity_t>(i));
     195       65792 :             for(int j(min_severity); j <= max_severity; ++j)
     196             :             {
     197             :                 ::snaplogger::message(
     198             :                           static_cast<::snaplogger::severity_t>(j)
     199             :                         , __FILE__
     200             :                         , __func__
     201             :                         , __LINE__
     202       65536 :                     ) << "The message itself";
     203             : 
     204       65536 :                 if(j >= i
     205       32896 :                 && i != static_cast<int>(snaplogger::severity_t::SEVERITY_OFF)
     206       32895 :                 && j != static_cast<int>(snaplogger::severity_t::SEVERITY_OFF))
     207             :                 {
     208       32640 :                     CATCH_REQUIRE(buffer->str() == "The message itself\n");
     209             :                 }
     210             :                 else
     211             :                 {
     212       32896 :                     CATCH_REQUIRE(buffer->str().empty());
     213             :                 }
     214       65536 :                 buffer->str(std::string());
     215             :             }
     216             :         }
     217             : 
     218           1 :         l->reset();
     219             :     }
     220             :     CATCH_END_SECTION()
     221           7 : }
     222             : 
     223             : 
     224             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.12