LCOV - code coverage report
Current view: top level - snaplogger - message.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 74 74 100.0 %
Date: 2019-08-18 12:59:55 Functions: 23 27 85.2 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2013-2019  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             : /** \file
      23             :  * \brief Appenders are used to append data to somewhere.
      24             :  *
      25             :  * This file declares the base appender class.
      26             :  */
      27             : 
      28             : // self
      29             : // 
      30             : #include    "snaplogger/message.h"
      31             : 
      32             : #include    "snaplogger/exception.h"
      33             : #include    "snaplogger/logger.h"
      34             : 
      35             : 
      36             : // C++ lib
      37             : //
      38             : #include    <iostream>
      39             : 
      40             : 
      41             : // C lib
      42             : //
      43             : #include    <sys/time.h>
      44             : 
      45             : 
      46             : // last include
      47             : //
      48             : #include    <snapdev/poison.h>
      49             : 
      50             : 
      51             : 
      52             : namespace snaplogger
      53             : {
      54             : 
      55             : 
      56     1093377 : int null_buffer::overflow(int c)
      57             : {
      58     1093377 :     return c;
      59             : }
      60             : 
      61             : 
      62             : 
      63             : 
      64             : 
      65       92588 : message::message(
      66             :           severity_t sev
      67             :         , char const * file
      68             :         , char const * func
      69             :         , int line)
      70             :     : f_logger(logger::get_instance())
      71             :     , f_severity(sev)
      72             :     , f_filename(file == nullptr ? std::string() : std::string(file))
      73             :     , f_funcname(func == nullptr ? std::string() : std::string(func))
      74             :     , f_line(line)
      75       92588 :     , f_environment(create_environment())
      76             : {
      77       92588 :     clock_gettime(CLOCK_REALTIME_COARSE, &f_timestamp);
      78             : 
      79      185176 :     if(f_severity < f_logger->get_lowest_severity()
      80       92588 :     || f_severity == severity_t::SEVERITY_OFF)
      81             :     {
      82       47638 :         f_null.reset(new null_buffer);
      83       47638 :         std::ostream & ref = *this;
      84       47638 :         f_saved_buffer = ref.rdbuf(f_null.get());
      85             :     }
      86       92588 : }
      87             : 
      88             : 
      89           1 : message::message(std::basic_stringstream<char> const & m, message const & msg)
      90             :     : f_logger(msg.f_logger)
      91             :     , f_timestamp(msg.f_timestamp)
      92           1 :     , f_severity(msg.f_severity)
      93             :     , f_filename(msg.f_filename)
      94             :     , f_funcname(msg.f_funcname)
      95           1 :     , f_line(msg.f_line)
      96           1 :     , f_recursive_message(msg.f_recursive_message)
      97             :     , f_environment(msg.f_environment)
      98             :     , f_components(msg.f_components)
      99             :     , f_null(null_buffer::pointer_t())
     100             :     , f_saved_buffer(nullptr)
     101           4 :     , f_copy(true)
     102             : {
     103           1 :     *this << m.rdbuf();
     104           1 : }
     105             : 
     106             : 
     107      185178 : message::~message()
     108             : {
     109       92589 :     if(f_saved_buffer != nullptr)
     110             :     {
     111       47638 :         std::ostream & ref = *this;
     112       47638 :         ref.rdbuf(f_saved_buffer);
     113             :     }
     114       92589 : }
     115             : 
     116             : 
     117       27032 : void message::set_severity(severity_t severity)
     118             : {
     119       27032 :     f_severity = severity;
     120       27032 : }
     121             : 
     122             : 
     123           1 : void message::set_filename(std::string const & filename)
     124             : {
     125           1 :     f_filename = filename;
     126           1 : }
     127             : 
     128             : 
     129           1 : void message::set_function(std::string const & funcname)
     130             : {
     131           1 :     f_funcname = funcname;
     132           1 : }
     133             : 
     134             : 
     135           1 : void message::set_line(int line)
     136             : {
     137           1 :     f_line = line;
     138           1 : }
     139             : 
     140             : 
     141           8 : void message::set_recursive_message(bool state) const
     142             : {
     143           8 :     f_recursive_message = state;
     144           8 : }
     145             : 
     146             : 
     147           4 : void message::add_component(component::pointer_t c)
     148             : {
     149           4 :     if(c != nullptr)
     150             :     {
     151           4 :         f_components.insert(c);
     152             :     }
     153           4 : }
     154             : 
     155             : 
     156       45571 : std::shared_ptr<logger> message::get_logger() const
     157             : {
     158       45571 :     return f_logger;
     159             : }
     160             : 
     161             : 
     162       92588 : severity_t message::get_severity() const
     163             : {
     164       92588 :     return f_severity;
     165             : }
     166             : 
     167             : 
     168           4 : timespec const & message::get_timestamp() const
     169             : {
     170           4 :     return f_timestamp;
     171             : }
     172             : 
     173             : 
     174           2 : std::string const & message::get_filename() const
     175             : {
     176           2 :     return f_filename;
     177             : }
     178             : 
     179             : 
     180           2 : std::string const & message::get_function() const
     181             : {
     182           2 :     return f_funcname;
     183             : }
     184             : 
     185             : 
     186           2 : int message::get_line() const
     187             : {
     188           2 :     return f_line;
     189             : }
     190             : 
     191             : 
     192       45546 : bool message::get_recursive_message() const
     193             : {
     194       45546 :     return f_recursive_message;
     195             : }
     196             : 
     197             : 
     198      138137 : component::set_t const & message::get_components() const
     199             : {
     200      138137 :     return f_components;
     201             : }
     202             : 
     203             : 
     204           6 : environment::pointer_t message::get_environment() const
     205             : {
     206           6 :     return f_environment;
     207             : }
     208             : 
     209             : 
     210       45545 : std::string message::get_message() const
     211             : {
     212       45545 :     std::string s(str());
     213       45545 :     if(s.back() == '\n')
     214             :     {
     215           3 :         s.pop_back();
     216             :     }
     217       45545 :     if(s.back() == '\r')
     218             :     {
     219           1 :         s.pop_back();
     220             :     }
     221       45545 :     return s;
     222             : }
     223             : 
     224             : 
     225       92587 : void send_message(std::basic_ostream<char> & out)
     226             : {
     227       92587 :     message * msg(dynamic_cast<message *>(&out));
     228       92587 :     if(msg == nullptr)
     229             :     {
     230           1 :         throw not_a_message("the 'out' parameter to the send_message() function is expected to be a snaplogger::message object.");
     231             :     }
     232             : 
     233       92586 :     logger::get_instance()->log_message(*msg);
     234       92586 : }
     235             : 
     236             : 
     237             : 
     238             : 
     239           6 : } // snaplogger namespace
     240             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.12