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