xtd - Reference Guide  0.1.2
Modern c++17/20 framework to create console, GUI and unit test applications on Windows, macOS, Linux, iOS and android.
Loading...
Searching...
No Matches
trace_listener.h
Go to the documentation of this file.
1
4#pragma once
5#include <memory>
6#include <stdexcept>
7#include "../object.h"
8#include "../ustring.h"
9#include "trace_event_cache.h"
10#include "trace_event_type.h"
11#include "trace_options.h"
12
14namespace xtd {
16 namespace diagnostics {
28 class trace_listener : public object {
29 public:
37 trace_listener() = default;
38
40 trace_listener(const trace_listener& tl) = delete;
41 trace_listener& operator=(const trace_listener& tl) = delete;
44
48
52 unsigned int indent_level() const;
53
57 void indent_level(unsigned int indent_level);
58
62 unsigned int indent_size() const;
63
67 void indent_size(unsigned int indent_size);
68
72 virtual bool is_thread_safe() const;
73
77 const xtd::ustring& name() const;
78
82 void name(const xtd::ustring& name);
83
92
101
104 virtual void close();
105
109 virtual void fail(const xtd::ustring& message) {
110#if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
111 write_line(xtd::ustring::format("Fail: {}", message));
112#endif
113 }
114
119 virtual void fail(const xtd::ustring& message, const xtd::ustring& detail_message) {
120#if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
121 write_line(xtd::ustring::format("Fail: {} {}", message, detail_message));
122#endif
123 }
124
126 virtual void flush() {}
127
136 template<typename object>
137 void trace_data(const xtd::diagnostics::trace_event_cache& event_cache, const xtd::ustring& source, const xtd::diagnostics::trace_event_type& event_type, int id, const object& data) {
138#if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
139 write_line(xtd::ustring::format("{} {}: {} : {}", source, event_type, id, data));
140 write_event_cache(event_cache);
141#endif
142 }
143
152 template<typename object>
153 void trace_data(const xtd::diagnostics::trace_event_cache& event_cache, const xtd::ustring& source, const xtd::diagnostics::trace_event_type& event_type, int id, const std::vector<object>& data) {
154#if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
155 write_line(xtd::ustring::format("{} {}: {} : {}", source, event_type, id, xtd::ustring::join(", ", data)));
156 write_event_cache(event_cache);
157#endif
158 }
159
168 template<typename ...objects>
169 void trace_data(const xtd::diagnostics::trace_event_cache& event_cache, const xtd::ustring& source, const xtd::diagnostics::trace_event_type& event_type, int id, const objects& ... data) {
170#if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
171 write_line(xtd::ustring::format("{} {}: {} : {}", source, event_type, id, xtd::ustring::join(", ", {data...})));
172 write_event_cache(event_cache);
173#endif
174 }
175
183 virtual void trace_event(const xtd::diagnostics::trace_event_cache& event_cache, const xtd::ustring& source, const xtd::diagnostics::trace_event_type& event_type, int id) {
184#if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
185 write_line(xtd::ustring::format("{} {}: {}", source, event_type, id));
186 write_event_cache(event_cache);
187#endif
188 }
189
198 virtual void trace_event(const xtd::diagnostics::trace_event_cache& event_cache, const xtd::ustring& source, const xtd::diagnostics::trace_event_type& event_type, int id, const xtd::ustring& message) {
199#if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
200 write_line(xtd::ustring::format("{} {}: {} : {}", source, event_type, id, message));
201 write_event_cache(event_cache);
202#endif
203 }
204
214 template<typename ...objects>
215 void trace_event(const xtd::diagnostics::trace_event_cache& event_cache, const xtd::ustring& source, const xtd::diagnostics::trace_event_type& event_type, int id, const xtd::ustring& format, const objects& ... args) {
216#if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
217 write_line(xtd::ustring::format("{} {}: {} : {}", source, event_type, id, xtd::ustring::format(format, args...)));
218 write_event_cache(event_cache);
219#endif
220 }
221
231 template<typename activity_id_type>
232 void trace_transfer(const xtd::diagnostics::trace_event_cache& event_cache, const xtd::ustring& source, int id, const xtd::ustring& message, const activity_id_type& related_activity_id) {
233#if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
234 write_line(xtd::ustring::format("{} transfer: {} : {}, related_activity_id={}", source, id, message, related_activity_id));
235 write_event_cache(event_cache);
236#endif
237 }
238
241 template <typename object>
242 void write(const object& o) {
243#if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
245#endif
246 }
247
251 template <typename object>
252 void write(const object& o, const xtd::ustring& category) {
253#if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
254 write(xtd::ustring::format("{} : {}", o, category));
255#endif
256 }
257
260 virtual void write(const xtd::ustring& message) = 0;
261
264 template <typename object>
265 void write_line(const object& o) {
266#if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
268#endif
269 }
270
274 template <typename object>
275 void write_line(const object& o, const xtd::ustring& category) {
276#if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
277 write_line(xtd::ustring::format("{} : {}", o, category));
278#endif
279 }
280
283 virtual void write_line(const xtd::ustring& message) = 0;
284
286 template<typename object>
287 trace_listener& operator<<(object&& message) {
288#if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
289 write_line(message);
290#endif
291 return *this;
292 }
294
295 protected:
298 bool need_indent() const;
302
306
309 virtual void write_indent() {
310#if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
311 need_indent_ = false;
312 for (unsigned int i = 0; i < indent_level_; ++i)
313 write(xtd::ustring(indent_size_, ' '));
314#endif
315 }
316
317 private:
318 void write_event_cache(const trace_event_cache& event_cache);
319
320 unsigned int indent_level_ = 0;
321 unsigned int indent_size_ = 4;
322 bool is_thread_safe_ = false;
323 xtd::ustring name_;
324 bool need_indent_ = true;
325 trace_options trace_output_options_ = trace_options::none;
326 };
327 }
328}
Provides trace event data specific to a thread and a process.
Definition trace_event_cache.h:22
Provides the abstract base class for the listeners who monitor trace and debug output.
Definition trace_listener.h:28
void trace_data(const xtd::diagnostics::trace_event_cache &event_cache, const xtd::ustring &source, const xtd::diagnostics::trace_event_type &event_type, int id, const std::vector< object > &data)
Writes trace information, a data object and event information to the listener specific output.
Definition trace_listener.h:153
void need_indent(bool need_indent)
Sets a value indicating whether to indent the output.
virtual void flush()
When overridden in a derived class, flushes the output buffer.
Definition trace_listener.h:126
void thread_safe(bool thread_safe)
Sets a value indicating whether the trace listener is thread safe.
trace_listener(const xtd::ustring &name)
Initializes a new instance of the trace_listener class using the specified name as the listener.
virtual void write_line(const xtd::ustring &message)=0
Writes the message to the listener you create when you implement the trace_listener class followed by...
virtual void trace_event(const xtd::diagnostics::trace_event_cache &event_cache, const xtd::ustring &source, const xtd::diagnostics::trace_event_type &event_type, int id)
Writes trace and event information to the listener specific output.
Definition trace_listener.h:183
virtual bool is_thread_safe() const
Gets a value indicating whether the trace listener is thread safe.
void trace_event(const xtd::diagnostics::trace_event_cache &event_cache, const xtd::ustring &source, const xtd::diagnostics::trace_event_type &event_type, int id, const xtd::ustring &format, const objects &... args)
Writes trace information, a formatted array of objects and event information to the listener specific...
Definition trace_listener.h:215
virtual void trace_event(const xtd::diagnostics::trace_event_cache &event_cache, const xtd::ustring &source, const xtd::diagnostics::trace_event_type &event_type, int id, const xtd::ustring &message)
Writes trace information, a message, and event information to the listener specific output.
Definition trace_listener.h:198
virtual void close()
When overridden in a derived class, closes the output stream so it no longer receives tracing or debu...
void write(const object &o, const xtd::ustring &category)
Writes a category name and the value of the object's ToString method to the listener you create when ...
Definition trace_listener.h:252
trace_listener()=default
Initializes a new instance of the trace_listener class.
virtual void write(const xtd::ustring &message)=0
Writes the message to the listener you create when you implement the trace_listener class.
void trace_data(const xtd::diagnostics::trace_event_cache &event_cache, const xtd::ustring &source, const xtd::diagnostics::trace_event_type &event_type, int id, const objects &... data)
Writes trace information, an array of data objects and event information to the listener specific out...
Definition trace_listener.h:169
void trace_output_options(const trace_options &trace_output_options)
Sets the trace output options.
void trace_data(const xtd::diagnostics::trace_event_cache &event_cache, const xtd::ustring &source, const xtd::diagnostics::trace_event_type &event_type, int id, const object &data)
Writes trace information, a data object and event information to the listener specific output.
Definition trace_listener.h:137
virtual void fail(const xtd::ustring &message)
Emits an error message to the listener you create when you implement the TraceListener class.
Definition trace_listener.h:109
unsigned int indent_size() const
Gets the number of spaces in an indent.
virtual void write_indent()
Writes the indent to the listener you create when you implement this class, and resets the NeedIndent...
Definition trace_listener.h:309
unsigned int indent_level() const
Gets the indent level.
void trace_transfer(const xtd::diagnostics::trace_event_cache &event_cache, const xtd::ustring &source, int id, const xtd::ustring &message, const activity_id_type &related_activity_id)
Writes trace information, a message, a related activity identity and event information to the listene...
Definition trace_listener.h:232
virtual void fail(const xtd::ustring &message, const xtd::ustring &detail_message)
Emits the specified error message.
Definition trace_listener.h:119
void indent_level(unsigned int indent_level)
Sets the indent level.
bool need_indent() const
Gets a value indicating whether to indent the output.
void write_line(const object &o)
Writes the value of the object's ToString method to the listener you create when you implement the Tr...
Definition trace_listener.h:265
void write_line(const object &o, const xtd::ustring &category)
Writes a category name and the value of the object's ToString method to the listener you create when ...
Definition trace_listener.h:275
const trace_options & trace_output_options() const
Gets the trace output options.
void write(const object &o)
Writes the value of the object's ToString method to the listener you create when you implement the Tr...
Definition trace_listener.h:242
void indent_size(unsigned int indent_size)
Sets the number of spaces in an indent.
const xtd::ustring & name() const
Gets or sets a name for this TraceListener.
void name(const xtd::ustring &name)
Sets a name for this TraceListener.
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.h:26
Represents text as a sequence of UTF-8 code units.
Definition ustring.h:48
static ustring join(const ustring separator, const collection_t &values) noexcept
Concatenates a specified separator string between each element of a specified object array,...
Definition ustring.h:842
static ustring format(const ustring &fmt, args_t &&... args)
Writes the text representation of the specified arguments list, to string using the specified format ...
Definition ustring.h:689
trace_options
Specifies trace data options to be written to the trace output.
Definition trace_options.h:18
trace_event_type
Identifies the type of event that has caused the trace.
Definition trace_event_type.h:18
@ none
Do not write any elements.
@ i
The I key.
@ o
The O key.
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition system_report.h:17
Contains xtd::diagnostics::trace_event_cache class.
Contains xtd::diagnostics::trace_event_type enum class.
Contains xtd::diagnostics::trace_options enum class.