ALib C++ Library
Library Version: 2412 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
scopedump.cpp
1// #################################################################################################
2// alib::lox::detail - ALox Logging Library
3//
4// Copyright 2013-2024 A-Worx GmbH, Germany
5// Published under 'Boost Software License' (a free software license, see LICENSE.txt)
6// #################################################################################################
8
9#if !DOXYGEN
10# include "alib/alox/alox.hpp"
11# define HPP_ALIB_LOX_PROPPERINCLUDE
15# undef HPP_ALIB_LOX_PROPPERINCLUDE
16#endif // !DOXYGEN
17
18
19using namespace alib;
20
21
22namespace alib { namespace lox { namespace detail {
23
24//! @cond NO_DOX
25
26// #################################################################################################
27// template instantiations
28// #################################################################################################
29template int ScopeDump::writeStore ( ScopeStore<NString , true >* store, int indentSpaces );
30template int ScopeDump::writeStore ( ScopeStore<PrefixLogable* , true >* store, int indentSpaces );
31template int ScopeDump::writeStoreMap( ScopeStore<SSMap<int>*, false>* store );
32template int ScopeDump::writeStoreMap( ScopeStore<SSMap<Box>*, false>* store );
33
34// #################################################################################################
35// local helper functions (non members)
36// #################################################################################################
37namespace {
38
39template<typename T> void write( const T& val, NAString& target )
40{
41 target._(val);
42}
43
44template<typename T> void write( T* val, NAString& target )
45{
46 // prefix logable?
47 if( std::is_same<T, Box*>::value )
48 {
49 String256 buffer;
50 buffer << '"';
51 integer actLen= buffer.Length();
52 buffer._( *val );
53 ESC::ReplaceToReadable( buffer, actLen );
54 buffer << Format::Escape( lang::Switch::On, actLen );
55 buffer << '"';
56 target << buffer;
57 }
58 else
59 target._(*static_cast<Box*>(val));
60}
61} // anonymous namespace
62
63// #################################################################################################
64// protected methods
65// #################################################################################################
67{
68 integer fileNameEnd= key.IndexOf('#');
69 integer methodEnd= fileNameEnd >= 0 ? key.IndexOf('#', fileNameEnd + 1) : -1;
70
71 targetBuffer._<NC>("Scope::");
72 if ( methodEnd >= 0 ) targetBuffer._<NC>( "Method [" );
73 else if ( fileNameEnd >= 0 ) targetBuffer._<NC>( "FileName [" );
74 else targetBuffer._<NC>( "Path [" );
75
76 integer targetStart= targetBuffer.Length();
77 targetBuffer._<NC>( key );
78
79 if ( methodEnd >= 0 )
80 {
81 targetBuffer.ReplaceSubstring<NC>( " @", targetStart + fileNameEnd +1, 2 ); // characters: "/#"
82 targetBuffer._<NC>( "()" );
83 }
84
85 if ( fileNameEnd >= 0 )
86 targetBuffer.ReplaceSubstring<NC>(".*", targetStart + fileNameEnd, 1);
87 else
88 targetBuffer._('/');
89
90 targetBuffer._(']');
91
92 return targetBuffer;
93}
94
95#if ALIB_THREADS
97{
98 auto it= threadDict.Find( threadID );
99 if ( it != threadDict.end() )
100 return targetBuffer._("[Thread=\"")._( it->second )._("\"]");
101
102 return targetBuffer._("[ThreadID=")._( threadID )._(']');
103}
104#endif
105
106template<typename T>
108{
109 for ( auto& it : map )
110 {
111 targetBuffer._<NC>( prefix );
112
113 String64 keyString;
114
115 if ( it.first.template Equals<NC>( noKey ) )
116 keyString._<NC>( "<global>" );
117 else
118 keyString._<NC>( '"' )._( it.first )._( '"' );
119 if ( maximumKeyLength < keyString.Length() + 1 )
120 maximumKeyLength= keyString.Length() + 1;
121
123
124
125 write( it.second, targetBuffer);
127 }
128 return maximumKeyLength;
129}
130
131// #################################################################################################
132// Interface
133// #################################################################################################
134template<typename T>
135int ScopeDump::writeStoreMap( ScopeStore<T, false>* store )
136{
137 int cnt= 0;
138 bool firstEntry= true;
139 if ( store->globalStore && store->globalStore->Size() > 0)
140 {
141 cnt+= static_cast<int>( store->globalStore->Size() );
142 firstEntry= false;
143 targetBuffer._<NC>( " Scope::Global:" ).NewLine();
144 maximumKeyLength= writeStoreMapHelper( *store->globalStore, " " );
145 }
146
147#if ALIB_THREADS
148 for ( auto thread= store->threadStore.begin() ; thread != store->threadStore.end() ; ++thread )
149 {
150 if ( thread->first.first== false )
151 continue;
152 if( firstEntry ) firstEntry= false; else targetBuffer.NewLine();
153 targetBuffer._<NC>(" Scope::ThreadOuter "); storeThreadToScope( thread->first.second )._( ':' ).NewLine();
154 cnt+= static_cast<int>( thread->second->Size() );
155 maximumKeyLength= writeStoreMapHelper( *thread->second, " " );
156 }
157#endif
158
159
160 String512 keyStr;
162 iterator.SetSorting( lang::Switch::On );
163 iterator.SetPathGeneration( lang::Switch::On );
164 for( iterator.Initialize( store->languageStore) ; iterator.IsValid() ; iterator.Next() )
165 {
166 if( *iterator.Node() == nullptr )
167 continue;
168 cnt+= static_cast<int>( (*iterator.Node())->Size() );
169 if( firstEntry ) firstEntry= false; else targetBuffer.NewLine();
170 targetBuffer._<NC>( " " );
171 storeKeyToScope( iterator.FullPath( keyStr) ).NewLine();
172 maximumKeyLength= writeStoreMapHelper( **iterator.Node(), " " );
173 }
174
175#if ALIB_THREADS
176 for ( auto thread= store->threadStore.begin() ; thread != store->threadStore.end() ; ++thread )
177 {
178 if ( thread->first.first == true )
179 continue;
180 if( firstEntry ) firstEntry= false; else targetBuffer.NewLine();
181 targetBuffer._<NC>(" Scope::ThreadInner "); storeThreadToScope( thread->first.second )._( ':' ).NewLine();
182 cnt+= static_cast<int>( thread->second->Size() );
183 maximumKeyLength= writeStoreMapHelper( *thread->second, " " );
184 }
185#endif
186 return cnt;
187}
188
189template<typename T>
190int ScopeDump::writeStore( ScopeStore<T, true>* store, int indentSpaces )
191{
192 int cnt= 0;
193
194 // global store
195 if ( store->globalStore != nullptr )
196 {
197 ++cnt;
198 targetBuffer.InsertChars( ' ', indentSpaces );
199 write( store->globalStore, targetBuffer );
200 targetBuffer._<NC>(NFormat::Tab( 25, -1 ) )._<NC>( "Scope::Global " ).NewLine();
201 }
202
203 // outer thread store
204#if ALIB_THREADS
205 for ( auto thread= store->threadStore.begin() ; thread != store->threadStore.end() ; ++thread )
206 if( thread->first.first == false )
207 for ( auto& it : thread->second )
208 {
209 ++cnt;
210 targetBuffer.InsertChars( ' ', indentSpaces );
211 write(it, targetBuffer);
212 targetBuffer._<NC>( NFormat::Tab( 25, -1 ) )
213 ._<NC>( "Scope::ThreadOuter " );
214 storeThreadToScope( thread->first.second ).NewLine();
215 }
216#endif
217
218 // language store
219 {
220 String512 keyStr;
222 iterator.SetSorting( lang::Switch::On );
223 iterator.SetPathGeneration( lang::Switch::On );
224 for( iterator.Initialize( store->languageStore ); iterator.IsValid() ; iterator.Next() )
225 {
226 if( *iterator.Node() == nullptr )
227 continue;
228 ++cnt;
229 targetBuffer.InsertChars( ' ', indentSpaces );
230 write( *iterator.Node(), targetBuffer );
231 targetBuffer._<NC>(NFormat::Tab( 25, -1 ) );
232 storeKeyToScope( iterator.FullPath( keyStr) ).NewLine();
233 }
234 }
235
236 // inner thread store
237#if ALIB_THREADS
238 for ( auto thread= store->threadStore.begin() ; thread != store->threadStore.end() ; ++thread )
239 if( thread->first.first == true )
240 for ( auto& it : thread->second )
241 {
242 ++cnt;
243 targetBuffer.InsertChars( ' ', indentSpaces );
244 write(it, targetBuffer);
245 targetBuffer._<NC>( NFormat::Tab( 25, -1 ) )
246 ._<NC>( "Scope::ThreadInner " );
247 storeThreadToScope( thread->first.second ).NewLine();
248 }
249#endif
250 return cnt;
251}
252
253//! @endcond
254}}} // namespace [alib::lox::detail]
255
Iterator Find(const KeyType &key)
static ALIB_API void ReplaceToReadable(AString &target, integer startIdx)
Definition aloxcamp.cpp:413
ALIB_API int writeStoreMap(ScopeStore< T, false > *store)
ALIB_API int writeStore(ScopeStore< T, true > *store, int indentSpaces)
ALIB_API NAString & storeKeyToScope(String key)
integer maximumKeyLength
The maximum length of a key. Adjusts (increases) over life-cycle.
Definition scopedump.inl:42
ALIB_API integer writeStoreMapHelper(SSMap< T > &map, const NString &prefix)
const NString noKey
String to identify global keys.
Definition scopedump.inl:39
ALIB_API NAString & storeThreadToScope(ThreadID threadID)
ScopeInfo::ThreadDictionary & threadDict
User-defined threads names.
Definition scopedump.inl:46
NAString & targetBuffer
The target to write to.
Definition scopedump.inl:36
TAString & ReplaceSubstring(const TString< TChar > &src, integer regionStart, integer regionLength)
TAString & InsertChars(TChar c, integer qty)
TAString & _(const TString< TChar > &src, integer regionStart, integer regionLength=MAX_LEN)
integer IndexOf(TChar needle, integer startIdx=0) const
Definition string.hpp:896
constexpr integer Length() const
Definition string.hpp:326
@ On
Switch it on, switched on, etc.
@ Left
Chooses left alignment.
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 loxpimpl.inl:28
Definition alib.cpp:69
lang::integer integer
Type alias in namespace alib.
Definition integers.hpp:273