LCOV - code coverage report
Current view: top level - snaplogger - message.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 1 1 100.0 %
Date: 2019-08-13 00:35:33 Functions: 3 3 100.0 %
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             : #pragma once
      26             : 
      27             : /** \file
      28             :  * \brief Handle the message generator.
      29             :  *
      30             :  * This file declares the base message class which is derived from an
      31             :  * std::stringstram. This allows you to use our logger with `<<`
      32             :  * to send anything that the `<<` operator understands to the logs.
      33             :  */
      34             : 
      35             : 
      36             : // self
      37             : //
      38             : #include    "snaplogger/component.h"
      39             : #include    "snaplogger/environment.h"
      40             : #include    "snaplogger/severity.h"
      41             : 
      42             : 
      43             : // advgetopt lib
      44             : //
      45             : #include    <advgetopt/advgetopt.h>
      46             : 
      47             : 
      48             : // C++ lib
      49             : //
      50             : #include    <sstream>
      51             : #include    <streambuf>
      52             : 
      53             : 
      54             : // C lib
      55             : //
      56             : #include    <sys/time.h>
      57             : 
      58             : 
      59             : 
      60             : namespace snaplogger
      61             : {
      62             : 
      63             : 
      64             : class logger;
      65             : 
      66             : 
      67       98691 : class null_buffer
      68             :     : public std::streambuf
      69             : {
      70             : public:
      71             :     typedef std::unique_ptr<null_buffer>    pointer_t;
      72             : 
      73             :     virtual int         overflow(int c) override;
      74             : };
      75             : 
      76             : 
      77             : // the message class is final because the destructor does tricks which
      78             : // would not work right if derived further
      79             : //
      80             : // also we do not offer a shared pointer because we expect the message
      81             : // object to be created and immediately destroyed from the stack
      82             : //
      83             : class message final
      84             :     : public std::basic_stringstream<char>
      85             : {
      86             : public:
      87             :     typedef std::shared_ptr<message>    pointer_t;
      88             : 
      89             :                                 message(
      90             :                                           severity_t sev = severity_t::SEVERITY_INFORMATION
      91             :                                         , char const * file = nullptr
      92             :                                         , char const * func = nullptr
      93             :                                         , int line = -1);
      94             :                                 message(std::basic_stringstream<char> const & m, message const & msg);
      95             :                                 message(message const & rhs) = delete;
      96             :     virtual                     ~message();
      97             : 
      98             :     message &                   operator = (message const & rhs) = delete;
      99             : 
     100             :     void                        set_severity(severity_t severity);
     101             :     void                        set_filename(std::string const & filename);
     102             :     void                        set_function(std::string const & funcname);
     103             :     void                        set_line(int line);
     104             :     void                        set_recursive_message(bool state) const;
     105             :     void                        add_component(component::pointer_t c);
     106             : 
     107             :     std::shared_ptr<logger>     get_logger() const;
     108             :     severity_t                  get_severity() const;
     109             :     timespec const &            get_timestamp() const;
     110             :     std::string const &         get_filename() const;
     111             :     std::string const &         get_function() const;
     112             :     int                         get_line() const;
     113             :     bool                        get_recursive_message() const;
     114             :     component::set_t const &    get_components() const;
     115             :     environment::pointer_t      get_environment() const;
     116             :     std::string                 get_message() const;
     117             : 
     118             : private:
     119             :     std::shared_ptr<logger>     f_logger = std::shared_ptr<logger>(); // make sure it does not go away under our feet
     120             :     timespec                    f_timestamp = timespec();
     121             :     severity_t                  f_severity = severity_t::SEVERITY_INFORMATION;
     122             :     std::string                 f_filename = std::string();
     123             :     std::string                 f_funcname = std::string();
     124             :     int                         f_line = 0;
     125             :     mutable bool                f_recursive_message = false;
     126             :     environment::pointer_t      f_environment = environment::pointer_t();
     127             :     component::set_t            f_components = component::set_t();
     128             :     null_buffer::pointer_t      f_null = null_buffer::pointer_t();
     129             :     std::streambuf *            f_saved_buffer = nullptr;
     130             :     bool                        f_copy = false;
     131             : };
     132             : 
     133             : 
     134             : template<typename CharT, typename Traits>
     135             : inline std::basic_ostream<CharT, Traits> &
     136             : operator << (std::basic_ostream<CharT, Traits> & os, section_ptr sec)
     137             : {
     138             :     message * m(dynamic_cast<message *>(&os));
     139             :     if(m == nullptr)
     140             :     {
     141             :         os << "(section:"
     142             :            << sec.f_component->get_name()
     143             :            << ")";
     144             :     }
     145             :     else
     146             :     {
     147             :         m->add_component(sec.f_component);
     148             :     }
     149             :     return os;
     150             : }
     151             : 
     152             : 
     153             : template<typename CharT, typename Traits>
     154             : inline std::basic_ostream<CharT, Traits> &
     155             : secure(std::basic_ostream<CharT, Traits> & os)
     156             : {
     157             :     message * m(dynamic_cast<message *>(&os));
     158             :     if(m == nullptr)
     159             :     {
     160             :         os << "(section:secure)";
     161             :     }
     162             :     else
     163             :     {
     164             :         m->add_component(g_secure_component);
     165             :     }
     166             :     return os;
     167             : }
     168             : 
     169             : 
     170             : #define    SNAP_LOG_FATAL               ::snaplogger::message(::snaplogger::severity_t::SEVERITY_FATAL,             __FILE__, __func__, __LINE__)
     171             : #define    SNAP_LOG_EMERG               ::snaplogger::message(::snaplogger::severity_t::SEVERITY_EMERGENCY,         __FILE__, __func__, __LINE__)
     172             : #define    SNAP_LOG_EMERGENCY           ::snaplogger::message(::snaplogger::severity_t::SEVERITY_EMERGENCY,         __FILE__, __func__, __LINE__)
     173             : #define    SNAP_LOG_ALERT               ::snaplogger::message(::snaplogger::severity_t::SEVERITY_ALERT,             __FILE__, __func__, __LINE__)
     174             : #define    SNAP_LOG_CRIT                ::snaplogger::message(::snaplogger::severity_t::SEVERITY_CRITICAL,          __FILE__, __func__, __LINE__)
     175             : #define    SNAP_LOG_CRITICAL            ::snaplogger::message(::snaplogger::severity_t::SEVERITY_CRITICAL,          __FILE__, __func__, __LINE__)
     176             : #define    SNAP_LOG_ERR                 ::snaplogger::message(::snaplogger::severity_t::SEVERITY_ERROR,             __FILE__, __func__, __LINE__)
     177             : #define    SNAP_LOG_ERROR               ::snaplogger::message(::snaplogger::severity_t::SEVERITY_ERROR,             __FILE__, __func__, __LINE__)
     178             : #define    SNAP_LOG_RECOVERABLE_ERROR   ::snaplogger::message(::snaplogger::severity_t::SEVERITY_RECOVERABLE_ERROR, __FILE__, __func__, __LINE__)
     179             : #define    SNAP_LOG_MAJOR               ::snaplogger::message(::snaplogger::severity_t::SEVERITY_MAJOR,             __FILE__, __func__, __LINE__)
     180             : #define    SNAP_LOG_WARN                ::snaplogger::message(::snaplogger::severity_t::SEVERITY_WARNING,           __FILE__, __func__, __LINE__)
     181             : #define    SNAP_LOG_WARNING             ::snaplogger::message(::snaplogger::severity_t::SEVERITY_WARNING,           __FILE__, __func__, __LINE__)
     182             : #define    SNAP_LOG_DEPRECATED          ::snaplogger::message(::snaplogger::severity_t::SEVERITY_DEPRECATED,        __FILE__, __func__, __LINE__)
     183             : #define    SNAP_LOG_MINOR               ::snaplogger::message(::snaplogger::severity_t::SEVERITY_MINOR,             __FILE__, __func__, __LINE__)
     184             : #define    SNAP_LOG_IMPORTANT           ::snaplogger::message(::snaplogger::severity_t::SEVERITY_IMPORTANT,         __FILE__, __func__, __LINE__)
     185             : #define    SNAP_LOG_INFO                ::snaplogger::message(::snaplogger::severity_t::SEVERITY_INFORMATION,       __FILE__, __func__, __LINE__)
     186             : #define    SNAP_LOG_INFORMATION         ::snaplogger::message(::snaplogger::severity_t::SEVERITY_INFORMATION,       __FILE__, __func__, __LINE__)
     187             : #define    SNAP_LOG_UNIMPORTANT         ::snaplogger::message(::snaplogger::severity_t::SEVERITY_UNIMPORTANT,       __FILE__, __func__, __LINE__)
     188             : #define    SNAP_LOG_NOTICE              ::snaplogger::message(::snaplogger::severity_t::SEVERITY_NOTICE,            __FILE__, __func__, __LINE__)
     189             : #define    SNAP_LOG_DEBUG               ::snaplogger::message(::snaplogger::severity_t::SEVERITY_DEBUG,             __FILE__, __func__, __LINE__)
     190             : #define    SNAP_LOG_TRACE               ::snaplogger::message(::snaplogger::severity_t::SEVERITY_TRACE,             __FILE__, __func__, __LINE__)
     191             : 
     192             : 
     193             : 
     194             : } // snaplogger namespace
     195             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.12