LCOV - code coverage report
Current view: top level - tests - catch_component.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 64 64 100.0 %
Date: 2022-07-01 22:43:09 Functions: 5 5 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : // Copyright (c) 2006-2022  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             : // this test checks components
      21             : //
      22             : #include    <snaplogger/component.h>
      23             : 
      24             : 
      25             : // self
      26             : //
      27             : #include    "catch_main.h"
      28             : 
      29             : 
      30             : // snaplogger lib
      31             : //
      32             : #include    <snaplogger/buffer_appender.h>
      33             : #include    <snaplogger/format.h>
      34             : #include    <snaplogger/logger.h>
      35             : #include    <snaplogger/message.h>
      36             : #include    <snaplogger/options.h>
      37             : 
      38             : 
      39             : 
      40             : 
      41             : 
      42             : 
      43           5 : CATCH_TEST_CASE("component", "[component]")
      44             : {
      45           6 :     CATCH_START_SECTION("component: Write component to stream")
      46             :     {
      47             :         {
      48           2 :             std::stringstream ss;
      49           1 :             ss << snaplogger::section(snaplogger::g_cppthread_component);
      50           1 :             CATCH_REQUIRE(ss.str() == "(section:cppthread)");
      51             :         }
      52             : 
      53             :         {
      54           2 :             std::stringstream ss;
      55           1 :             ss << snaplogger::section(snaplogger::g_debug_component);
      56           1 :             CATCH_REQUIRE(ss.str() == "(section:debug)");
      57             :         }
      58             : 
      59             :         {
      60           2 :             std::stringstream ss;
      61           1 :             ss << snaplogger::section(snaplogger::g_normal_component);
      62           1 :             CATCH_REQUIRE(ss.str() == "(section:normal)");
      63             :         }
      64             : 
      65             :         {
      66           2 :             std::stringstream ss;
      67           1 :             ss << snaplogger::section(snaplogger::g_secure_component);
      68           1 :             CATCH_REQUIRE(ss.str() == "(section:secure)");
      69             :         }
      70             : 
      71             :         {
      72           2 :             std::stringstream ss;
      73           1 :             ss << snaplogger::section(snaplogger::g_self_component);
      74           1 :             CATCH_REQUIRE(ss.str() == "(section:self)");
      75             :         }
      76             : 
      77             :         {
      78           2 :             std::stringstream ss;
      79           1 :             ss << snaplogger::section(snaplogger::g_banner_component);
      80           1 :             CATCH_REQUIRE(ss.str() == "(section:banner)");
      81             :         }
      82             : 
      83             :         {
      84           2 :             std::stringstream ss;
      85           1 :             ss << snaplogger::section(snaplogger::g_not_implemented_component);
      86           1 :             CATCH_REQUIRE(ss.str() == "(section:not_implemented)");
      87             :         }
      88             :     }
      89             :     CATCH_END_SECTION()
      90             : 
      91           6 :     CATCH_START_SECTION("component: Make sure creating component generates unique entries")
      92             :     {
      93             :         // this worked from the start since the private logger instance
      94             :         // uses a map to store the components
      95             :         //
      96          30 :         struct name_ptr
      97             :         {
      98             :             typedef std::vector<name_ptr>   vector_t;
      99             : 
     100             :             std::string     f_name = std::string();
     101             :             snaplogger::component::pointer_t
     102             :                             f_component = snaplogger::component::pointer_t();
     103             :         };
     104           1 :         name_ptr::vector_t names =
     105             :         {
     106             :             { "component1", },
     107             :             { "component2", },
     108             :             { "component3", },
     109             :             { "component4", },
     110             :             { "component5", },
     111             :             { "component6", },
     112             :             { "component7", },
     113             :             { "component8", },
     114             :             { "component9", },
     115             :             { "component10", },
     116           2 :         };
     117             : 
     118             :         // create the components
     119             :         //
     120          11 :         for(auto & p : names)
     121             :         {
     122          10 :             p.f_component = snaplogger::get_component(p.f_name);
     123             :         }
     124             : 
     125             :         // verify the component pointers
     126             :         //
     127          11 :         for(auto & p : names)
     128             :         {
     129          10 :             CATCH_REQUIRE(p.f_component == snaplogger::get_component(p.f_name));
     130             :         }
     131             :     }
     132             :     CATCH_END_SECTION()
     133             : 
     134           6 :     CATCH_START_SECTION("component: Send a component via the macros and << operator")
     135             :     {
     136           2 :         snaplogger::logger::pointer_t l(snaplogger::logger::get_instance());
     137             : 
     138           2 :         snaplogger::buffer_appender::pointer_t buffer(std::make_shared<snaplogger::buffer_appender>("test-buffer"));
     139           1 :         buffer->add_component(snaplogger::g_debug_component);
     140           1 :         snaplogger::format::pointer_t f(std::make_shared<snaplogger::format>(
     141           2 :                                         "${message} (${severity:format=number}) -- ${components}"));
     142           1 :         buffer->set_format(f);
     143           1 :         l->add_appender(buffer);
     144             : 
     145           1 :         char const * cargv[] =
     146             :         {
     147             :             "/usr/bin/daemon",
     148             :             "--log-severity",
     149             :             "noisy",
     150             :             nullptr
     151             :         };
     152           1 :         int const argc(sizeof(cargv) / sizeof(cargv[0]) - 1);
     153           1 :         char ** argv = const_cast<char **>(cargv);
     154             : 
     155           1 :         advgetopt::options_environment environment_options;
     156           1 :         environment_options.f_project_name = "test-logger";
     157           1 :         environment_options.f_environment_flags = advgetopt::GETOPT_ENVIRONMENT_FLAG_SYSTEM_PARAMETERS;
     158           1 :         environment_options.f_version = "3.4";
     159           2 :         advgetopt::getopt opts(environment_options);
     160           1 :         opts.parse_program_name(argv);
     161           1 :         snaplogger::add_logger_options(opts);
     162           1 :         opts.parse_arguments(argc, argv, advgetopt::option_source_t::SOURCE_COMMAND_LINE);
     163             : 
     164           1 :         CATCH_REQUIRE(snaplogger::process_logger_options(opts, "/etc/my-app/logger"));
     165             : 
     166           1 :         buffer->set_config(opts);
     167             : 
     168           3 :         SNAP_LOG_NOISY
     169             :             << "We got a component!"
     170           2 :             << snaplogger::section(snaplogger::g_debug_component)
     171             :             << SNAP_LOG_SEND;
     172             : 
     173           1 :         CATCH_REQUIRE_FALSE(buffer->empty());
     174             : 
     175           2 :         std::string const severity1(std::to_string(static_cast<int>(snaplogger::severity_t::SEVERITY_INFORMATION)));
     176           2 :         std::string const severity2(std::to_string(static_cast<int>(snaplogger::severity_t::SEVERITY_NOISY)));
     177           3 :         std::string const expected("-------------------------------------------------- (" + severity1 + ") -- [normal,self,banner]\n"
     178           3 :                       "test-logger v3.4 started. (" + severity1 + ") -- [normal,self,banner]\n"
     179           3 :                       "We got a component! (" + severity2 + ") -- [debug]\n");
     180           1 :         CATCH_REQUIRE(buffer->str() == expected);
     181             : 
     182           1 :         buffer->clear();
     183             : 
     184           1 :         l->reset();
     185             :     }
     186             :     CATCH_END_SECTION()
     187           9 : }
     188             : 
     189             : 
     190             : 
     191             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.13