ALib C++ Library
Library Version: 2412 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
domain.inl
Go to the documentation of this file.
1//==================================================================================================
2/// \file
3/// This header file is part of module \alib_alox of the \aliblong.
4///
5/// \emoji :copyright: 2013-2024 A-Worx GmbH, Germany.
6/// Published under \ref mainpage_license "Boost Software License".
7//==================================================================================================
8#ifndef HPP_ALIB_LOX_DETAIL_DOMAIN
9#define HPP_ALIB_LOX_DETAIL_DOMAIN 1
10#pragma once
11#if !defined(HPP_ALIB_LOX_PROPPERINCLUDE)
12# error "ALib sources with ending '.inl' must not be included from outside."
13#endif
14
16
20
21namespace alib::lox::detail {
22
23// forward declarations
24class ScopeInfo;
25
26//==================================================================================================
27/// Used to store log prefixes. Those that are provided as \ref alib::boxing::Box "boxes" of
28/// character arrays are copied into an internal AString
29/// and will be deleted with the object. This ensures, that simple strings might get assembled
30/// on the stack (aka as local function variables) and still be used as a prefix logable which
31/// survive the livespan on the setting function.
32//==================================================================================================
33class PrefixLogable : public Box
34{
35 protected:
36 /// If set, it will be deleted.
38
39 public:
40 /// Constructor taking the originally provided box. If this is an array of characters, the
41 /// contents is copied into a heap allocated (new) AString and our vtable is replaced accordingly.
42 /// @param pa The pool allocator of the \b Lox.
43 /// @param src The prefix object provided by the user
44 PrefixLogable( PoolAllocator& pa, const Box& src )
45 : Box( src )
46 , copy(pa)
47 {
48 // uses "placement-new" to overwrite the box part of ourselves
49 if( src.IsNotNull() )
50 {
51 if ( IsArrayOf<nchar>() ) new (this) Box(copy.Reset( Unbox<NString>() ));
52 else if ( IsArrayOf<wchar>() ) new (this) Box(copy.Reset( Unbox<WString>() ));
53 else if ( IsArrayOf<xchar>() ) new (this) Box(copy.Reset( Unbox<XString>() ));
54
55 }
56 }
57};
58
59//==================================================================================================
60/// Objects of this class represent a <em>Log Domain</em> of \alox. This class is internally used by
61/// class \b Lox.
62//==================================================================================================
63class Domain
64{
65 public:
66 //==============================================================================================
67 /// Internal class that holds data stored per Logger
68 //==============================================================================================
70 {
71 /// The logger.
73
74 /// The verbosity of the \e Logger for this domain.
75 Verbosity LoggerVerbosity = Verbosity::Off;
76
77 /// The priority value that was used to set the verbosity.
78 config::Priority Priority = config::Priority::NONE;
79
80 /// the number of log calls on this domain for this logger.
82
83 /// Constructor
84 /// @param logger The logger to add
86 {
87 this->Logger= logger;
88 }
89 };
90
91 // #############################################################################################
92 // Public fields
93 // #############################################################################################
94 public:
95
96 /// The name of the domain. For root domains, this is \e nulled.
98
99 /// The full path of the domain (set in the constructor once) .
101
102 /// The parent domain. For root domains, this is \c nullptr.
104
105 /// A list of subdomains, sorted by name.
107 Recycling::None> SubDomains;
108
109 /// Data stored per logger. The index is corresponding to the list of loggers in 'our'
110 /// Lox.
112
113 /// <em>Prefix Logables</em> associated with this domain.
115 std::pair<PrefixLogable*,
117 Recycling::None> PrefixLogables;
118
119 /// A counter for the quantity of calls on this domain.
120 /// Counting does not include:
121 /// - logs when no \e Logger was set
122 /// - conditional logs that were suppressed
123 /// Otherwise, it includes all log calls, even when no \e Logger was enabled on this domain.
125
126 /// Flag which is set when verbosity configuration data was read.
128
129 // #############################################################################################
130 // Public interface
131 // #############################################################################################
132 public:
133 /// @return Returns the domain path separation character.
134 static constexpr
136 {
137 return '/';
138 }
139
140 //==========================================================================================
141 /// Constructor used for the root domain.
142 /// @param allocator The monotonic allocator used allocation of permanent objects.
143 /// @param pool The pool allocator used allocation of non-permanent objects.
144 /// @param name The name of this root domains
145 //==========================================================================================
147 Domain( MonoAllocator& allocator, PoolAllocator& pool, const NString& name );
148
149 //==========================================================================================
150 /// Constructor
151 /// @param parent The parent domain. For root domains, this is \c nullptr.
152 /// @param name The name of the domain.
153 //==========================================================================================
155 Domain( Domain* parent, const NString& name );
156
157 //==========================================================================================
158 /// Returns the root domain of this object.
159 /// @return The root domain of this object
160 //==========================================================================================
162 {
163 Domain* rootDomain= this;
164 while ( rootDomain->Parent != nullptr )
165 rootDomain= rootDomain->Parent;
166 return rootDomain;
167 }
168
169 //==========================================================================================
170 /// Adds a new entry in field #Data and recursively demands the same from its Subdomains.
171 /// Checks if a logger with the same name exists.
172 ///
173 /// @param logger The logger to add.
174 /// @return The number of the \e Logger, -1 if a logger with the same name exists already.
175 //==========================================================================================
177 {
178 // let our root do this
179 if ( Parent != nullptr )
180 return Parent->AddLogger( logger );
181
182 // check for doubles
183 if ( GetLoggerNo( logger->GetName() ) >= 0 )
184 return -1;
185
186 // now this and all children
187 addLoggerRecursive( logger );
188 return static_cast<int>(Data.size()) - 1;
189 }
190
191 //==========================================================================================
192 /// Removes an new entry in field #Data and recursively demands the same from
193 /// its Subdomains.
194 /// @param loggerNo The number of the \e Logger to be removed.
195 //==========================================================================================
196 void RemoveLogger( int loggerNo )
197 {
198 // let our root do this
199 if ( Parent != nullptr )
200 {
201 Parent->RemoveLogger( loggerNo );
202 return;
203 }
204
205 // now this and all children
206 removeLoggerRecursive( loggerNo );
207 }
208
209 //==========================================================================================
210 /// Returns the number of loggers stored in this domain (the same for all domains within
211 /// a tree).
212 /// @return The number of loggers attached.
213 //==========================================================================================
215 {
216 return static_cast<int>( Data.size() );
217 }
218
219 //==========================================================================================
220 /// Searches and returns the \e Logger given by name.
221 /// @param loggerName The logger to search.
222 /// @return The \e Logger found corresponding to given name.
223 /// If the \e Logger does not exist, nullptr is returned.
224 //==========================================================================================
225 detail::Logger* GetLogger( const NString& loggerName )
226 {
227 for ( size_t i= 0; i < Data.size() ; ++i )
228 if ( loggerName.Equals<CHK, lang::Case::Ignore>( Data[i].Logger->GetName()) )
229 return Data[i].Logger;
230 return nullptr;
231 }
232
233 //==========================================================================================
234 /// Returns logger of given number.
235 /// @param no The number of the \e Logger to return.
236 /// @return The \e Logger found with number \p{no}.
237 //==========================================================================================
239 {
240 ALIB_ASSERT_ERROR( no < static_cast<int>(Data.size()), "ALOX", "Internal error: Illegal Logger Number" )
241 return Data[size_t(no)].Logger;
242 }
243
244 //==========================================================================================
245 /// Returns the number of the \e Logger specified by name.
246 /// @param loggerName The logger name to search.
247 /// @return The number of the \e Logger found corresponding to given name.
248 /// If the \e Logger does not exist, -1 is returned.
249 //==========================================================================================
250 int GetLoggerNo( const NString& loggerName )
251 {
252 for ( size_t i= 0; i < Data.size() ; ++i )
253 if ( loggerName.Equals<CHK, lang::Case::Ignore>( Data[i].Logger->GetName() ) )
254 return static_cast<int>( i );
255 return -1;
256 }
257
258 //==========================================================================================
259 /// Returns the number of the \e Logger.
260 /// @param logger The logger to search.
261 /// @return The number of the \e Logger. If the \e Logger does not exist, -1 is returned.
262 //==========================================================================================
264 {
265 for ( size_t i= 0; i < Data.size() ; ++i )
266 if ( logger == Data[i].Logger )
267 return static_cast<int>( i );
268 return -1;
269 }
270
271 //==========================================================================================
272 /// Sets the verbosity for a logger of this domain of all its subdomains to the specified
273 /// value. If given priority is lower than those actually stored, nothing is set and
274 /// recursion is stopped.
275 ///
276 /// @param loggerNo The number of the \e Logger to set the \e Verbosity for.
277 /// @param verbosity The verbosity value to set.
278 /// @param priority The priority of the setting.
279 /// @return The new \e Verbosity.
280 //==========================================================================================
282 Verbosity SetVerbosity( int loggerNo, Verbosity verbosity, Priority priority );
283
284 //==========================================================================================
285 /// Returns the <em>%Log %Domain's %Verbosity</em> for the given logger number.
286 /// @param loggerNo The number of the \e Logger whose \e Verbosity is requested.
287 /// @return The found/defined domain \e Verbosity.
288 //==========================================================================================
289 Verbosity GetVerbosity( int loggerNo )
290 {
291 return Data[size_t(loggerNo)].LoggerVerbosity;
292 }
293
294 //==========================================================================================
295 /// Returns the priority of the \e Verbosity setting for the given logger number.
296 /// @param loggerNo The number of the \e Logger whose \e Verbosity is requested.
297 /// @return The priority.
298 //==========================================================================================
299 Priority GetPriority( int loggerNo )
300 {
301 return Data[size_t(loggerNo)].Priority;
302 }
303
304
305 //==========================================================================================
306 /// Returns the number of log calls for this domain and logger.
307 /// @param loggerNo The number of the \e Logger whose \e Verbosity is requested.
308 /// @return The number of calls executed by this logger on this domain.
309 //==========================================================================================
310 int GetCount( int loggerNo )
311 {
312 return Data[size_t(loggerNo)].LogCallsPerDomain;
313 }
314
315 //==========================================================================================
316 /// Determines if the domain is active in respect to the given Verbosity.
317 ///
318 /// @param loggerNo The number of the \e Logger whose \e Verbosity is to be evaluated against
319 /// \p{statement}.
320 /// @param statement The \e Verbosity to check.
321 /// @return \c true if domain is active (log should be performed)
322 //==========================================================================================
323 bool IsActive( int loggerNo, Verbosity statement )
324 {
325 Verbosity domain= GetVerbosity( loggerNo );
326
327 // domain ^ / stmnt > | Off Error Warning Info Verbose
328 // ---------------------------------------------------------------------
329 // Off | - - - - -
330 // Errors | - Y - - -
331 // Warning | - Y Y - -
332 // Info | - Y Y Y -
333 // Verbose | - Y Y Y Y
334
335 if( statement != Verbosity::Off
336 && ( ( domain == Verbosity::Error && statement == Verbosity::Error )
337 || ( domain == Verbosity::Warning && ( statement == Verbosity::Warning || statement == Verbosity::Error ) )
338 || ( domain == Verbosity::Info && statement != Verbosity::Verbose )
339 || domain == Verbosity::Verbose )
340 )
341 {
342 ++Data[size_t(loggerNo)].LogCallsPerDomain;
343 return true;
344 }
345
346 return false;
347 }
348
349 //==========================================================================================
350 /// Searches a domain. If not found, the domain is (or path of domains are) created in
351 /// the domain tree.
352 /// If the path string starts with the character defined with #Separator, then
353 /// the search (and creation) is done starting from the root domain of this domain and not
354 /// from this domain.
355 ///
356 /// @param domainPath Path and domain to search.
357 /// @param maxCreate The maximum number of subdomains that are created if not
358 /// found at the end of the path.
359 /// @param[out] wasCreated Output parameter that is set \c true if domain was not found
360 /// and hence created. If \c nullptr, it is ignored.
361 /// @return The domain found or created.
362 //==========================================================================================
364 Domain* Find( NSubstring domainPath, int maxCreate, bool* wasCreated );
365
366
367 //==========================================================================================
368 /// Creates a string representation of this object.
369 /// @param target The target string.
370 //==========================================================================================
371 void ToString( NAString& target );
372
373 // #############################################################################################
374 // Internals
375 // #############################################################################################
376 protected:
377 //==========================================================================================
378 /// Internal, recursive helper of #Find.
379 ///
380 /// @param domainPath Path to search.
381 /// @param maxCreate The maximum number of subdomains that are created if not
382 /// found at the end of the path.
383 /// @param[out] wasCreated Output parameter that is set \c true if domain was not found
384 /// and hence created. If \c nullptr, it is ignored.
385 /// @return The domain found or created.
386 //==========================================================================================
387 Domain* findRecursive( NSubstring& domainPath, int maxCreate, bool* wasCreated );
388
389 //==========================================================================================
390 /// Internal, recursive helper of #AddLogger.
391 /// @param logger The logger to add.
392 //==========================================================================================
394 void addLoggerRecursive( detail::Logger* logger);
395
396 //==========================================================================================
397 /// Internal, recursive helper of #RemoveLogger.
398 /// @param loggerNo The number of the \e Logger to be removed.
399 //==========================================================================================
401 void removeLoggerRecursive( int loggerNo );
402
403}; // Domain
404
405}// namespace [alib::lox::detail]
406
407#if !DOXYGEN
409 target.Append(static_cast<const Box&>(src) ); )
410#endif
411
412#endif // HPP_ALIB_LOX_DETAIL_DOMAIN
413
ALIB_API bool IsNotNull() const
Definition boxing.cpp:162
Verbosity GetVerbosity(int loggerNo)
Definition domain.inl:289
bool ConfigurationAlreadyRead
Flag which is set when verbosity configuration data was read.
Definition domain.inl:127
ALIB_API void removeLoggerRecursive(int loggerNo)
Definition domain.cpp:219
Domain * Parent
The parent domain. For root domains, this is nullptr.
Definition domain.inl:103
int GetCount(int loggerNo)
Definition domain.inl:310
ALIB_API Verbosity SetVerbosity(int loggerNo, Verbosity verbosity, Priority priority)
Definition domain.cpp:198
int GetLoggerNo(detail::Logger *logger)
Definition domain.inl:263
int AddLogger(detail::Logger *logger)
Definition domain.inl:176
void ToString(NAString &target)
Definition domain.cpp:226
NString FullPath
The full path of the domain (set in the constructor once) .
Definition domain.inl:100
detail::Logger * GetLogger(int no)
Definition domain.inl:238
bool IsActive(int loggerNo, Verbosity statement)
Definition domain.inl:323
List< PoolAllocator, std::pair< PrefixLogable *, lang::Inclusion >, Recycling::None > PrefixLogables
Prefix Logables associated with this domain.
Definition domain.inl:117
void RemoveLogger(int loggerNo)
Definition domain.inl:196
StdVectorMono< LoggerData > Data
Definition domain.inl:111
Priority GetPriority(int loggerNo)
Definition domain.inl:299
ALIB_API void addLoggerRecursive(detail::Logger *logger)
Definition domain.cpp:212
Domain * findRecursive(NSubstring &domainPath, int maxCreate, bool *wasCreated)
Definition domain.cpp:105
int GetLoggerNo(const NString &loggerName)
Definition domain.inl:250
NString Name
The name of the domain. For root domains, this is nulled.
Definition domain.inl:97
detail::Logger * GetLogger(const NString &loggerName)
Definition domain.inl:225
ALIB_API Domain(MonoAllocator &allocator, PoolAllocator &pool, const NString &name)
Definition domain.cpp:28
List< MonoAllocator, Domain, Recycling::None > SubDomains
A list of subdomains, sorted by name.
Definition domain.inl:107
static constexpr nchar Separator()
Definition domain.inl:135
ALIB_API Domain * Find(NSubstring domainPath, int maxCreate, bool *wasCreated)
Definition domain.cpp:75
Logger(const NString &name, const NString &typeName)
Definition logger.hpp:128
const NString & GetName() const
Definition logger.hpp:173
AStringPA copy
If set, it will be deleted.
Definition domain.inl:37
PrefixLogable(PoolAllocator &pa, const Box &src)
Definition domain.inl:44
bool Equals(const TString< TChar > &rhs) const
Definition string.hpp:580
#define ALIB_API
Definition alib.hpp:639
#define ALIB_STRINGS_APPENDABLE_TYPE_INLINE(TYPE, IMPL)
Definition tastring.inl:174
#define ALIB_ASSERT_ERROR(cond,...)
Definition alib.hpp:1271
Inclusion
Denotes how members of a set something should be taken into account.
std::vector< T, SCAMono< T > > StdVectorMono
Type alias in namespace alib.
Definition stdvector.hpp:21
monomem::TMonoAllocator< lang::HeapAllocator > MonoAllocator
monomem::TPoolAllocator< MonoAllocator, ALIB_MONOMEM_POOLALLOCATOR_DEFAULT_ALIGNMENT > PoolAllocator
characters::nchar nchar
Type alias in namespace alib.
lang::integer integer
Type alias in namespace alib.
Definition integers.hpp:273
See sibling type NC.
Definition alib.hpp:1097
Internal class that holds data stored per Logger.
Definition domain.inl:70
int LogCallsPerDomain
the number of log calls on this domain for this logger.
Definition domain.inl:81
Verbosity LoggerVerbosity
The verbosity of the Logger for this domain.
Definition domain.inl:75
LoggerData(detail::Logger *logger)
Definition domain.inl:85
detail::Logger * Logger
The logger.
Definition domain.inl:72