LCOV - code coverage report
Current view: top level - snaplogger - message.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 56 70 80.0 %
Date: 2019-08-13 00:35:33 Functions: 17 26 65.4 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.12