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