LCOV - code coverage report
Current view: top level - snaplogger - severity.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 8 8 100.0 %
Date: 2022-07-01 22:43:09 Functions: 2 2 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : // Copyright (c) 2013-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             : #pragma once
      20             : 
      21             : 
      22             : /** \file
      23             :  * \brief Severity levels for your log messages.
      24             :  *
      25             :  * The severity implementation loads the severity configuration file
      26             :  * and generates a set of severity levels that one can attach to
      27             :  * log messages.
      28             :  */
      29             : 
      30             : // self
      31             : //
      32             : #include    "snaplogger/utils.h"
      33             : 
      34             : 
      35             : // C++ lib
      36             : //
      37             : #include    <memory>
      38             : 
      39             : 
      40             : // C lib
      41             : //
      42             : #include    <sys/time.h>
      43             : 
      44             : 
      45             : 
      46             : namespace snaplogger
      47             : {
      48             : 
      49             : 
      50             : class message;
      51             : 
      52             : 
      53             : enum class severity_t
      54             : {
      55             :     SEVERITY_ALL                = 0,
      56             :     SEVERITY_TRACE              = 10,
      57             :     SEVERITY_NOISY              = 20,
      58             :     SEVERITY_DEBUG              = 30,
      59             :     SEVERITY_NOTICE             = 40,
      60             :     SEVERITY_UNIMPORTANT        = 50,
      61             :     SEVERITY_VERBOSE            = 60,
      62             :     SEVERITY_CONFIGURATION      = 70,
      63             :     SEVERITY_INFORMATION        = 80,
      64             :     SEVERITY_IMPORTANT          = 90,
      65             :     SEVERITY_MINOR              = 100,
      66             :     SEVERITY_TODO               = 110,
      67             :     SEVERITY_DEPRECATED         = 120,
      68             :     SEVERITY_WARNING            = 130,
      69             :     SEVERITY_MAJOR              = 150,
      70             :     SEVERITY_RECOVERABLE_ERROR  = 170,
      71             :     SEVERITY_ERROR              = 180,
      72             :     SEVERITY_SEVERE             = 190,
      73             :     SEVERITY_EXCEPTION          = 200,
      74             :     SEVERITY_CRITICAL           = 210,
      75             :     SEVERITY_ALERT              = 220,
      76             :     SEVERITY_EMERGENCY          = 230,
      77             :     SEVERITY_FATAL              = 250,
      78             :     SEVERITY_OFF                = 255,
      79             : 
      80             :     SEVERITY_DEFAULT = SEVERITY_INFORMATION,        // WARNING: can dynamically be changed using the severity.ini file
      81             :     SEVERITY_MIN = SEVERITY_ALL,
      82             :     SEVERITY_MAX = SEVERITY_OFF
      83             : };
      84             : 
      85             : 
      86             : typedef std::vector<severity_t>     severity_array_t;
      87             : 
      88             : 
      89             : 
      90             : 
      91           4 : class severity
      92             : {
      93             : public:
      94             :     typedef std::shared_ptr<severity>           pointer_t;
      95             : 
      96             :                         severity(severity_t sev, std::string const & name, bool system = false);
      97             : 
      98             :     severity_t          get_severity() const;
      99             :     bool                is_system() const;
     100             :     void                mark_as_registered();
     101             :     bool                is_registered() const;
     102             : 
     103             :     std::string         get_name() const;
     104             :     void                add_alias(std::string const & name);
     105             :     string_vector_t     get_all_names() const;
     106             : 
     107             :     void                set_description(std::string const & description);
     108             :     std::string         get_description() const;
     109             : 
     110             :     void                set_styles(std::string const & styles);
     111             :     std::string         get_styles() const;
     112             : 
     113             : private:
     114             :     severity_t const    f_severity;
     115             :     string_vector_t     f_names = string_vector_t();
     116             :     bool const          f_system;
     117             :     bool                f_registered = false;
     118             :     std::string         f_description = std::string();
     119             :     std::string         f_styles = std::string();
     120             : };
     121             : 
     122             : typedef std::map<severity_t, severity::pointer_t>   severity_by_severity_t;
     123             : typedef std::map<std::string, severity::pointer_t>  severity_by_name_t;
     124             : 
     125             : void                    add_severity(severity::pointer_t sev);
     126             : severity::pointer_t     get_severity(std::string const & name);
     127             : severity::pointer_t     get_severity(message const & msg, std::string const & name);
     128             : severity::pointer_t     get_severity(severity_t sev);
     129             : severity::pointer_t     get_severity(message const & msg, severity_t sev);
     130             : severity_by_name_t      get_severities_by_name();
     131             : severity_by_severity_t  get_severities_by_severity();
     132             : 
     133             : template<typename CharT, typename Traits>
     134             : inline std::basic_ostream<CharT, Traits> &
     135          20 : operator << (std::basic_ostream<CharT, Traits> & os, severity_t sev)
     136             : {
     137          40 :     severity::pointer_t s(get_severity(sev));
     138          20 :     if(s == nullptr)
     139             :     {
     140           2 :         os << "(unknown severity: "
     141             :            << static_cast<int>(sev)
     142           1 :            << ")";
     143             :     }
     144             :     else
     145             :     {
     146          19 :         os << s->get_name();
     147             :     }
     148          40 :     return os;
     149             : }
     150             : 
     151             : 
     152             : 
     153             : } // snaplogger namespace
     154             : 
     155             : 
     156             : // outside of namespace so it can be used right up
     157             : //
     158             : #if defined(__GNUC__) && __GNUC__ >= 7 && __GNUC_MINOR__ >= 5 && __GNUC_PATCHLEVEL__ >= 0
     159             : snaplogger::severity::pointer_t     operator ""_sev (char const * name, unsigned long size);
     160             : #endif
     161             : 
     162             : 
     163             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.13