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 :
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 2 : component::pointer_t g_not_implemented_component(get_component(COMPONENT_NOT_IMPLEMENTED));
63 :
64 :
65 : /** \brief Create a new component.
66 : *
67 : * Please use the get_component() function whenever you want to create
68 : * a new component. If it already exists, then the existing one will
69 : * be returned.
70 : *
71 : * It is not possible to register the same component more than once.
72 : * The get_component() functions make sure that won't happen.
73 : *
74 : * \param[in] name The name of the new component.
75 : */
76 24 : component::component(std::string const & name)
77 24 : : f_name(name)
78 : {
79 24 : }
80 :
81 :
82 14 : std::string const & component::get_name() const
83 : {
84 14 : return f_name;
85 : }
86 :
87 :
88 4 : void component::add_mutually_exclusive_components(set_t components)
89 : {
90 4 : f_mutually_exclusive_components.insert(components.begin(), components.end());
91 4 : }
92 :
93 :
94 0 : bool component::is_mutually_exclusive(pointer_t other_component) const
95 : {
96 0 : return f_mutually_exclusive_components.find(other_component) != f_mutually_exclusive_components.end();
97 : }
98 :
99 :
100 15 : bool component::is_mutually_exclusive(set_t const & other_components) const
101 : {
102 : // IMPORTNAT NOTE: the sets are sorted by pointers, not names (it
103 : // makes them a lot faster!) so this code compares
104 : // the pointers
105 : //
106 15 : auto mc(f_mutually_exclusive_components.begin());
107 15 : auto me(f_mutually_exclusive_components.end());
108 15 : auto oc(other_components.begin());
109 15 : auto oe(other_components.end());
110 15 : while(mc != me && oc != oe)
111 : {
112 0 : std::intptr_t const c(mc->get() - oc->get());
113 0 : if(c < 0)
114 : {
115 0 : ++mc;
116 : }
117 0 : else if(c > 0)
118 : {
119 0 : ++oc;
120 : }
121 : else
122 : {
123 0 : return true;
124 : }
125 : }
126 :
127 15 : return false;
128 : }
129 :
130 :
131 :
132 :
133 :
134 :
135 :
136 :
137 :
138 :
139 :
140 59 : component::pointer_t get_component(std::string const & name)
141 : {
142 59 : return get_private_logger()->get_component(name);
143 : }
144 :
145 :
146 2 : component::pointer_t get_component(std::string const & name, component::set_t mutually_exclusive)
147 : {
148 2 : component::pointer_t component(get_private_logger()->get_component(name));
149 :
150 2 : component->add_mutually_exclusive_components(mutually_exclusive);
151 :
152 4 : component::set_t new_component = { component };
153 4 : for(auto & me : mutually_exclusive)
154 : {
155 2 : me->add_mutually_exclusive_components(new_component);
156 : }
157 :
158 2 : return component;
159 : }
160 :
161 :
162 0 : component::pointer_t get_component(message const & msg, std::string const & name)
163 : {
164 0 : return get_private_logger(msg)->get_component(name);
165 : }
166 :
167 :
168 6 : } // snaplogger namespace
169 : // vim: ts=4 sw=4 et
|