xtd - Reference Guide 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 {
33  class core_export_ trace_listener abstract_ {
34  public:
36 
45  trace_listener() = default;
46 
48  trace_listener(const trace_listener& tl) = delete;
49  trace_listener& operator=(const trace_listener& tl) = delete;
50  ~trace_listener();
52 
55  explicit trace_listener(const xtd::ustring& name);
57 
59 
64  unsigned int indent_level() const;
65 
69  void indent_level(unsigned int indent_level);
70 
74  unsigned int indent_size() const;
75 
79  void indent_size(unsigned int indent_size);
80 
84  virtual bool is_thread_safe() const;
85 
89  const xtd::ustring& name() const;
90 
94  void name(const xtd::ustring& name);
95 
103  const trace_options& trace_output_options() const;
104 
112  void trace_output_options(const trace_options& trace_output_options);
114 
116 
120  virtual void close();
121 
125  virtual void fail(const xtd::ustring& message) {
126  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
127  write_line(xtd::ustring::format("Fail: {}", message));
128  #endif
129  }
130 
135  virtual void fail(const xtd::ustring& message, const xtd::ustring& detail_message) {
136  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
137  write_line(xtd::ustring::format("Fail: {} {}", message, detail_message));
138  #endif
139  }
140 
142  virtual void flush() {}
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 object& data) {
154  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
155  write_line(xtd::ustring::format("{} {}: {} : {}", source, event_type, id, data));
156  write_event_cache(event_cache);
157  #endif
158  }
159 
168  template<typename object>
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 std::vector<object>& 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 
184  template<typename ...objects>
185  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) {
186  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
187  write_line(xtd::ustring::format("{} {}: {} : {}", source, event_type, id, xtd::ustring::join(", ", {data...})));
188  write_event_cache(event_cache);
189  #endif
190  }
191 
199  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) {
200  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
201  write_line(xtd::ustring::format("{} {}: {}", source, event_type, id));
202  write_event_cache(event_cache);
203  #endif
204  }
205 
214  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) {
215  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
216  write_line(xtd::ustring::format("{} {}: {} : {}", source, event_type, id, message));
217  write_event_cache(event_cache);
218  #endif
219  }
220 
230  template<typename ...objects>
231  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) {
232  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
233  write_line(xtd::ustring::format("{} {}: {} : {}", source, event_type, id, xtd::ustring::format(format, args...)));
234  write_event_cache(event_cache);
235  #endif
236  }
237 
247  template<typename activity_id_type>
248  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) {
249  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
250  write_line(xtd::ustring::format("{} transfer: {} : {}, related_activity_id={}", source, id, message, related_activity_id));
251  write_event_cache(event_cache);
252  #endif
253  }
254 
257  template <typename object>
258  void write(const object& o) {
259  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
260  write(xtd::ustring::format("{}", o));
261  #endif
262  }
263 
267  template <typename object>
268  void write(const object& o, const xtd::ustring& category) {
269  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
270  write(xtd::ustring::format("{} : {}", o, category));
271  #endif
272  }
273 
276  virtual void write(const xtd::ustring& message) = 0;
277 
280  template <typename object>
281  void write_line(const object& o) {
282  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
283  write_line(xtd::ustring::format("{}", o));
284  #endif
285  }
286 
290  template <typename object>
291  void write_line(const object& o, const xtd::ustring& category) {
292  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
293  write_line(xtd::ustring::format("{} : {}", o, category));
294  #endif
295  }
296 
299  virtual void write_line(const xtd::ustring& message) = 0;
301 
303  template<typename object>
304  trace_listener& operator<<(object&& message) {
305  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
306  write_line(message);
307  #endif
308  return *this;
309  }
311 
312  protected:
314 
318  bool need_indent() const;
321  void need_indent(bool need_indent);
322 
325  void thread_safe(bool thread_safe);
327 
329 
333  virtual void write_indent() {
334  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
335  need_indent_ = false;
336  for (unsigned int i = 0; i < indent_level_; ++i)
337  write(xtd::ustring(indent_size_, ' '));
338  #endif
339  }
341 
342  private:
343  void write_event_cache(const trace_event_cache& event_cache);
344 
345  unsigned int indent_level_ = 0;
346  unsigned int indent_size_ = 4;
347  bool is_thread_safe_ = false;
348  xtd::ustring name_;
349  bool need_indent_ = true;
350  trace_options trace_output_options_ = trace_options::none;
351  };
352  }
353 }
Provides the abstract base class for the listeners who monitor trace and debug output.
Definition: trace_listener.h:33
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: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)
Writes trace information, a data object and event information to the listener specific output...
Definition: trace_listener.h:169
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:258
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:185
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:268
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:135
Provides trace event data specific to a thread and a process.
Definition: trace_event_cache.h:27
Represents text as a sequence of UTF-8 code units.
Definition: ustring.h:50
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:333
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:199
Contains xtd::diagnostics::trace_options enum class.
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:214
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:248
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:291
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:231
#define abstract_
This keyword is used to represents an abstract class.
Definition: abstract.h:52
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:281
virtual void flush()
When overridden in a derived class, flushes the output buffer.
Definition: trace_listener.h:142
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:708
Contains xtd::diagnostics::trace_event_cache class.
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:125
Do not write any elements.
trace_options
Specifies trace data options to be written to the trace output.
Definition: trace_options.h:21
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:869
trace_event_type
Identifies the type of event that has caused the trace.
Definition: trace_event_type.h:21