ALib C++ Library
Library Version: 2510 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
scopedump.cpp
1// #################################################################################################
2// alib::lox::detail - ALox Logging Library
3//
4// Copyright 2013-2025 A-Worx GmbH, Germany
5// Published under 'Boost Software License' (a free software license, see LICENSE.txt)
6// #################################################################################################
7#include "alib_precompile.hpp"
8#if !defined(ALIB_C20_MODULES) || ((ALIB_C20_MODULES != 0) && (ALIB_C20_MODULES != 1))
9# error "Symbol ALIB_C20_MODULES has to be given to the compiler as either 0 or 1"
10#endif
11#if ALIB_C20_MODULES
12 module;
13#endif
14// ====================================== Global Fragment ======================================
16
17// =========================================== Module ==========================================
18#if ALIB_C20_MODULES
19 module ALib.ALox.Impl;
20 import ALib.Lang;
21 import ALib.Strings;
22 import ALib.Boxing;
23 import ALib.EnumRecords;
25 import ALib.Variables;
26 import ALib.Camp;
27 import ALib.Camp.Base;
28#else
29# include "ALib.Lang.H"
30# include "ALib.Strings.H"
31# include "ALib.Boxing.H"
33# include "ALib.Variables.H"
34# include "ALib.Camp.H"
35# include "ALib.Camp.Base.H"
36# include "ALib.Camp.H"
37# include "ALib.Camp.Base.H"
38# include "ALib.ALox.H"
39# include "ALib.ALox.Impl.H"
40#endif
41// ====================================== Implementation =======================================
42namespace alib { namespace lox { namespace detail {
43
44//! @cond NO_DOX
45
46// #################################################################################################
47// template instantiations
48// #################################################################################################
49template int ScopeDump::writeStore ( ScopeStore<NString , true >* store, int indentSpaces );
50template int ScopeDump::writeStore ( ScopeStore<PrefixLogable* , true >* store, int indentSpaces );
51template int ScopeDump::writeStoreMap( ScopeStore<SSMap<int>*, false>* store );
52template int ScopeDump::writeStoreMap( ScopeStore<SSMap<Box>*, false>* store );
53
54// #################################################################################################
55// local helper functions (non members)
56// #################################################################################################
57namespace {
58
59template<typename T> void write( const T& val, NAString& target )
60{
61 target._(val);
62}
63
64template<typename T> void write( T* val, NAString& target )
65{
66 // prefix logable?
67 if( std::is_same<T, Box*>::value )
68 {
69 String256 buffer;
70 buffer << '"';
71 integer actLen= buffer.Length();
72 buffer._( *val );
73 ESC::ReplaceToReadable( buffer, actLen );
74 buffer << Escape( lang::Switch::On, actLen );
75 buffer << '"';
76 target << buffer;
77 }
78 else
79 target._(*static_cast<Box*>(val));
80}
81} // anonymous namespace
82
83// #################################################################################################
84// protected methods
85// #################################################################################################
87{
88 integer fileNameEnd= key.IndexOf('#');
89 integer methodEnd= fileNameEnd >= 0 ? key.IndexOf('#', fileNameEnd + 1) : -1;
90
91 targetBuffer._<NC>("Scope::");
92 if ( methodEnd >= 0 ) targetBuffer._<NC>( "Method [" );
93 else if ( fileNameEnd >= 0 ) targetBuffer._<NC>( "FileName [" );
94 else targetBuffer._<NC>( "Path [" );
95
96 integer targetStart= targetBuffer.Length();
97 targetBuffer._<NC>( key );
98
99 if ( methodEnd >= 0 )
100 {
101 targetBuffer.ReplaceSubstring<NC>( " @", targetStart + fileNameEnd +1, 2 ); // characters: "/#"
102 targetBuffer._<NC>( "()" );
103 }
104
105 if ( fileNameEnd >= 0 )
106 targetBuffer.ReplaceSubstring<NC>(".*", targetStart + fileNameEnd, 1);
107 else
108 targetBuffer._('/');
109
110 targetBuffer._(']');
111
112 return targetBuffer;
113}
114
115#if !ALIB_SINGLE_THREADED
117{
118 auto it= threadDict.Find( threadID );
119 if ( it != threadDict.end() )
120 return targetBuffer._("[Thread=\"")._( it->second )._("\"]");
121
122 return targetBuffer._("[ThreadID=")._( threadID )._(']');
123}
124#endif
125
126template<typename T>
128{
129 for ( auto& it : map )
130 {
131 targetBuffer._<NC>( prefix );
132
133 String64 keyString;
134
135 if ( it.first.template Equals<NC>( noKey ) )
136 keyString._<NC>( "<global>" );
137 else
138 keyString._<NC>( '"' )._( it.first )._( '"' );
139 if ( maximumKeyLength < keyString.Length() + 1 )
140 maximumKeyLength= keyString.Length() + 1;
141
142 targetBuffer._<NC>(NField(keyString, maximumKeyLength, lang::Alignment::Left))._<NC>( '=' );
143
144
145 write( it.second, targetBuffer);
146 targetBuffer.NewLine();
147 }
148 return maximumKeyLength;
149}
150
151// #################################################################################################
152// Interface
153// #################################################################################################
154template<typename T>
156{
157 int cnt= 0;
158 bool firstEntry= true;
159 if ( store->globalStore && store->globalStore->Size() > 0)
160 {
161 cnt+= int( store->globalStore->Size() );
162 firstEntry= false;
163 targetBuffer._<NC>( " Scope::Global:" ).NewLine();
164 maximumKeyLength= writeStoreMapHelper( *store->globalStore, " " );
165 }
166
167#if !ALIB_SINGLE_THREADED
168 for ( auto thread= store->threadStore.begin() ; thread != store->threadStore.end() ; ++thread )
169 {
170 if ( thread->first.first== false )
171 continue;
172 if( firstEntry ) firstEntry= false; else targetBuffer.NewLine();
173 targetBuffer._<NC>(" Scope::ThreadOuter "); storeThreadToScope( thread->first.second )._( ':' ).NewLine();
174 cnt+= int( thread->second->Size() );
175 maximumKeyLength= writeStoreMapHelper( *thread->second, " " );
176 }
177#endif
178
179
180 String512 keyStr;
182 iterator.SetSorting( lang::Switch::On );
183 iterator.SetPathGeneration( lang::Switch::On );
184 for( iterator.Initialize( store->languageStore) ; iterator.IsValid() ; iterator.Next() )
185 {
186 if( *iterator.Node() == nullptr )
187 continue;
188 cnt+= int( (*iterator.Node())->Size() );
189 if( firstEntry ) firstEntry= false; else targetBuffer.NewLine();
190 targetBuffer._<NC>( " " );
191 storeKeyToScope( iterator.FullPath( keyStr) ).NewLine();
192 maximumKeyLength= writeStoreMapHelper( **iterator.Node(), " " );
193 }
194
195#if !ALIB_SINGLE_THREADED
196 for ( auto thread= store->threadStore.begin() ; thread != store->threadStore.end() ; ++thread )
197 {
198 if ( thread->first.first == true )
199 continue;
200 if( firstEntry ) firstEntry= false; else targetBuffer.NewLine();
201 targetBuffer._<NC>(" Scope::ThreadInner "); storeThreadToScope( thread->first.second )._( ':' ).NewLine();
202 cnt+= int( thread->second->Size() );
203 maximumKeyLength= writeStoreMapHelper( *thread->second, " " );
204 }
205#endif
206 return cnt;
207}
208
209template<typename T>
210int ScopeDump::writeStore( ScopeStore<T, true>* store, int indentSpaces )
211{
212 int cnt= 0;
213
214 // global store
215 if ( store->globalStore != nullptr )
216 {
217 ++cnt;
218 targetBuffer.InsertChars( ' ', indentSpaces );
219 write( store->globalStore, targetBuffer );
220 targetBuffer._<NC>(NTab( 25, -1 ) )._<NC>( "Scope::Global " ).NewLine();
221 }
222
223 // outer thread store
224#if !ALIB_SINGLE_THREADED
225 for ( auto thread= store->threadStore.begin() ; thread != store->threadStore.end() ; ++thread )
226 if( thread->first.first == false )
227 for ( auto& it : thread->second )
228 {
229 ++cnt;
230 targetBuffer.InsertChars( ' ', indentSpaces );
231 write(it, targetBuffer);
232 targetBuffer._<NC>( NTab( 25, -1 ) )
233 ._<NC>( "Scope::ThreadOuter " );
234 storeThreadToScope( thread->first.second ).NewLine();
235 }
236#endif
237
238 // language store
239 {
240 String512 keyStr;
242 iterator.SetSorting( lang::Switch::On );
243 iterator.SetPathGeneration( lang::Switch::On );
244 for( iterator.Initialize( store->languageStore ); iterator.IsValid() ; iterator.Next() )
245 {
246 if( *iterator.Node() == nullptr )
247 continue;
248 ++cnt;
249 targetBuffer.InsertChars( ' ', indentSpaces );
250 write( *iterator.Node(), targetBuffer );
251 targetBuffer._<NC>(NTab( 25, -1 ) );
252 storeKeyToScope( iterator.FullPath( keyStr) ).NewLine();
253 }
254 }
255
256 // inner thread store
257#if !ALIB_SINGLE_THREADED
258 for ( auto thread= store->threadStore.begin() ; thread != store->threadStore.end() ; ++thread )
259 if( thread->first.first == true )
260 for ( auto& it : thread->second )
261 {
262 ++cnt;
263 targetBuffer.InsertChars( ' ', indentSpaces );
264 write(it, targetBuffer);
265 targetBuffer._<NC>( NTab( 25, -1 ) )
266 ._<NC>( "Scope::ThreadInner " );
267 storeThreadToScope( thread->first.second ).NewLine();
268 }
269#endif
270 return cnt;
271}
272
273//! @endcond
274}}} // namespace [alib::lox::detail]
275
static ALIB_DLL void ReplaceToReadable(AString &target, integer startIdx)
Definition aloxinit.cpp:48
ALIB_DLL NAString & storeThreadToScope(ThreadID threadID)
ALIB_DLL NAString & storeKeyToScope(String key)
NAString & targetBuffer
The target to write to.
Definition scopedump.inl:24
ALIB_DLL int writeStore(ScopeStore< T, true > *store, int indentSpaces)
ScopeInfo::ThreadDictionary & threadDict
User-defined threads names.
Definition scopedump.inl:34
ALIB_DLL integer writeStoreMapHelper(SSMap< T > &map, const NString &prefix)
ALIB_DLL int writeStoreMap(ScopeStore< T, false > *store)
const NString noKey
String to identify global keys.
Definition scopedump.inl:27
integer maximumKeyLength
The maximum length of a key. Adjusts (increases) over life-cycle.
Definition scopedump.inl:30
TAString & _(const TAppendable &src)
integer IndexOf(TChar needle, integer startIdx=0) const
Definition string.inl:844
@ Left
Chooses left alignment.
@ On
Switch it on, switched on, etc.
HashMap< PoolAllocator, NString, T, std::hash< NString >, std::equal_to< NString >, lang::Caching::Enabled, Recycling::None > SSMap
Shortcut to the ScopeStore's hashmap.
integer ThreadID
The ALib thread identifier type.
Definition thread.inl:23
strings::TEscape< character > Escape
Type alias in namespace alib.
Definition format.inl:536
LocalString< 512 > String512
Type alias name for TLocalString<character,512>.
LocalString< 256 > String256
Type alias name for TLocalString<character,256>.
LocalString< 64 > String64
Type alias name for TLocalString<character,64>.
lang::integer integer
Type alias in namespace alib.
Definition integers.inl:149
strings::TString< nchar > NString
Type alias in namespace alib.
Definition string.inl:2390
strings::TAString< nchar, lang::HeapAllocator > NAString
Type alias in namespace alib.
strings::TTab< nchar > NTab
Type alias in namespace alib.
Definition format.inl:519
boxing::Box Box
Type alias in namespace alib.
Definition box.inl:1216
strings::TField< nchar > NField
Type alias in namespace alib.
strings::TString< character > String
Type alias in namespace alib.
Definition string.inl:2381