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