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