ALib C++ Library
Library Version: 2412 R0
Documentation generated by doxygen
Loading...
Searching...
No Matches
fmtcallerinfo.cpp
1// #################################################################################################
2// ALib C++ 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
12
13
21# if ALIB_THREADS
23# endif
24#endif // !DOXYGEN
25
26namespace alib::lang::format {
27
28void FFormat_CallerInfo( const Box& box, const String& formatSpec, NumberFormat&, AString& target )
29{
30 FMTCallerInfo fmtCI{*box.Unbox<CallerInfo*>() };
31 fmtCI.Format( formatSpec.IsNotEmpty() ? formatSpec
32 : BASECAMP.GetResource("FMTCI"),
33 target );
34}
35
37{
38 if ( targetData == lang::CurrentData::Clear )
39 target.Reset();
40
41 // this ensures that target is not nulled, as all other appends are NC-versions
42 target._("");
43
44 while ( format.IsNotEmpty() )
45 {
46 // read n equal characters
47 int n= 1;
48 character c= format.ConsumeChar();
49 while ( format.ConsumeChar(c) )
50 ++n;
51
52 switch (c)
53 {
54 case '\'': // single quotes
55 {
56 // one or more pairs of single quotes?
57 if ( n > 1 )
58 {
59 int pairs= n / 2;
60 target.InsertChars<NC>( '\'', pairs );
61 n-= (pairs * 2);
62 }
63
64 // one single quote?
65 if ( n == 1 )
66 {
67 // search end
68 integer end= format.IndexOf( '\'' );
69 if ( end < 1 )
70 {
71 ALIB_WARNING( "ALIB", "Format Error: Missing single Quote" )
72 target << "Format Error: Missing closing single quote character <'>" ;
73 return target;
74 }
75
76 target._<NC>( format, 0, end );
77 format.ConsumeChars<NC>( end + 1 );
78 }
79
80 } break;
81
82 // source information
83 case 's':
84 {
85 if ( n == 1 )
86 {
87 if ( format.ConsumeChar('f') )
88 {
89 if( ci.File ) target._<NC>( ci.File );
90 else target._<NC>( "<NULL>" );
91 break;
92 }
93 if ( format.ConsumeChar('l') )
94 {
95 if( ci.File )
96 target._<NC>( ci.Line );
97 break;
98 }
99 if ( format.ConsumeChar('m') )
100 {
101 if( ci.File )
102 target._<NC>( ci.Func );
103 break;
104 }
105 }
106 // otherwise: copy what was in
107 target.InsertChars<NC>( c, n );
108 break;
109 }
110
111 #if ALIB_THREADS
112 // thread information
113 case 't':
114 {
115 if ( n == 1 )
116 {
117 Thread* thread= Thread::Get(ci.ThreadID);
118
119 // alib thread name
120 if ( format.ConsumeChar('n') )
121 {
122 target._<NC>( thread ? thread->GetName()
123 : BASECAMP.GetResource("FMTCINT") );
124 break;
125 }
126 // alib thread id
127 if ( format.ConsumeChar('i') )
128 {
129 if ( thread ) target._<NC>( thread->GetID() );
130 else target._<NC>( BASECAMP.GetResource("FMTCINR") );
131 break;
132 }
133
134 // native thread id
135 if ( format.ConsumeChar('c') )
136 {
137 if constexpr ( sizeof(std::thread::id) == sizeof(uint16_t) )
138 {
139 uint16_t nativeID= 0;
140 memcpy(&nativeID, &ci.ThreadID, 2);
141 target._<NC>("0x")._<NC>(Format::Hex(nativeID, 4));
142 }
143 if constexpr ( sizeof(std::thread::id) == sizeof(uint32_t) )
144 {
145 uint32_t nativeID= 0;
146 memcpy(&nativeID, &ci.ThreadID, 4);
147 target._<NC>("0x")._<NC>(Format::Hex(nativeID, 8));
148 }
149 if constexpr ( sizeof(std::thread::id) == sizeof(uint64_t) )
150 {
151 uint64_t nativeID= 0;
152 memcpy(&nativeID, &ci.ThreadID, 8);
153 target._<NC>("0x")._<NC>(Format::Hex(nativeID, 16));
154 }
155
156 break;
157 }
158
159 // all/auto
160 if ( format.ConsumeChar('a') )
161 {
162 target._<NC>( ci.ThreadID );
163 break;
164 }
165 }
166 // otherwise: copy what was in
167 target.InsertChars<NC>( c, n );
168 break;
169 }
170 #endif // ALIB_THREADS
171
172 // type information
173 case 'y':
174 {
175 if ( n == 1 )
176 {
177 // full type name
178 if ( format.ConsumeChar('f') )
179 {
180 #if !ALIB_DEBUG
181 target._<NC>( BASECAMP.GetResource("FMTCINY") );
182 #else
183 if (!ci.TypeInfo) target._<NC>( BASECAMP.GetResource("FMTCINY") );
184 else target << lang::DbgTypeDemangler(*ci.TypeInfo).Get();
185 #endif
186 break;
187 }
188
189 // stripped type name
190 if ( format.ConsumeChar('n') )
191 {
192 #if !ALIB_DEBUG
193 target._<NC>( BASECAMP.GetResource("FMTCINY") );
194 #else
195 if (!ci.TypeInfo)
196 target._<NC>( BASECAMP.GetResource("FMTCINY") );
197 else
198 {
199 NString2K typeName;
201 target._<NC>( typeName );
202 }
203 #endif
204 break;
205 }
206
207 // automatic type together with shortened function name
208 if ( format.ConsumeChar('a') )
209 {
210 #if !ALIB_DEBUG
211 target._<NC>( ci.Func )._<NC>("()");
212 #else
213 if (ci.TypeInfo)
214 target._<NC>( *ci.TypeInfo )._<NC>("::");
215 target._<NC>( ci.Func )._<NC>("()");
216 #endif
217 break;
218 }
219
220 }
221 // otherwise: copy what was in
222 target.InsertChars<NC>( c, n );
223 break;
224 }
225
226 default: // otherwise: copy what was in
227 target.InsertChars<NC>( c, n );
228 break;
229 }
230
231 }
232
233 return target;
234}
235
236
237} // namespace [ alib::lang::system]
238
const TUnboxable Unbox() const
const String & GetResource(const NString &name)
ALIB_API NAString & GetShort(NAString &target)
ALIB_API const char * Get()
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 bool IsNotEmpty() const
Definition string.hpp:389
virtual const CString GetName() const
Definition thread.hpp:225
ThreadID GetID() const
Definition thread.hpp:207
static ALIB_API Thread * Get(std::thread::id nativeID)
Definition thread.cpp:321
#define ALIB_WARNING(...)
Definition alib.hpp:1268
void FFormat_CallerInfo(const Box &box, const String &formatSpec, NumberFormat &, AString &target)
platform_specific integer
Definition integers.hpp:43
@ Clear
Chooses to clear existing data.
lang::basecamp::BaseCamp BASECAMP
The singleton instance of ALib Camp class BaseCamp.
Definition basecamp.cpp:70
characters::character character
Type alias in namespace alib.
std::thread::id ThreadID
The ID of the calling thread.
Definition alib.hpp:1136
const char * Func
Definition alib.hpp:1132
const char * File
The name of the source file as given by compiler.
Definition alib.hpp:1130
const std::type_info * TypeInfo
The calling type.
Definition alib.hpp:1138
int Line
The line number within File.
Definition alib.hpp:1131
const lang::CallerInfo & ci
The wrapped caller information.
ALIB_API AString & Format(Substring format, AString &target, lang::CurrentData targetData=lang::CurrentData::Keep) const