xtd 0.2.0
trace_listener.hpp
Go to the documentation of this file.
1
4#pragma once
5#include <memory>
6#include <stdexcept>
7#include "../abstract.hpp"
8#include "../array.hpp"
9#include "../string.hpp"
10#include "trace_event_cache.hpp"
11#include "trace_event_type.hpp"
12#include "trace_options.hpp"
13
15namespace xtd {
17 namespace diagnostics {
39 public:
41
50 trace_listener() = default;
51
53 trace_listener(const trace_listener& tl) = delete;
54 trace_listener& operator =(const trace_listener& tl) = delete;
57
60 explicit trace_listener(const xtd::string& name);
62
64
69 uint32 indent_level() const noexcept;
70
74 void indent_level(uint32 indent_level) noexcept;
75
79 uint32 indent_size() const noexcept;
80
84 void indent_size(uint32 indent_size) noexcept;
85
89 virtual bool is_thread_safe() const noexcept;
90
94 const xtd::string& name() const noexcept;
95
99 void name(const xtd::string& name) noexcept;
100
108 trace_options trace_output_options() const noexcept;
109
117 void trace_output_options(trace_options trace_output_options) noexcept;
119
121
125 virtual void close();
126
130 virtual void fail(const xtd::string& message) {
131 #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
132 write_line(xtd::string::format("Fail: {}", message));
133 #endif
134 }
135
140 virtual void fail(const xtd::string& message, const xtd::string& detail_message) {
141 #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
142 write_line(xtd::string::format("Fail: {} {}", message, detail_message));
143 #endif
144 }
145
147 virtual void flush() {}
148
157 template<class objelassct>
158 void trace_data(const xtd::diagnostics::trace_event_cache& event_cache, const xtd::string& source, const xtd::diagnostics::trace_event_type& event_type, int32 id, const object& data) {
159 #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
160 write_line(xtd::string::format("{} {}: {} : {}", source, event_type, id, data));
161 write_event_cache(event_cache);
162 #endif
163 }
164
173 template<class objelassct>
174 void trace_data(const xtd::diagnostics::trace_event_cache& event_cache, const xtd::string& source, const xtd::diagnostics::trace_event_type& event_type, int32 id, const xtd::array<object>& data) {
175 #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
176 write_line(xtd::string::format("{} {}: {} : {}", source, event_type, id, xtd::string::join(", ", data)));
177 write_event_cache(event_cache);
178 #endif
179 }
180
189 template<class ...objects>
190 void trace_data(const xtd::diagnostics::trace_event_cache& event_cache, const xtd::string& source, const xtd::diagnostics::trace_event_type& event_type, int32 id, objects&& ... data) {
191 #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
192 write_line(xtd::string::format("{} {}: {} : {}", source, event_type, id, xtd::string::join(", ", {std::forward<objects>(data)...})));
193 write_event_cache(event_cache);
194 #endif
195 }
196
204 virtual void trace_event(const xtd::diagnostics::trace_event_cache& event_cache, const xtd::string& source, const xtd::diagnostics::trace_event_type& event_type, int32 id) {
205 #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
206 write_line(xtd::string::format("{} {}: {}", source, event_type, id));
207 write_event_cache(event_cache);
208 #endif
209 }
210
219 virtual void trace_event(const xtd::diagnostics::trace_event_cache& event_cache, const xtd::string& source, const xtd::diagnostics::trace_event_type& event_type, int32 id, const xtd::string& message) {
220 #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
221 write_line(xtd::string::format("{} {}: {} : {}", source, event_type, id, message));
222 write_event_cache(event_cache);
223 #endif
224 }
225
235 template<class ...objects>
236 void trace_event(const xtd::diagnostics::trace_event_cache& event_cache, const xtd::string& source, const xtd::diagnostics::trace_event_type& event_type, int32 id, const xtd::string& format, const objects& ... args) {
237 #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
238 write_line(xtd::string::format("{} {}: {} : {}", source, event_type, id, xtd::string::format(format, args...)));
239 write_event_cache(event_cache);
240 #endif
241 }
242
252 template<class activity_id_type>
253 void trace_transfer(const xtd::diagnostics::trace_event_cache& event_cache, const xtd::string& source, int32 id, const xtd::string& message, const activity_id_type& related_activity_id) {
254 #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
255 write_line(xtd::string::format("{} transfer: {} : {}, related_activity_id={}", source, id, message, related_activity_id));
256 write_event_cache(event_cache);
257 #endif
258 }
259
262 template <class object>
263 void write(const object& o) {
264 #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
265 write(xtd::string::format("{}", o));
266 #endif
267 }
268
272 template <class object>
273 void write(const object& o, const xtd::string& category) {
274 #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
275 write(xtd::string::format("{} : {}", o, category));
276 #endif
277 }
278
281 virtual void write(const xtd::string& message) = 0;
282
285 template <class object>
286 void write_line(const object& o) {
287 #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
288 write_line(xtd::string::format("{}", o));
289 #endif
290 }
291
295 template <class object>
296 void write_line(const object& o, const xtd::string& category) {
297 #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
298 write_line(xtd::string::format("{} : {}", o, category));
299 #endif
300 }
301
304 virtual void write_line(const xtd::string& message) = 0;
306
308 template <class object>
309 trace_listener& operator <<(object&& message) {
310 #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
311 write_line(message);
312 #endif
313 return *this;
314 }
316
317 protected:
319
323 bool need_indent() const noexcept;
326 void need_indent(bool need_indent) noexcept;
327
330 void thread_safe(bool thread_safe) noexcept;
332
334
338 virtual void write_indent() {
339 #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
340 need_indent_ = false;
341 for (uint32 i = 0; i < indent_level_; ++i)
342 write(xtd::string(indent_size_, ' '));
343 #endif
344 }
346
347 private:
348 void write_event_cache(const trace_event_cache& event_cache);
349
350 uint32 indent_level_ = 0;
351 uint32 indent_size_ = 4;
352 bool is_thread_safe_ = false;
353 xtd::string name_;
354 bool need_indent_ = true;
355 trace_options trace_output_options_ = trace_options::none;
356 };
357 }
358}
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition array.hpp:61
Represents text as a sequence of character units.
Definition basic_string.hpp:71
static basic_string join(const basic_string separator, const collection_t &values) noexcept
Concatenates a specified separator basic_string between each element of a specified object array,...
Definition basic_string.hpp:2288
Provides trace event data specific to a thread and a process.
Definition trace_event_cache.hpp:29
Provides the abstract base class for the listeners who monitor trace and debug output.
Definition trace_listener.hpp:38
virtual void flush()
When overridden in a derived class, flushes the output buffer.
Definition trace_listener.hpp:147
virtual void trace_event(const xtd::diagnostics::trace_event_cache &event_cache, const xtd::string &source, const xtd::diagnostics::trace_event_type &event_type, int32 id)
Writes trace and event information to the listener specific output.
Definition trace_listener.hpp:204
void trace_data(const xtd::diagnostics::trace_event_cache &event_cache, const xtd::string &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.hpp:158
virtual void trace_event(const xtd::diagnostics::trace_event_cache &event_cache, const xtd::string &source, const xtd::diagnostics::trace_event_type &event_type, int32 id, const xtd::string &message)
Writes trace information, a message, and event information to the listener specific output.
Definition trace_listener.hpp:219
void trace_event(const xtd::diagnostics::trace_event_cache &event_cache, const xtd::string &source, const xtd::diagnostics::trace_event_type &event_type, int32 id, const xtd::string &format, const objects &... args)
Writes trace information, a formatted array of objects and event information to the listener specific...
Definition trace_listener.hpp:236
trace_listener(const xtd::string &name)
Initializes a new instance of the trace_listener class using the specified name as the listener.
bool need_indent() const noexcept
Gets a value indicating whether to indent the output.
virtual void write(const xtd::string &message)=0
Writes the message to the listener you create when you implement the trace_listener class.
trace_listener()=default
Initializes a new instance of the trace_listener class.
void write(const object &o, const xtd::string &category)
Writes a category name and the value of the object's ToString method to the listener you create when ...
Definition trace_listener.hpp:273
void trace_data(const xtd::diagnostics::trace_event_cache &event_cache, const xtd::string &source, const xtd::diagnostics::trace_event_type &event_type, int32 id, objects &&... data)
Writes trace information, an array of data objects and event information to the listener specific out...
Definition trace_listener.hpp:190
virtual void fail(const xtd::string &message, const xtd::string &detail_message)
Emits the specified error message.
Definition trace_listener.hpp:140
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.hpp:286
virtual void write_line(const xtd::string &message)=0
Writes the message to the listener you create when you implement the trace_listener class followed by...
uint32 indent_level() const noexcept
Gets the indent level.
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.hpp:263
void trace_transfer(const xtd::diagnostics::trace_event_cache &event_cache, const xtd::string &source, int32 id, const xtd::string &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.hpp:253
void trace_data(const xtd::diagnostics::trace_event_cache &event_cache, const xtd::string &source, const xtd::diagnostics::trace_event_type &event_type, int32 id, const xtd::array< object > &data)
Writes trace information, a data object and event information to the listener specific output.
Definition trace_listener.hpp:174
void write_line(const object &o, const xtd::string &category)
Writes a category name and the value of the object's ToString method to the listener you create when ...
Definition trace_listener.hpp:296
trace_options
Specifies trace data options to be written to the trace output.
Definition trace_options.hpp:25
static basic_string format(const basic_string< char > &fmt, args_t &&... args)
Writes the text representation of the specified arguments list, to string using the specified format ...
xtd::string format(const xtd::string &fmt, args_t &&... args)
Writes the text representation of the specified arguments list, to string using the specified format ...
Definition format.hpp:20
#define abstract_
This keyword is used to represents an abstract class.
Definition abstract.hpp:25
#define core_export_
Define shared library export.
Definition core_export.hpp:13
int32_t int32
Represents a 32-bit signed integer.
Definition int32.hpp:23
uint32_t uint32
Represents a 32-bit unsigned integer.
Definition uint32.hpp:23
trace_event_type
Identifies the type of event that has caused the trace.
Definition trace_event_type.hpp:25
@ i
The I key.
@ o
The O key.
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.hpp:10
Contains xtd::diagnostics::trace_event_cache class.
Contains xtd::diagnostics::trace_event_type enum class.
Contains xtd::diagnostics::trace_options enum class.