ALib C++ Library
Library Version: 2402 R1
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 !defined(ALIB_DOX)
10# if !defined(HPP_ALIB_ALOX)
11# include "alib/alox/alox.hpp"
12# endif
13
14# define HPP_ALIB_LOX_PROPPERINCLUDE
15# if !defined (HPP_ALOX_DETAIL_SCOPE)
17# endif
18# if !defined (HPP_ALOX_DETAIL_SCOPEINFO)
20# endif
22# undef HPP_ALIB_LOX_PROPPERINCLUDE
23
24# if !defined (HPP_ALIB_STRINGS_FORMAT)
26# endif
27#endif // !defined(ALIB_DOX)
28
29
30using namespace alib;
31
32
33namespace alib { namespace lox { namespace detail {
34
35//! @cond NO_DOX
36
37// #################################################################################################
38// template instantiations
39// #################################################################################################
40template int ScopeDump::writeStore ( ScopeStore<NString , true >* store, int indentSpaces );
41template int ScopeDump::writeStore ( ScopeStore<PrefixLogable* , true >* store, int indentSpaces );
42template int ScopeDump::writeStoreMap( ScopeStore<std::map<NString, int>*, false>* store );
43template int ScopeDump::writeStoreMap( ScopeStore<std::map<NString, Box>*, false>* store );
44
45// #################################################################################################
46// local helper functions (non members)
47// #################################################################################################
48namespace {
49
50template<typename T> void write( const T& val, NAString& target )
51{
52 target._(val);
53}
54
55template<typename T> void write( T* val, NAString& target )
56{
57 // prefix logable?
58 if( std::is_same<T, Box*>::value )
59 {
60 String256 buffer;
61 buffer << '"';
62 integer actLen= buffer.Length();
63 buffer._( *val );
64 ESC::ReplaceToReadable( buffer, actLen );
65 buffer << Format::Escape( lang::Switch::On, actLen );
66 buffer << '"';
67 target << buffer;
68 }
69 else
70 target._(*static_cast<Box*>(val));
71}
72} // anonymous namespace
73
74// #################################################################################################
75// protected methods
76// #################################################################################################
78{
79 integer fileNameEnd= key.IndexOf('#');
80 integer methodEnd= fileNameEnd >= 0 ? key.IndexOf('#', fileNameEnd + 1) : -1;
81
82 targetBuffer._<false>("Scope::");
83 if ( methodEnd >= 0 ) targetBuffer._<false>( "Method [" );
84 else if ( fileNameEnd >= 0 ) targetBuffer._<false>( "FileName [" );
85 else targetBuffer._<false>( "Path [" );
86
87 integer targetStart= targetBuffer.Length();
88 targetBuffer._<false>( key );
89
90 if ( methodEnd >= 0 )
91 {
92 targetBuffer.ReplaceSubstring<false>( " @", targetStart + fileNameEnd +1, 2 ); // characters: "/#"
93 targetBuffer._<false>( "()" );
94 }
95
96 if ( fileNameEnd >= 0 )
97 targetBuffer.ReplaceSubstring<false>(".*", targetStart + fileNameEnd, 1);
98 else
99 targetBuffer._('/');
100
101 targetBuffer._(']');
102
103 return targetBuffer;
104}
105
106#if ALIB_THREADS
108{
109 auto it= threadDict.Find( threadID );
110 if ( it != threadDict.end() )
111 return targetBuffer._("[Thread=\"")._( it->second )._("\"]");
112
113 return targetBuffer._("[ThreadID=")._( threadID )._(']');
114}
115#endif
116
117template<typename T>
118integer ScopeDump::writeStoreMapHelper( std::map<NString, T>& map, const NString& prefix )
119{
120 for ( auto& it : map )
121 {
122 targetBuffer._<false>( prefix );
123
124 String64 keyString;
125
126 if ( it.first.template Equals<false>( noKey ) )
127 keyString._<false>( "<global>" );
128 else
129 keyString._<false>( '"' )._( it.first )._( '"' );
130 if ( maximumKeyLength < keyString.Length() + 1 )
131 maximumKeyLength= keyString.Length() + 1;
132
133 targetBuffer._<false>(NFormat::Field(keyString, maximumKeyLength, lang::Alignment::Left))._<false>( '=' );
134
135
136 write( it.second, targetBuffer);
138 }
139 return maximumKeyLength;
140}
141
142// #################################################################################################
143// Interface
144// #################################################################################################
145template<typename T>
146int ScopeDump::writeStoreMap( ScopeStore<T, false>* store )
147{
148 int cnt= 0;
149 bool firstEntry= true;
150 if ( store->globalStore && store->globalStore->size() > 0)
151 {
152 cnt+= static_cast<int>( store->globalStore->size() );
153 firstEntry= false;
154 targetBuffer._<false>( " Scope::Global:" ).NewLine();
155 maximumKeyLength= writeStoreMapHelper( *store->globalStore, " " );
156 }
157
158#if ALIB_THREADS
159 for ( auto thread= store->threadStore.begin() ; thread != store->threadStore.end() ; ++thread )
160 {
161 if ( thread->first.first== false )
162 continue;
163 if( firstEntry ) firstEntry= false; else targetBuffer.NewLine();
164 targetBuffer._<false>(" Scope::ThreadOuter "); storeThreadToScope( thread->first.second )._( ':' ).NewLine();
165 cnt+= static_cast<int>( thread->second->size() );
166 maximumKeyLength= writeStoreMapHelper( *thread->second, " " );
167 }
168#endif
169
170
171 String512 keyStr;
173 iterator.SetSorting( lang::Switch::On );
174 iterator.SetPathGeneration( lang::Switch::On );
175 for( iterator.Initialize( store->languageStore) ; iterator.IsValid() ; iterator.Next() )
176 {
177 if( iterator.Node().Value() == ScopeStoreType<T>::NullValue() )
178 continue;
179 cnt+= static_cast<int>( iterator.Node().Value()->size() );
180 if( firstEntry ) firstEntry= false; else targetBuffer.NewLine();
181 targetBuffer._<false>( " " );
182 storeKeyToScope( iterator.FullPath( keyStr) ).NewLine();
183 maximumKeyLength= writeStoreMapHelper( *iterator.Node().Value(), " " );
184 }
185
186#if ALIB_THREADS
187 for ( auto thread= store->threadStore.begin() ; thread != store->threadStore.end() ; ++thread )
188 {
189 if ( thread->first.first == true )
190 continue;
191 if( firstEntry ) firstEntry= false; else targetBuffer.NewLine();
192 targetBuffer._<false>(" Scope::ThreadInner "); storeThreadToScope( thread->first.second )._( ':' ).NewLine();
193 cnt+= static_cast<int>( thread->second->size() );
194 maximumKeyLength= writeStoreMapHelper( *thread->second, " " );
195 }
196#endif
197 return cnt;
198}
199
200template<typename T>
201int ScopeDump::writeStore( ScopeStore<T, true>* store, int indentSpaces )
202{
203 int cnt= 0;
204
205 // global store
206 if ( !ScopeStoreType<T>::IsNull(store->globalStore) )
207 {
208 ++cnt;
209 targetBuffer.InsertChars( ' ', indentSpaces );
210 write( store->globalStore, targetBuffer );
211 targetBuffer._<false>(NFormat::Tab( 25, -1 ) )._<false>( "Scope::Global " ).NewLine();
212 }
213
214 // outer thread store
215#if ALIB_THREADS
216 for ( auto thread= store->threadStore.begin() ; thread != store->threadStore.end() ; ++thread )
217 if( thread->first.first == false )
218 for ( auto& it : thread->second )
219 {
220 ++cnt;
221 targetBuffer.InsertChars( ' ', indentSpaces );
222 write(it, targetBuffer);
223 targetBuffer._<false>( NFormat::Tab( 25, -1 ) )
224 ._<false>( "Scope::ThreadOuter " );
225 storeThreadToScope( thread->first.second ).NewLine();
226 }
227#endif
228
229 // language store
230 {
231 String512 keyStr;
233 iterator.SetSorting( lang::Switch::On );
234 iterator.SetPathGeneration( lang::Switch::On );
235 for( iterator.Initialize( store->languageStore ); iterator.IsValid() ; iterator.Next() )
236 {
237 if( iterator.Node().Value() == ScopeStoreType<T>::NullValue() )
238 continue;
239 ++cnt;
240 targetBuffer.InsertChars( ' ', indentSpaces );
241 write( iterator.Node().Value(), targetBuffer );
242 targetBuffer._<false>(NFormat::Tab( 25, -1 ) );
243 storeKeyToScope( iterator.FullPath( keyStr) ).NewLine();
244 }
245 }
246
247 // inner thread store
248#if ALIB_THREADS
249 for ( auto thread= store->threadStore.begin() ; thread != store->threadStore.end() ; ++thread )
250 if( thread->first.first == true )
251 for ( auto& it : thread->second )
252 {
253 ++cnt;
254 targetBuffer.InsertChars( ' ', indentSpaces );
255 write(it, targetBuffer);
256 targetBuffer._<false>( NFormat::Tab( 25, -1 ) )
257 ._<false>( "Scope::ThreadInner " );
258 storeThreadToScope( thread->first.second ).NewLine();
259 }
260#endif
261 return cnt;
262}
263
264//! @endcond
265}}} // namespace [alib::lox::detail]
static ALIB_API void ReplaceToReadable(AString &target, integer startIdx)
ALIB_API int writeStoreMap(ScopeStore< T, false > *store)
ALIB_API int writeStore(ScopeStore< T, true > *store, int indentSpaces)
ALIB_API NAString & storeKeyToScope(String key)
ALIB_API NAString & storeThreadToScope(ThreadID threadID)
ScopeInfo::ThreadDictionary & threadDict
Definition scopedump.inl:49
ALIB_API integer writeStoreMapHelper(std::map< NString, T > &map, const NString &prefix)
static ALIB_API bool IsNull(T value)
Iterator Find(const KeyType &key)
TAString & ReplaceSubstring(const TString< TChar > &src, integer regionStart, integer regionLength)
Definition astring.hpp:1708
TAString & InsertChars(TChar c, integer qty)
Definition astring.hpp:1405
TAString & _(const TString< TChar > &src, integer regionStart, integer regionLength=MAX_LEN)
Definition astring.hpp:1056
integer IndexOf(TChar needle, integer startIdx=0) const
Definition string.hpp:889
constexpr integer Length() const
Definition string.hpp:357
@ On
Switch it on, switched on, etc.
@ Left
Chooses left alignment.
integer ThreadID
Definition loxpimpl.inl:34
Definition alib.cpp:57
constexpr CString NewLine()
Definition cstring.hpp:528
lang::integer integer
Type alias in namespace alib.
Definition integers.hpp:286