LCOV - code coverage report
Current view: top level - snaplogger - component.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 1 4 25.0 %
Date: 2021-10-14 20:12:47 Functions: 1 4 25.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : // Copyright (c) 2013-2021  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             : /** \file
      22             :  * \brief Define the component class used as a way to group messages.
      23             :  *
      24             :  * Whenever you send a log, you can assign one or more components to it.
      25             :  *
      26             :  * By default, when a log component's list is empty, it is viewed as if
      27             :  * the list was only composed of the "normal" component.
      28             :  *
      29             :  * Errors generated by the snaplogger library include the "normal" and
      30             :  * "self" components.
      31             :  *
      32             :  * The library also offers a "debug" and "secure" set of components.
      33             :  *
      34             :  * To use these components and your own, use the section() function like so:
      35             :  *
      36             :  * \code
      37             :  *     SNAP_LOG_SEND
      38             :  *         << snaplogger::section(g_normal_component)
      39             :  *         << snaplogger::section(g_self_component)
      40             :  *         << "Message..."
      41             :  *         << SNAP_LOG_SEND;
      42             :  * \endcode
      43             :  *
      44             :  * For the "secure" component, you can also use the special ending macro
      45             :  * like so:
      46             :  *
      47             :  * \code
      48             :  *     SNAP_LOG_SEND
      49             :  *         << snaplogger::section(g_debug_component)
      50             :  *         << "Message..."
      51             :  *         << SNAP_LOG_SEND_SECURELY;
      52             :  * \endcode
      53             :  *
      54             :  * You cannot include "normal" and "secure" to the same message (they are
      55             :  * mutually exclusive). Trying to do so will generate an exception.
      56             :  *
      57             :  * To create a component, you have to use one of the get_component()
      58             :  * functions. One includes a message which means we have a direct access
      59             :  * to the private logger object.
      60             :  */
      61             : 
      62             : 
      63             : // C++ lib
      64             : //
      65             : #include    <map>
      66             : #include    <memory>
      67             : #include    <set>
      68             : #include    <string>
      69             : 
      70             : 
      71             : 
      72             : namespace snaplogger
      73             : {
      74             : 
      75             : 
      76             : class message;
      77             : class private_logger;
      78             : 
      79             : 
      80          18 : class component
      81             : {
      82             : public:
      83             :     typedef std::shared_ptr<component>          pointer_t;
      84             :     typedef std::map<std::string, pointer_t>    map_t;
      85             :     typedef std::set<pointer_t>                 set_t;
      86             : 
      87             :     std::string const &         get_name() const;
      88             : 
      89             :     void                        add_mutually_exclusive_components(set_t components);
      90             :     bool                        is_mutually_exclusive(pointer_t other_component) const;
      91             :     bool                        is_mutually_exclusive(set_t const & other_component) const;
      92             : 
      93             : private:
      94             :     friend private_logger;
      95             : 
      96             :                                 component(std::string const & name);
      97             : 
      98             :     std::string const           f_name;
      99             :     set_t                       f_mutually_exclusive_components = {};
     100             : };
     101             : 
     102             : 
     103             : 
     104             : component::pointer_t            get_component(std::string const & name);
     105             : component::pointer_t            get_component(std::string const & name, component::set_t mutually_exclusive);
     106             : component::pointer_t            get_component(message const & msg, std::string const & name);
     107             : 
     108             : 
     109             : constexpr char const            COMPONENT_CPPTHREAD[] = "cppthread";
     110             : constexpr char const            COMPONENT_DEBUG[]     = "debug";
     111             : constexpr char const            COMPONENT_NORMAL[]    = "normal";
     112             : constexpr char const            COMPONENT_SECURE[]    = "secure";
     113             : constexpr char const            COMPONENT_SELF[]      = "self"; // for the snaplogger only
     114             : constexpr char const            COMPONENT_BANNER[]    = "banner";
     115             : 
     116             : extern component::pointer_t     g_cppthread_component;
     117             : extern component::pointer_t     g_debug_component;
     118             : extern component::pointer_t     g_normal_component;
     119             : extern component::pointer_t     g_secure_component;
     120             : extern component::pointer_t     g_self_component;
     121             : extern component::pointer_t     g_banner_component;
     122             : 
     123             : 
     124           0 : struct section_ptr
     125             : {
     126             :     component::pointer_t    f_component;
     127             : };
     128             : 
     129           0 : inline section_ptr section(component::pointer_t comp)
     130             : {
     131           0 :     return { comp };
     132             : }
     133             : 
     134             : 
     135             : 
     136             : 
     137             : 
     138             : } // snaplogger namespace
     139             : // vim: ts=4 sw=4 et

Generated by: LCOV version 1.13