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