LCOV - code coverage report
Current view: top level - tests - catch_log_for_test.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 21 39 53.8 %
Date: 2022-06-26 14:27:46 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/libaddr
       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             : // self
      21             : //
      22             : #include    "catch_main.h"
      23             : 
      24             : 
      25             : // cppthread
      26             : //
      27             : #include    <cppthread/log.h>
      28             : 
      29             : 
      30             : // libexcept
      31             : //
      32             : #include    <libexcept/exception.h>
      33             : 
      34             : 
      35             : // last include
      36             : //
      37             : #include    <snapdev/poison.h>
      38             : 
      39             : 
      40             : 
      41             : 
      42             : namespace SNAP_CATCH2_NAMESPACE
      43             : {
      44             : 
      45             : 
      46             : 
      47             : namespace
      48             : {
      49             : 
      50           2 : std::vector<std::string>        g_expected_logs = std::vector<std::string>();
      51             : 
      52             : }
      53             : // no name namespace
      54             : 
      55             : 
      56             : 
      57           1 : void push_expected_log(std::string const & message)
      58             : {
      59           1 :     g_expected_logs.push_back(message);
      60           1 : }
      61             : 
      62             : 
      63           1 : void log_for_test(cppthread::log_level_t level, std::string const & message)
      64             : {
      65           2 :     if(SNAP_CATCH2_NAMESPACE::g_verbose()
      66           1 :     || g_expected_logs.empty())
      67             :     {
      68             :         std::cerr << "logger sent:\n"
      69           0 :                   << cppthread::to_string(level)
      70             :                   << ": "
      71           0 :                   << message
      72           0 :                   << std::endl;
      73             :     }
      74             : 
      75             :     // at this time it's impossible to debug the location of the empty
      76             :     // problem without a proper stack trace...
      77             :     //
      78           1 :     if(g_expected_logs.empty())
      79             :     {
      80           0 :         libexcept::stack_trace_t trace(libexcept::collect_stack_trace_with_line_numbers());
      81           0 :         std::cerr << "*** STACK TRACE ***" << std::endl;
      82           0 :         for(auto const & l : trace)
      83             :         {
      84           0 :             std::cerr << l << std::endl;
      85             :         }
      86           0 :         std::cerr << "***" << std::endl;
      87             :     }
      88             : 
      89           1 :     CATCH_REQUIRE_FALSE(g_expected_logs.empty());
      90             : 
      91           2 :     std::stringstream ss;
      92           1 :     ss << cppthread::to_string(level) << ": " << message;
      93             : 
      94             :     // again, the REQUIRE() is not going to be useful in terms of line number
      95             :     //
      96           1 :     if(g_expected_logs[0] != ss.str())
      97             :     {
      98           0 :         libexcept::stack_trace_t trace(libexcept::collect_stack_trace_with_line_numbers());
      99           0 :         std::cerr << "*** STACK TRACE ***" << std::endl;
     100           0 :         for(auto const & l : trace)
     101             :         {
     102           0 :             std::cerr << l << std::endl;
     103             :         }
     104           0 :         std::cerr << "***" << std::endl;
     105             :     }
     106             : 
     107           2 :     std::string expected_msg(g_expected_logs[0]);
     108           1 :     g_expected_logs.erase(g_expected_logs.begin());
     109             : 
     110           1 :     CATCH_REQUIRE(expected_msg == ss.str());
     111           1 : }
     112             : 
     113             : 
     114           2 : void expected_logs_stack_is_empty()
     115             : {
     116           2 :     if(!g_expected_logs.empty())
     117             :     {
     118           0 :         std::cerr << "List of expected error logs which did not occur:" << std::endl;
     119           0 :         for(auto l : g_expected_logs)
     120             :         {
     121           0 :             std::cerr << "  " << l << std::endl;
     122             :         }
     123           0 :         g_expected_logs.clear();
     124           0 :         throw std::logic_error("a test left an unexpected error message in the g_expected_logs vector.");
     125             :     }
     126             : 
     127           2 :     cppthread::log.reset_counters();
     128           2 : }
     129             : 
     130             : 
     131           6 : } // SNAP_CATCH2_NAMESPACE namespace
     132             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.13