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 :
20 : /** \file
21 : * \brief Implementation of the components.
22 : *
23 : * This file implements the component base classes.
24 : */
25 :
26 : // self
27 : //
28 : #include "snaplogger/component.h"
29 :
30 : #include "snaplogger/exception.h"
31 : #include "snaplogger/guard.h"
32 : #include "snaplogger/private_logger.h"
33 :
34 :
35 : // snapdev lib
36 : //
37 : #include <snapdev/not_used.h>
38 :
39 :
40 : // C++ lib
41 : //
42 : #include <map>
43 :
44 :
45 : // last include
46 : //
47 : #include <snapdev/poison.h>
48 :
49 :
50 :
51 : namespace snaplogger
52 : {
53 :
54 :
55 :
56 2 : component::pointer_t g_cppthread_component(get_component(COMPONENT_CPPTHREAD));
57 2 : component::pointer_t g_debug_component(get_component(COMPONENT_DEBUG));
58 2 : component::pointer_t g_normal_component(get_component(COMPONENT_NORMAL));
59 2 : component::pointer_t g_secure_component(get_component(COMPONENT_SECURE, { g_normal_component }));
60 2 : component::pointer_t g_self_component(get_component(COMPONENT_SELF));
61 2 : component::pointer_t g_banner_component(get_component(COMPONENT_BANNER));
62 :
63 :
64 : /** \brief Create a new component.
65 : *
66 : * \warning
67 : * DO NOT CREATE A COMPONENT DIRECTLY.
68 : *
69 : * Please use the get_component() function whenever you want to create
70 : * a new component. If it already exists, then the existing one will
71 : * be returned.
72 : *
73 : * It is not possible to register the same component more than once.
74 : * The get_component() function makes sure that won't happen.
75 : *
76 : * \param[in] name The name of the new component.
77 : */
78 22 : component::component(std::string const & name)
79 22 : : f_name(name)
80 : {
81 22 : }
82 :
83 :
84 0 : std::string const & component::get_name() const
85 : {
86 0 : return f_name;
87 : }
88 :
89 :
90 4 : void component::add_mutually_exclusive_components(set_t components)
91 : {
92 4 : f_mutually_exclusive_components.insert(components.begin(), components.end());
93 4 : }
94 :
95 :
96 0 : bool component::is_mutually_exclusive(pointer_t other_component) const
97 : {
98 0 : return f_mutually_exclusive_components.find(other_component) != f_mutually_exclusive_components.end();
99 : }
100 :
101 :
102 8 : bool component::is_mutually_exclusive(set_t const & other_components) const
103 : {
104 : // IMPORTNAT NOTE: the sets are sorted by pointers, not names (it
105 : // makes them a lot faster!) so this code compares
106 : // the pointers
107 : //
108 8 : auto mc(f_mutually_exclusive_components.begin());
109 8 : auto me(f_mutually_exclusive_components.end());
110 8 : auto oc(other_components.begin());
111 8 : auto oe(other_components.end());
112 8 : while(mc != me && oc != oe)
113 : {
114 0 : std::intptr_t const c(mc->get() - oc->get());
115 0 : if(c < 0)
116 : {
117 0 : ++mc;
118 : }
119 0 : else if(c > 0)
120 : {
121 0 : ++oc;
122 : }
123 : else
124 : {
125 0 : return true;
126 : }
127 : }
128 :
129 8 : return false;
130 : }
131 :
132 :
133 :
134 :
135 :
136 :
137 :
138 :
139 :
140 :
141 :
142 54 : component::pointer_t get_component(std::string const & name)
143 : {
144 54 : return get_private_logger()->get_component(name);
145 : }
146 :
147 :
148 2 : component::pointer_t get_component(std::string const & name, component::set_t mutually_exclusive)
149 : {
150 2 : component::pointer_t component(get_private_logger()->get_component(name));
151 :
152 2 : component->add_mutually_exclusive_components(mutually_exclusive);
153 :
154 4 : component::set_t new_component = { component };
155 4 : for(auto & me : mutually_exclusive)
156 : {
157 2 : me->add_mutually_exclusive_components(new_component);
158 : }
159 :
160 2 : return component;
161 : }
162 :
163 :
164 0 : component::pointer_t get_component(message const & msg, std::string const & name)
165 : {
166 0 : return get_private_logger(msg)->get_component(name);
167 : }
168 :
169 :
170 6 : } // snaplogger namespace
171 : // vim: ts=4 sw=4 et
|