Stroika Library 3.0d23
 
Loading...
Searching...
No Matches
Trace.inl
1
2/*
3 * Copyright(c) Sophist Solutions, Inc. 1990-2026. All rights reserved
4 */
5
6#include <mutex>
7
8CompileTimeFlagChecker_HEADER (Stroika::Foundation::Debug, qTraceToFile, qStroika_Foundation_Debug_TraceToFile);
9CompileTimeFlagChecker_HEADER (Stroika::Foundation::Debug, qDefaultTracingOn, qStroika_Foundation_Debug_DefaultTracingOn);
10
13
14namespace Stroika::Foundation::Debug {
15
16 namespace Private_ {
17 struct ModuleInit_ final {
18 ModuleInit_ () noexcept;
19 ~ModuleInit_ ();
20 };
21 const inline ModuleInit_ _MI_;
22 }
23
24 class Private_::Emitter final {
25 public:
26 Emitter () = default;
27 Emitter (const Emitter&) = delete;
28
29 public:
30 static Emitter& Get () noexcept;
31
32 public:
33 /**
34 * \note DbgTrace() is NOT a cancelation point, so you can call this freely without worrying about Throw (ThreadAbortException) etc
35 */
36 [[deprecated ("Since Stroika v3.0d6 - use _f format strings")]] void EmitTraceMessage (const char* format, ...) noexcept;
37 [[deprecated ("Since Stroika v3.0d6 - use _f format strings")]] void EmitTraceMessage (const wchar_t* format, ...) noexcept;
38
39 template <typename CHAR_T, Common::StdCompat::formattable<wchar_t>... Args>
40 nonvirtual void EmitTraceMessage (Characters::FormatString<CHAR_T> fmt, Args&&... args) noexcept
41 {
42 try {
43 EmitTraceMessage_ (fmt.get (), Common::StdCompat::make_wformat_args (args...));
44 }
45 catch (...) {
46 }
47 }
48
49 private:
50 nonvirtual TraceLastBufferedWriteTokenType EmitTraceMessage_ (size_t bufferLastNChars, wstring_view format,
51 Common::StdCompat::wformat_args&& args) noexcept;
52 nonvirtual void EmitTraceMessage_ (wstring_view format, Common::StdCompat::wformat_args&& args) noexcept;
53 nonvirtual void EmitTraceMessage_ (string_view format, Common::StdCompat::format_args&& args) noexcept;
54 nonvirtual void EmitTraceMessage_ (const wstring& raw) noexcept;
55
56 public:
57 // if the last write matches the given token (no writes since then) and the timestamp is unchanged, abandon
58 // the buffered characters and return true. Else flush(write) them, and return false.
59 nonvirtual bool UnputBufferedCharactersForMatchingToken (TraceLastBufferedWriteTokenType token);
60
61 template <typename CHARTYPE>
62 nonvirtual void EmitUnadornedText (const CHARTYPE* p);
63
64 private:
65 // This is the same as EmitTraceMessage_ - but it takes a plain string - and assumes the caller does any 'sprintf' stuff...
66 template <typename CHARTYPE>
67 nonvirtual TraceLastBufferedWriteTokenType DoEmitMessage_ (size_t bufferLastNChars, const CHARTYPE* p, const CHARTYPE* e);
68
69 private:
70 size_t fLastNCharBufCharCount_{0}; // len of valid data in fLastNCharBuf_CHAR_ or fLastNCharBuf_WCHAR_
71 char fLastNCharBuf_CHAR_[10]; // always filled in before used, so no need to initialize - NOT nul-terminated(see fLastNCharBufCharCount_)
72 wchar_t fLastNCharBuf_WCHAR_[10];
73 bool fLastNCharBuf_WCHARFlag_{false}; // determines (if fLastNCharBufCharCount_!=0) which buffer CHAR or WCHAR to use
74 TraceLastBufferedWriteTokenType fLastNCharBuf_Token_{0};
75 Time::DisplayedRealtimeClock::time_point fLastNCharBuf_WriteTickcount_{};
76
77 nonvirtual void BufferNChars_ (size_t nChars, const char* p);
78 nonvirtual void BufferNChars_ (size_t nChars, const wchar_t* p);
79
80 private:
81 nonvirtual void FlushBufferedCharacters_ ();
82
83 private:
84 nonvirtual void DoEmit_ (const char* p) noexcept;
85 nonvirtual void DoEmit_ (const wchar_t* p) noexcept;
86 nonvirtual void DoEmit_ (const char* p, const char* e) noexcept;
87 nonvirtual void DoEmit_ (const wchar_t* p, const wchar_t* e) noexcept;
88
89 private:
90 friend class Debug::TraceContextBumper;
91 };
92
93 /*
94 ********************************************************************************
95 ******************************* TraceContextBumper *****************************
96 ********************************************************************************
97 */
99 {
100#if qStroika_Foundation_Debug_DefaultTracingOn
101 IncCount_ ();
102#endif
103 }
104 template <typename CHAR_T>
105 inline TraceContextBumper::TraceContextBumper ([[maybe_unused]] const CHAR_T* contextName) noexcept
106 requires (same_as<CHAR_T, char> or same_as<CHAR_T, wchar_t>)
107#if qStroika_Foundation_Debug_DefaultTracingOn
108 : TraceContextBumper{cvt2WChartArrayTrunc_ (contextName)}
109#endif
110 {
111 }
112 template <typename CHAR_T, typename FCHAR_T, typename... ARGS>
113 TraceContextBumper::TraceContextBumper ([[maybe_unused]] const CHAR_T* contextName,
114 [[maybe_unused]] Characters::FormatString<FCHAR_T> fmt, [[maybe_unused]] ARGS&&... args) noexcept
115 requires ((same_as<CHAR_T, char> or same_as<CHAR_T, wchar_t>) and (same_as<FCHAR_T, char> or same_as<FCHAR_T, wchar_t>))
116#if qStroika_Foundation_Debug_DefaultTracingOn
117 : TraceContextBumper{cvt2WChartArrayTrunc_ (contextName), ProcessFmtString_ (fmt, forward<ARGS> (args)...)}
118#endif
119 {
120 }
121#if !qStroika_Foundation_Debug_DefaultTracingOn
122 inline TraceContextBumper::TraceContextBumper ([[maybe_unused]] const wchar_t* contextName, [[maybe_unused]] const wchar_t* extraFmt, ...) noexcept
123 {
124 }
125#endif
126#if qStroika_Foundation_Debug_DefaultTracingOn
127 template <typename CHAR_T, typename... ARGS>
128 auto TraceContextBumper::ProcessFmtString_ (Characters::FormatString<CHAR_T> fmt, ARGS&&... args) noexcept -> CHAR_ARRAY_T
129 requires (same_as<CHAR_T, char> or same_as<CHAR_T, wchar_t>)
130 {
131 try {
132 wstring r = Common::StdCompat::vformat (qStroika_Foundation_Characters_FMT_PREFIX_::wstring_view{fmt.get ()},
133 Common::StdCompat::make_wformat_args (args...));
134 size_t len = min<size_t> (r.size (), kMaxContextNameLen_);
135 CHAR_ARRAY_T result;
136 // Dont call Memory::Span here - but reproduce in loop to avoid deadly embrace
137 char_traits<wchar_t>::copy (result.data (), r.data (), len);
138 // for (size_t i = 0; i < len; ++i) {
139 // result[i] = static_cast<wchar_t> (r[i]);
140 // }
141 result[len] = '\0'; //char_traits::copy doesn't appear to NUL-terminate
142 return result;
143 }
144 catch (...) {
145 // we are already deep in the guts of DbgTrace stuff - so not much chance of 'logging' this issue ;-)
146 return {};
147 }
148 }
149 template <typename CHAR_T>
150 auto TraceContextBumper::cvt2WChartArrayTrunc_ (span<const CHAR_T> contextName) -> CHAR_ARRAY_T
151 {
152 // Return item with max size kMaxContextNameLen_+1 so we can tell if we need to add ellipsis
153 array<wchar_t, kMaxContextNameLen_ + 1> r;
154 auto ci = contextName.begin ();
155 for (; ci != contextName.end (); ++ci) {
156 size_t i = ci - contextName.begin ();
157 if (i < kMaxContextNameLen_) {
158 r[i] = *ci;
159 }
160 else {
161 break;
162 }
163 }
164 Assert (ci - contextName.begin () <= kMaxContextNameLen_);
165 r[ci - contextName.begin ()] = '\0';
166 return r;
167 }
168 template <typename CHAR_T>
169 auto TraceContextBumper::cvt2WChartArrayTrunc_ (const CHAR_T* contextName) -> CHAR_ARRAY_T
170 {
171 return cvt2WChartArrayTrunc_ (span{contextName, char_traits<CHAR_T>::length (contextName)});
172 }
173#endif
174
175 /*
176 ********************************************************************************
177 ******************************* TraceContextBumper *****************************
178 ********************************************************************************
179 */
180 inline TraceContextSuppressor::TraceContextSuppressor () noexcept
181 {
182 ++tSuppressCnt_;
183 }
184 inline TraceContextSuppressor::~TraceContextSuppressor ()
185 {
186 --tSuppressCnt_;
187 }
188 inline bool TraceContextSuppressor::GetSuppressTraceInThisThread ()
189 {
190 return tSuppressCnt_ > 0;
191 }
192
193}
#define CompileTimeFlagChecker_HEADER(NS_PREFIX, NAME, VALUE)
CompileTimeFlagChecker_HEADER () will generate a LINK ERROR if you ever compile a header with one val...
#define qStroika_Foundation_Debug_TraceToFile
Definition Trace.h:46
Roughly equivalent to std::wformat_string, except that it can be constructed from 'char' string,...