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 : /** \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(snaplogger::g_normal_component)
39 : * << snaplogger::section(snaplogger::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(snaplogger::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 :
77 : class message;
78 : class private_logger;
79 :
80 :
81 5 : class component
82 : {
83 : public:
84 : typedef std::shared_ptr<component> pointer_t;
85 : typedef std::map<std::string, pointer_t> map_t;
86 : typedef std::set<pointer_t> set_t;
87 :
88 : std::string const & get_name() const;
89 :
90 : void add_mutually_exclusive_components(set_t components);
91 : bool is_mutually_exclusive(pointer_t other_component) const;
92 : bool is_mutually_exclusive(set_t const & other_component) const;
93 :
94 : private:
95 : friend private_logger;
96 :
97 : component(std::string const & name);
98 :
99 : std::string const f_name;
100 : set_t f_mutually_exclusive_components = {};
101 : };
102 :
103 :
104 :
105 : component::pointer_t get_component(std::string const & name);
106 : component::pointer_t get_component(std::string const & name, component::set_t mutually_exclusive);
107 : component::pointer_t get_component(message const & msg, std::string const & name);
108 :
109 :
110 : constexpr char const COMPONENT_CPPTHREAD[] = "cppthread";
111 : constexpr char const COMPONENT_DEBUG[] = "debug";
112 : constexpr char const COMPONENT_NORMAL[] = "normal";
113 : constexpr char const COMPONENT_SECURE[] = "secure";
114 : constexpr char const COMPONENT_SELF[] = "self"; // for the snaplogger only
115 : constexpr char const COMPONENT_BANNER[] = "banner";
116 : constexpr char const COMPONENT_NOT_IMPLEMENTED[] = "not_implemented";
117 :
118 : extern component::pointer_t g_cppthread_component;
119 : extern component::pointer_t g_debug_component;
120 : extern component::pointer_t g_normal_component;
121 : extern component::pointer_t g_secure_component;
122 : extern component::pointer_t g_self_component;
123 : extern component::pointer_t g_banner_component;
124 : extern component::pointer_t g_not_implemented_component;
125 :
126 :
127 14 : struct section_ptr
128 : {
129 : component::pointer_t f_component;
130 : };
131 :
132 14 : inline section_ptr section(component::pointer_t comp)
133 : {
134 14 : return { comp };
135 : }
136 :
137 :
138 :
139 : } // snaplogger namespace
140 : // vim: ts=4 sw=4 et
|