Line data Source code
1 : /*
2 : * Copyright (c) 2013-2021 Made to Order Software Corp. All Rights Reserved
3 : *
4 : * https://snapwebsites.org/project/snaplogger
5 : * contact@m2osw.com
6 : *
7 : * This program is free software; you can redistribute it and/or modify
8 : * it under the terms of the GNU General Public License as published by
9 : * the Free Software Foundation; either version 2 of the License, or
10 : * (at your option) any later version.
11 : *
12 : * This program is distributed in the hope that it will be useful,
13 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 : * GNU General Public License for more details.
16 : *
17 : * You should have received a copy of the GNU General Public License along
18 : * with this program; if not, write to the Free Software Foundation, Inc.,
19 : * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 : */
21 : #pragma once
22 :
23 : /** \file
24 : * \brief Define the component class used as a way to group messages.
25 : *
26 : * Whenever you send a log, you can assign one or more components to it.
27 : *
28 : * By default, when a log component's list is empty, it is viewed as if
29 : * the list was only composed of the "normal" component.
30 : *
31 : * Errors generated by the snaplogger library include the "normal" and
32 : * "self" components.
33 : *
34 : * The library also offers a "debug" and "secure" set of components.
35 : *
36 : * To use these components and your own, use the section() function like so:
37 : *
38 : * \code
39 : * SNAP_LOG_SEND
40 : * << snaplogger::section(g_normal_component)
41 : * << snaplogger::section(g_self_component)
42 : * << "Message..."
43 : * << SNAP_LOG_SEND;
44 : * \endcode
45 : *
46 : * For the "secure" component, you can also use the special ending macro
47 : * like so:
48 : *
49 : * \code
50 : * SNAP_LOG_SEND
51 : * << snaplogger::section(g_debug_component)
52 : * << "Message..."
53 : * << SNAP_LOG_SEND_SECURELY;
54 : * \endcode
55 : *
56 : * You cannot include "normal" and "secure" to the same message (they are
57 : * mutually exclusive). Trying to do so will generate an exception.
58 : *
59 : * \warning
60 : * Do NOT create a component directly. The only real interface accessible
61 : * to create a component as far as you're concerned are the get_component()
62 : * functions. One includes a message which means we have a direct access
63 : * to the private logger object.
64 : */
65 :
66 :
67 : // C++ lib
68 : //
69 : #include <map>
70 : #include <memory>
71 : #include <set>
72 : #include <string>
73 :
74 :
75 :
76 : namespace snaplogger
77 : {
78 :
79 :
80 : class message;
81 :
82 :
83 16 : class component
84 : {
85 : public:
86 : typedef std::shared_ptr<component> pointer_t;
87 : typedef std::map<std::string, pointer_t> map_t;
88 : typedef std::set<pointer_t> set_t;
89 :
90 : component(std::string const & name);
91 :
92 : std::string const & get_name() const;
93 :
94 : void add_mutually_exclusive_components(set_t components);
95 : bool is_mutually_exclusive(pointer_t other_component) const;
96 : bool is_mutually_exclusive(set_t const & other_component) const;
97 :
98 : private:
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 :
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 :
122 :
123 0 : struct section_ptr
124 : {
125 : component::pointer_t f_component;
126 : };
127 :
128 0 : inline section_ptr section(component::pointer_t comp)
129 : {
130 0 : return { comp };
131 : }
132 :
133 :
134 :
135 :
136 :
137 : } // snaplogger namespace
138 : // vim: ts=4 sw=4 et
|