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