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