ALib C++ Framework
by
Library Version: 2605 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
alox.cpp
1#if !DOXYGEN
2namespace alib::lox::detail {
3namespace { ListMA<Lox*> loxes(monomem::GLOBAL_ALLOCATOR); }
4integer dbgCountLoxes() { return loxes.size(); }
5void shutdownLoxes() {
6 while ( loxes.IsNotEmpty() )
7 loxes.back()->~Lox();
8 loxes.Reset();
9}
10
11}
12#endif
13
14namespace alib::lox {
15
17
18//##################################################################################################
19// Lox management
20//##################################################################################################
21#if !DOXYGEN
22
23#if ALOX_DBG_LOG
24 Lox* DEBUG_LOX = nullptr; // will be created in ALoxCamp::Bootstrap
25#endif
26
27#endif
28
29
30// The lox singletons for debug and release logging
33
34 // search
35 for( auto* it : detail::loxes )
36 if( it->GetName().Equals<CHK, lang::Case::Ignore>( name ) )
37 return it;
38
39
40 // create?
41 if ( create == lang::CreateIfNotExists::Yes ) {
42 Lox* newLox= new Lox ( name, false );
43 detail::loxes.emplace_back( newLox );
44 return newLox;
45 }
46
47 // not found
48 return nullptr;
49}
50
53
54 // check
55 if ( lox == nullptr ) {
56 ALIB_ERROR( "ALOX", "Nullptr given" )
57 return;
58 }
59
60 // remove
61 if( operation == lang::ContainerOp::Remove ) {
62 for( auto searchIt= detail::loxes.begin() ; searchIt != detail::loxes.end() ; ++searchIt )
63 if ( *searchIt == lox ) {
64 (void) detail::loxes.erase( searchIt );
65 return;
66 }
67 ALIB_WARNING( "ALOX", "Given lox named \"{}\" could not be found for removal.",
68 lox != nullptr ? lox->GetName() : "<null>" )
69 }
70
71 // insert
72 else {
73 for( auto* it : detail::loxes )
74 if( it->GetName().Equals<NC>( lox->GetName() ) ) {
75 ALIB_ERROR( "ALOX", "Given lox named \"{}\" was already registered. "
76 "Registration ignored.", lox->GetName() )
77 return;
78 }
79 detail::loxes.emplace_back( lox );
80}}
81
82
83Lox::Lox(const NString& name, bool doRegister ) {
85
86 if( doRegister )
88}
89
96
97
98//##################################################################################################
99// Static methods of Lox
100//##################################################################################################
102 //--- check configuration setting "CONSOLE_TYPE" ---
104 if(variable.Define())
105 variable= String( A_CHAR("Default") );
106
107 Substring val = variable;
108 val.Trim();
109 if( val.IsEmpty() ||
110 val.Equals<NC, lang::Case::Ignore>( A_CHAR("Default") ) ) goto DEFAULT;
111
112 if( val.Equals<NC, lang::Case::Ignore>( A_CHAR("Plain") ) ) return new ConsoleLogger ( name );
113 if( val.Equals<NC, lang::Case::Ignore>( A_CHAR("Ansi") ) ) return new AnsiConsoleLogger( name );
114
115 if( val.Equals<NC, lang::Case::Ignore>( A_CHAR("Windows") ) )
116 #if defined( _WIN32 )
117 return new WindowsConsoleLogger( name );
118 #else
119 goto DEFAULT;
120 #endif
121 ALIB_WARNING( "ALOX", "Unrecognized value in config variable \"{}\" = \"{}\".",
122 variable, variable.GetString() )
123
124 DEFAULT:
125 if( variable.Define(variables::Priority::Standard) )
126 variable.GetString().Reset(A_CHAR("Default"));
127
128 #if defined( _WIN32 )
129 // if there is no console window we do not do colors
130 if ( !BASECAMP.HasConsoleWindow )
131 return new ConsoleLogger( name );
132 else
133 return new WindowsConsoleLogger( name );
134 #else
135 return new AnsiConsoleLogger( name );
136 #endif
137}
138
139
140#if ALOX_DBG_LOG
141//##################################################################################################
142// Auto detection of DEBUG environment
143//##################################################################################################
145TextLogger* Log::IDE_LOGGER = nullptr;
146
148 static bool recursion= false;
149 if( recursion )
150 return;
151 recursion= true;
152
153 // block recursion caused by log operations in this code
154 if ( DEBUG_LOGGER != nullptr ) {
155 ALIB_WARNING( "ALOX", "Log::AddDebugLogger(): called twice." )
156 recursion= false;
157 return;
158 }
159 DEBUG_LOGGER= reinterpret_cast<decltype(DEBUG_LOGGER)>(-1);
160
161 // add a VStudio logger if this is a VStudio debug session
162 #if defined(_MSC_VER) && ALIB_DEBUG
163 if( BASECAMP.IsDebuggerPresent() ) {
164 Variable variable= variables::CampVariable( ALOX, Variables::NO_IDE_LOGGER );
165 bool createIDELogger= variable.IsNotDefined() || (variable.GetBool() == false);
166
167 if(createIDELogger) {
168 IDE_LOGGER= new VStudioLogger("IDE_LOGGER");
169
170 // add logger
171 lox->SetVerbosity( IDE_LOGGER, Verbosity::Verbose, "/" );
172 lox->SetVerbosity( IDE_LOGGER, Verbosity::Warning, Lox::InternalDomains );
173 } }
174 #endif
175
176 // add a default console logger
177 DEBUG_LOGGER= Lox::CreateConsoleLogger("DEBUG_LOGGER");
178
179 // add logger by setting verbosities
180 lox->SetVerbosity( DEBUG_LOGGER, Verbosity::Verbose );
181 lox->SetVerbosity( DEBUG_LOGGER, Verbosity::Warning, Lox::InternalDomains );
182
183 // check various variables, if existed already externally. If not, create them empty or
184 // with debug defaults (only done here, namely for debug logger)
185 {ALIB_LOCK_WITH(ALOX.GetConfig())
186 // Verbosity: If not, set 'ExportAll' flag
187 Variable variable= variables::CampVariable( ALOX );
188 Box replacements[2]= { "LOG", "DEBUG_LOGGER" };
189 variable.Declare( Variables::VERBOSITY, replacements );
190 if( variable.IsNotDefined() ) {
191 (void) variable.Define();
192 variable.Get<CVVerbosities>().ExportAll= true;
193 }
194
195 variable.Declare( Variables::SPTR_LOX , "LOG" ); (void) variable.Define();
196 variable.Declare( Variables::DOMAIN_SUBSTITUTION, "LOG" ); (void) variable.Define();
197 variable.Declare( Variables::PREFIXES , "LOG" ); (void) variable.Define();
198 variable.Declare( Variables::DUMP_STATE_ON_EXIT , "LOG" ); (void) variable.Define();
199 if( dynamic_cast<AnsiConsoleLogger*>(DEBUG_LOGGER) != nullptr )
200 variable.Declare( Variables::CONSOLE_LIGHT_COLORS ); (void) variable.Define();
201 }
202
203 // set ALib's assertion plugin with one that uses ALox
204 SetALibAssertionPlugin( lox );
205
206 recursion= false;
207}
208
210 // remove ALox specific assertion plugin of ALib
211 SetALibAssertionPlugin( nullptr );
212
213 // remove debug logger(s)
214 ALIB_ASSERT_WARNING( DEBUG_LOGGER != nullptr, "ALOX",
215 "Log::RemoveDebugLogger(): no debug logger to remove." )
216
217 if ( DEBUG_LOGGER != nullptr ) {
218 lox->RemoveLogger( DEBUG_LOGGER );
219
220 delete DEBUG_LOGGER;
221 DEBUG_LOGGER= nullptr;
222 }
223
224 #if defined(_MSC_VER) && ALIB_DEBUG
225 if ( IDE_LOGGER != nullptr ) {
226 lox->RemoveLogger( IDE_LOGGER );
227
228 delete IDE_LOGGER;
229 IDE_LOGGER= nullptr;
230 }
231 #endif
232}
233#endif // ALOX_DBG_LOG
234
235
236//##################################################################################################
237// ALoxAssertionPlugin
238//##################################################################################################
239#if ALIB_DEBUG
240
241//==================================================================================================
242/// This function will be set to global pointer #"assert::PLUGIN" when calling
243/// method #"Log::AddDebugLogger;*".<br>
244/// If no debug-logging is used, or method #"%AddDebugLogger" is not used, then
245/// this function can also be used with a different #"%Lox" and explicitly activated using
246/// the static method #"Log::SetALibAssertionPlugin;*".
247/// Uses internal domain <b>"/ALIB"</b> for logging, respectively to what the global variable
248/// #"ALOX_ASSERTION_PLUGIN_DOMAIN_PREFIX" is set.
249/// @param ci Information about the scope of invocation.
250/// @param type The type of the message. As a convention, \c 0 is an assertion, \c 1 is a
251/// warning, \c 2 is an info message, \c 3 or above are a verbose messages.
252/// @param domain The domain of the assertion, warning, or message.
253/// Will be appended to the \alox domain.
254/// @param msg The assembled message to print.
255//==================================================================================================
257 int type,
258 std::string_view domain,
259 std::string_view msg );
260
261std::string_view const ALOX_ASSERTION_PLUGIN_DOMAIN_PREFIX = "/ALIB";
262
263#if !DOXYGEN
264namespace { Lox* assertionLox= nullptr; }
265#endif
266
268
269 // remove plugin
270 if ( pLox == nullptr ) {
271 if ( assertionLox == nullptr )
272 return;
273
274 assertionLox->Acquire( ALIB_CALLER );
275 assertionLox->GetLogableContainer().Add( "ALoxAssertionPlugin removed "
276 "from Lox {!Q}", assertionLox->GetName() );
277 assertionLox->Entry( ALOX_ASSERTION_PLUGIN_DOMAIN_PREFIX, Verbosity::Verbose );
278 assertionLox->Release ();
279 assertionLox = nullptr;
280 assert::PLUGIN= nullptr;
281 return;
282 }
283
284 // add plugin
285 assertionLox = pLox;
287 assertionLox->Acquire( ALIB_CALLER );
288 assertionLox->GetLogableContainer().Add( "ALoxAssertionPlugin set to Lox {!Q}.", pLox->GetName() );
289 assertionLox->Entry( ALOX_ASSERTION_PLUGIN_DOMAIN_PREFIX, Verbosity::Verbose );
290
291 // we set the verbosity only now. This should
292 // - allow to have the above verbose message seen once
293 // - in case the values become externalized, this setting is written to such external
294 // configuration file and thus is not displayed a second time.
295 assertionLox->SetVerbosity( DEBUG_LOGGER, Verbosity::Warning,
297 assertionLox->Release ();
298}
299
301 int type,
302 std::string_view domain,
303 std::string_view msg ) {
304 assertionLox->Acquire( ci );
305
306 auto& logables= assertionLox->GetLogableContainer();
307 logables.Add( msg );
308
309 auto verbosity= type == 0 ? Verbosity::Error :
310 type == 1 ? Verbosity::Warning :
311 type == 2 ? Verbosity::Info :
312 Verbosity::Verbose ;
313
314 NString256 dom(ALOX_ASSERTION_PLUGIN_DOMAIN_PREFIX); dom << '/' << domain;
315
316 assertionLox->Entry( dom, verbosity );
317 assertionLox->Release ();
318}
319
320
321#endif //ALIB_DEBUG
322
323}// namespace [alib::lox]
#define ALIB_CALLER
#define A_CHAR(STR)
#define ALIB_WARNING(domain,...)
#define ALIB_ASSERT_WARNING(cond, domain,...)
#define ALIB_ERROR(domain,...)
#define ALIB_LOCK_RECURSIVE_WITH(lock)
#define ALIB_LOCK_WITH(lock)
static textlogger::TextLogger * IDE_LOGGER
An (additional) IDE specific logger, that might be created by AddDebugLogger.
Definition log.hpp:42
static void RemoveDebugLogger(Lox *lox)
Definition alox.cpp:209
static void AddDebugLogger(Lox *lox)
Definition alox.cpp:147
static textlogger::TextLogger * DEBUG_LOGGER
The debug logger created by AddDebugLogger.
Definition log.hpp:39
static void SetALibAssertionPlugin(Lox *lox)
Definition alox.cpp:267
This class acts as a container for Loggers and provides a convenient interface to logging.
Definition lox.hpp:13
~Lox()
Destructs a lox.
Definition alox.cpp:90
Lox(const NString &name, bool doRegister=true)
Definition alox.cpp:83
detail::LoxImpl * impl
The implementation.
Definition lox.hpp:21
static textlogger::TextLogger * CreateConsoleLogger(const NString &name=nullptr)
Definition alox.cpp:101
static constexpr NString InternalDomains
Definition lox.hpp:45
const NString & GetName()
Definition lox.hpp:116
static void Register(Lox *lox, lang::ContainerOp operation)
Definition alox.cpp:51
static Lox * Get(const NString &name, lang::CreateIfNotExists create=lang::CreateIfNotExists::No)
Definition alox.cpp:31
constexpr bool IsEmpty() const
Definition string.hpp:349
bool Equals(const TString< TChar > &rhs) const
Definition string.hpp:515
TSubstring & Trim(const TCString< TChar > &whiteSpaces=CStringConstantsTraits< TChar >::DefaultWhitespaces())
bool Define(Priority requestedPriority=Priority::Standard)
Definition variable.cpp:237
void(* PLUGIN)(const CallerInfo &ci, int type, std::string_view domain, std::string_view msg)
Definition assert.cpp:70
ContainerOp
Denotes standard container operations.
@ Remove
Denotes removals.
@ Insert
Denotes insertions.
CreateIfNotExists
Denotes whether something should be created if it does not exist.
@ Yes
Create if something does not exist.
void shutdownLoxes()
Internal lox management.
integer dbgCountLoxes()
void ALoxAssertionPlugin(const lang::CallerInfo &ci, int type, std::string_view domain, std::string_view msg)
Definition alox.cpp:300
Lox * DEBUG_LOX
std::string_view const ALOX_ASSERTION_PLUGIN_DOMAIN_PREFIX
Definition alox.cpp:261
@ CONSOLE_LIGHT_COLORS
Denotes configuration variable #"alxcvALOX_CONSOLE_LIGHT_COLORS" used by colorful specializations of ...
Definition aloxcamp.hpp:66
@ CONSOLE_TYPE
Denotes configuration variable #"alxcvALOX_CONSOLE_TYPE" used by #"Lox::CreateConsoleLogger;2".
Definition aloxcamp.hpp:24
@ VERBOSITY
Denotes configuration variable #"alxcvALOX_LOGGERNAME_VERBOSITY_WITH_LOXNAME".
Definition aloxcamp.hpp:27
@ DOMAIN_SUBSTITUTION
Denotes configuration variable #"alxcvALOX_LOXNAME_DOMAIN_SUBSTITUTION" used by class #"Lox".
Definition aloxcamp.hpp:36
@ DUMP_STATE_ON_EXIT
Denotes configuration variable #"alxcvALOX_LOXNAME_DUMP_STATE_ON_EXIT" used by class #"Lox".
Definition aloxcamp.hpp:42
@ SPTR_LOX
Denotes configuration variable #"alxcvALOX_LOXNAME_SOURCE_PATH_TRIM_RULES" used by class #"Lox".
Definition aloxcamp.hpp:33
@ PREFIXES
Denotes configuration variable #"alxcvALOX_LOXNAME_PREFIXES" used by class #"Lox".
Definition aloxcamp.hpp:39
RecursiveLock GLOBAL_ALLOCATOR_LOCK
TMonoAllocator< lang::HeapAllocator > GLOBAL_ALLOCATOR
variables::Variable CampVariable(camp::Camp &camp)
Definition camp.hpp:283
strings::TString< nchar > NString
Type alias in namespace #"%alib".
Definition string.hpp:2174
lox::textlogger::TextLogger TextLogger
Type alias in namespace #"%alib".
variables::Variable Variable
Type alias in namespace #"%alib".
containers::List< T, MonoAllocator, TRecycling > ListMA
Type alias in namespace #"%alib".
Definition list.hpp:689
lox::ALoxCamp ALOX
The singleton instance of ALib Camp class #"ALoxCamp".
Definition aloxcamp.cpp:5
lox::loggers::WindowsConsoleLogger WindowsConsoleLogger
Type alias in namespace #"%alib".
lang::integer integer
Type alias in namespace #"%alib".
Definition integers.hpp:149
lox::Lox Lox
Type alias in namespace #"%alib".
Definition lox.hpp:1303
boxing::Box Box
Type alias in namespace #"%alib".
Definition box.hpp:1128
strings::TString< character > String
Type alias in namespace #"%alib".
Definition string.hpp:2165
strings::TSubstring< character > Substring
Type alias in namespace #"%alib".
camp::Basecamp BASECAMP
The singleton instance of ALib Camp class #"Basecamp".
Definition basecamp.cpp:2
lox::loggers::AnsiConsoleLogger AnsiConsoleLogger
Type alias in namespace #"%alib".
NLocalString< 256 > NString256
Type alias name for #"TLocalString;TLocalString<nchar,256>".
lox::loggers::ConsoleLogger ConsoleLogger
Type alias in namespace #"%alib".
See sibling type #"NC".
Definition chk_nc.hpp:30
static void Destruct(LoxImpl *lox)
Definition loxpimpl.cpp:235
static LoxImpl * Construct(const NString &name)
Definition loxpimpl.cpp:225