xtd 0.2.0
trace_listener.h
Go to the documentation of this file.
1 #pragma once
5 #include <memory>
6 #include <stdexcept>
7 #include "../abstract.h"
8 #include "../ustring.h"
9 #include "trace_event_cache.h"
10 #include "trace_event_type.h"
11 #include "trace_options.h"
12 
14 namespace xtd {
16  namespace diagnostics {
35  class core_export_ trace_listener abstract_ {
36  public:
38 
47  trace_listener() = default;
48 
50  trace_listener(const trace_listener& tl) = delete;
51  trace_listener& operator =(const trace_listener& tl) = delete;
52  ~trace_listener();
54 
57  explicit trace_listener(const xtd::ustring& name);
59 
61 
66  uint32 indent_level() const noexcept;
67 
71  void indent_level(uint32 indent_level) noexcept;
72 
76  uint32 indent_size() const noexcept;
77 
81  void indent_size(uint32 indent_size) noexcept;
82 
86  virtual bool is_thread_safe() const noexcept;
87 
91  const xtd::ustring& name() const noexcept;
92 
96  void name(const xtd::ustring& name) noexcept;
97 
105  trace_options trace_output_options() const noexcept;
106 
114  void trace_output_options(trace_options trace_output_options) noexcept;
116 
118 
122  virtual void close();
123 
127  virtual void fail(const xtd::ustring& message) {
128  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
129  write_line(xtd::ustring::format("Fail: {}", message));
130  #endif
131  }
132 
137  virtual void fail(const xtd::ustring& message, const xtd::ustring& detail_message) {
138  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
139  write_line(xtd::ustring::format("Fail: {} {}", message, detail_message));
140  #endif
141  }
142 
144  virtual void flush() {}
145 
154  template<typename object>
155  void trace_data(const xtd::diagnostics::trace_event_cache& event_cache, const xtd::ustring& source, const xtd::diagnostics::trace_event_type& event_type, int32 id, const object& data) {
156  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
157  write_line(xtd::ustring::format("{} {}: {} : {}", source, event_type, id, data));
158  write_event_cache(event_cache);
159  #endif
160  }
161 
170  template<typename object>
171  void trace_data(const xtd::diagnostics::trace_event_cache& event_cache, const xtd::ustring& source, const xtd::diagnostics::trace_event_type& event_type, int32 id, const std::vector<object>& data) {
172  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
173  write_line(xtd::ustring::format("{} {}: {} : {}", source, event_type, id, xtd::ustring::join(", ", data)));
174  write_event_cache(event_cache);
175  #endif
176  }
177 
186  template<typename ...objects>
187  void trace_data(const xtd::diagnostics::trace_event_cache& event_cache, const xtd::ustring& source, const xtd::diagnostics::trace_event_type& event_type, int32 id, const objects& ... data) {
188  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
189  write_line(xtd::ustring::format("{} {}: {} : {}", source, event_type, id, xtd::ustring::join(", ", {data...})));
190  write_event_cache(event_cache);
191  #endif
192  }
193 
201  virtual void trace_event(const xtd::diagnostics::trace_event_cache& event_cache, const xtd::ustring& source, const xtd::diagnostics::trace_event_type& event_type, int32 id) {
202  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
203  write_line(xtd::ustring::format("{} {}: {}", source, event_type, id));
204  write_event_cache(event_cache);
205  #endif
206  }
207 
216  virtual void trace_event(const xtd::diagnostics::trace_event_cache& event_cache, const xtd::ustring& source, const xtd::diagnostics::trace_event_type& event_type, int32 id, const xtd::ustring& message) {
217  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
218  write_line(xtd::ustring::format("{} {}: {} : {}", source, event_type, id, message));
219  write_event_cache(event_cache);
220  #endif
221  }
222 
232  template<typename ...objects>
233  void trace_event(const xtd::diagnostics::trace_event_cache& event_cache, const xtd::ustring& source, const xtd::diagnostics::trace_event_type& event_type, int32 id, const xtd::ustring& format, const objects& ... args) {
234  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
235  write_line(xtd::ustring::format("{} {}: {} : {}", source, event_type, id, xtd::ustring::format(format, args...)));
236  write_event_cache(event_cache);
237  #endif
238  }
239 
249  template<typename activity_id_type>
250  void trace_transfer(const xtd::diagnostics::trace_event_cache& event_cache, const xtd::ustring& source, int32 id, const xtd::ustring& message, const activity_id_type& related_activity_id) {
251  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
252  write_line(xtd::ustring::format("{} transfer: {} : {}, related_activity_id={}", source, id, message, related_activity_id));
253  write_event_cache(event_cache);
254  #endif
255  }
256 
259  template <typename object>
260  void write(const object& o) {
261  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
262  write(xtd::ustring::format("{}", o));
263  #endif
264  }
265 
269  template <typename object>
270  void write(const object& o, const xtd::ustring& category) {
271  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
272  write(xtd::ustring::format("{} : {}", o, category));
273  #endif
274  }
275 
278  virtual void write(const xtd::ustring& message) = 0;
279 
282  template <typename object>
283  void write_line(const object& o) {
284  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
285  write_line(xtd::ustring::format("{}", o));
286  #endif
287  }
288 
292  template <typename object>
293  void write_line(const object& o, const xtd::ustring& category) {
294  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
295  write_line(xtd::ustring::format("{} : {}", o, category));
296  #endif
297  }
298 
301  virtual void write_line(const xtd::ustring& message) = 0;
303 
305  template<typename object>
306  trace_listener& operator <<(object&& message) {
307  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
308  write_line(message);
309  #endif
310  return *this;
311  }
313 
314  protected:
316 
320  bool need_indent() const noexcept;
323  void need_indent(bool need_indent) noexcept;
324 
327  void thread_safe(bool thread_safe) noexcept;
329 
331 
335  virtual void write_indent() {
336  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
337  need_indent_ = false;
338  for (uint32 i = 0; i < indent_level_; ++i)
339  write(xtd::ustring(indent_size_, ' '));
340  #endif
341  }
343 
344  private:
345  void write_event_cache(const trace_event_cache& event_cache);
346 
347  uint32 indent_level_ = 0;
348  uint32 indent_size_ = 4;
349  bool is_thread_safe_ = false;
350  xtd::ustring name_;
351  bool need_indent_ = true;
352  trace_options trace_output_options_ = trace_options::none;
353  };
354  }
355 }
Provides the abstract base class for the listeners who monitor trace and debug output.
Definition: trace_listener.h:35
void trace_event(const xtd::diagnostics::trace_event_cache &event_cache, const xtd::ustring &source, const xtd::diagnostics::trace_event_type &event_type, int32 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:233
void trace_data(const xtd::diagnostics::trace_event_cache &event_cache, const xtd::ustring &source, const xtd::diagnostics::trace_event_type &event_type, int32 id, const std::vector< object > &data)
Writes trace information, a data object and event information to the listener specific output...
Definition: trace_listener.h:171
void write(const object &o)
Writes the value of the object&#39;s ToString method to the listener you create when you implement the Tr...
Definition: trace_listener.h:260
void write(const object &o, const xtd::ustring &category)
Writes a category name and the value of the object&#39;s ToString method to the listener you create when ...
Definition: trace_listener.h:270
The xtd namespace contains all fundamental classes to access Hardware, Os, System, and more.
Definition: system_report.h:17
virtual void fail(const xtd::ustring &message, const xtd::ustring &detail_message)
Emits the specified error message.
Definition: trace_listener.h:137
Provides trace event data specific to a thread and a process.
Definition: trace_event_cache.h:28
virtual void trace_event(const xtd::diagnostics::trace_event_cache &event_cache, const xtd::ustring &source, const xtd::diagnostics::trace_event_type &event_type, int32 id)
Writes trace and event information to the listener specific output.
Definition: trace_listener.h:201
Represents text as a sequence of UTF-8 code units.
Definition: ustring.h:46
trace_options
Specifies trace data options to be written to the trace output.
Definition: trace_options.h:23
Contains xtd::diagnostics::trace_event_type enum class.
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:335
Contains xtd::diagnostics::trace_options enum class.
void trace_transfer(const xtd::diagnostics::trace_event_cache &event_cache, const xtd::ustring &source, int32 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:250
void write_line(const object &o, const xtd::ustring &category)
Writes a category name and the value of the object&#39;s ToString method to the listener you create when ...
Definition: trace_listener.h:293
void trace_data(const xtd::diagnostics::trace_event_cache &event_cache, const xtd::ustring &source, const xtd::diagnostics::trace_event_type &event_type, int32 id, const object &data)
Writes trace information, a data object and event information to the listener specific output...
Definition: trace_listener.h:155
int_least32_t int32
Represents a 32-bit signed integer.
Definition: types.h:129
#define abstract_
This keyword is used to represents an abstract class.
Definition: abstract.h:23
void write_line(const object &o)
Writes the value of the object&#39;s ToString method to the listener you create when you implement the Tr...
Definition: trace_listener.h:283
virtual void trace_event(const xtd::diagnostics::trace_event_cache &event_cache, const xtd::ustring &source, const xtd::diagnostics::trace_event_type &event_type, int32 id, const xtd::ustring &message)
Writes trace information, a message, and event information to the listener specific output...
Definition: trace_listener.h:216
virtual void flush()
When overridden in a derived class, flushes the output buffer.
Definition: trace_listener.h:144
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:744
Do not write any elements.
Contains xtd::diagnostics::trace_event_cache class.
uint_least32_t uint32
Represents a 32-bit unsigned integer.
Definition: types.h:239
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:127
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:932
void trace_data(const xtd::diagnostics::trace_event_cache &event_cache, const xtd::ustring &source, const xtd::diagnostics::trace_event_type &event_type, int32 id, const objects &... data)
Writes trace information, an array of data objects and event information to the listener specific out...
Definition: trace_listener.h:187
trace_event_type
Identifies the type of event that has caused the trace.
Definition: trace_event_type.h:23