LCOV - code coverage report
Current view: top level - tests - diagnostic.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 47 47 100.0 %
Date: 2021-08-20 16:41:45 Functions: 3 3 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/nested_diagnostic.h>
      35             : #include    <snaplogger/message.h>
      36             : #include    <snaplogger/severity.h>
      37             : #include    <snaplogger/version.h>
      38             : 
      39             : 
      40             : // C lib
      41             : //
      42             : #include    <unistd.h>
      43             : 
      44             : 
      45             : 
      46           3 : CATCH_TEST_CASE("diagnostic", "[message][diagnostic]")
      47             : {
      48           2 :     CATCH_START_SECTION("Map based and nested diagnostics")
      49             :     {
      50           1 :         snaplogger::set_diagnostic(snaplogger::DIAG_KEY_PROGNAME, "basic-format");
      51             : 
      52             :         // these two are not called in this test
      53             :         //
      54           1 :         snaplogger::set_diagnostic(snaplogger::DIAG_KEY_PROJECT_NAME, "test-logger");
      55           1 :         snaplogger::set_diagnostic(snaplogger::DIAG_KEY_VERSION, "5.32.1024");
      56             : 
      57             :         // test with our own diagnostics too
      58             :         //
      59           1 :         snaplogger::set_diagnostic("test_diag", "X-66-Q");
      60             : 
      61           2 :         snaplogger::logger::pointer_t l(snaplogger::logger::get_instance());
      62           2 :         snaplogger::buffer_appender::pointer_t buffer(std::make_shared<snaplogger::buffer_appender>("test-buffer"));
      63             : 
      64           1 :         char const * cargv[] =
      65             :         {
      66             :             "/usr/bin/daemon",
      67             :             nullptr
      68             :         };
      69           1 :         int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
      70           1 :         char ** argv = const_cast<char **>(cargv);
      71             : 
      72           1 :         advgetopt::options_environment environment_options;
      73           1 :         environment_options.f_project_name = "test-logger";
      74           1 :         environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
      75           1 :         environment_options.f_version = "5.32.1024";
      76           2 :         advgetopt::getopt opts(environment_options);
      77           1 :         opts.parse_program_name(argv);
      78           1 :         opts.parse_arguments(argc, argv, advgetopt::option_source_t::SOURCE_COMMAND_LINE);
      79             : 
      80           1 :         buffer->set_config(opts);
      81             : 
      82           2 :         snaplogger::format::pointer_t f(std::make_shared<snaplogger::format>("${project_name} ${message} v${version}"));
      83           1 :         buffer->set_format(f);
      84             : 
      85           1 :         l->add_appender(buffer);
      86             : 
      87           2 :         SNAP_LOG_WARNING
      88           1 :             << "{${diagnostic:map=test_diag}}"
      89             :             << SNAP_LOG_SEND;
      90             : 
      91           1 :         CATCH_REQUIRE(buffer->str() == "test-logger {<test_diag=X-66-Q>} v5.32.1024"
      92             :                 "\n");
      93             : 
      94           1 :         buffer->clear();
      95             : 
      96             :         {
      97           2 :             snaplogger::nested_diagnostic l1("level-I");
      98             : 
      99           2 :             SNAP_LOG_WARNING
     100           1 :                 << "$${diagnostic:map=test_diag}$ & [${diagnostic:nested=10}]"
     101             :                 << SNAP_LOG_SEND;
     102             : 
     103           1 :             CATCH_REQUIRE(buffer->str() ==
     104             :                     "test-logger $<test_diag=X-66-Q>$ & [{level-I}] v5.32.1024"
     105             :                     "\n");
     106             : 
     107           1 :             buffer->clear();
     108             : 
     109             :             {
     110           2 :                 snaplogger::nested_diagnostic l2("sub-level-II");
     111             : 
     112           2 :                 SNAP_LOG_WARNING
     113           1 :                     << "$${diagnostic:map=test_diag}$ & [${diagnostic:nested=10}]"
     114             :                     << SNAP_LOG_SEND;
     115             : 
     116           1 :                 CATCH_REQUIRE(buffer->str() ==
     117             :                         "test-logger $<test_diag=X-66-Q>$ & [{level-I/sub-level-II}] v5.32.1024"
     118             :                         "\n");
     119             : 
     120           1 :                 buffer->clear();
     121             : 
     122             :                 {
     123           2 :                     snaplogger::nested_diagnostic l3("under-level-III");
     124             : 
     125           2 :                     SNAP_LOG_WARNING
     126           1 :                         << "$${diagnostic:map=test_diag}$ & [${diagnostic:nested=10}]"
     127             :                         << SNAP_LOG_SEND;
     128             : 
     129           1 :                     CATCH_REQUIRE(buffer->str() ==
     130             :                             "test-logger $<test_diag=X-66-Q>$ & [{level-I/sub-level-II/under-level-III}] v5.32.1024"
     131             :                             "\n");
     132             : 
     133           1 :                     buffer->clear();
     134             : 
     135           2 :                     SNAP_LOG_WARNING
     136           1 :                         << "$${diagnostic:map=test_diag}$ & [${diagnostic:nested=2}]"
     137             :                         << SNAP_LOG_SEND;
     138             : 
     139           1 :                     CATCH_REQUIRE(buffer->str() ==
     140             :                             "test-logger $<test_diag=X-66-Q>$ & [{.../sub-level-II/under-level-III}] v5.32.1024"
     141             :                             "\n");
     142             : 
     143           1 :                     buffer->clear();
     144             :                 }
     145             :             }
     146             :         }
     147             : 
     148           1 :         l->reset();
     149             :     }
     150             :     CATCH_END_SECTION()
     151           7 : }
     152             : 
     153             : 
     154             : 
     155             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.13