xtd 0.2.0
Loading...
Searching...
No Matches
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 "../string.hpp"
10#include "trace_event_type.hpp"
11#include "trace_options.hpp"
12
14namespace xtd {
16 namespace diagnostics {
38 public:
40
49 trace_listener() = default;
50
52 trace_listener(const trace_listener& tl) = delete;
53 trace_listener& operator =(const trace_listener& tl) = delete;
56
59 explicit trace_listener(const xtd::string& name);
61
63
68 uint32 indent_level() const noexcept;
69
73 void indent_level(uint32 indent_level) noexcept;
74
78 uint32 indent_size() const noexcept;
79
83 void indent_size(uint32 indent_size) noexcept;
84
88 virtual bool is_thread_safe() const noexcept;
89
93 const xtd::string& name() const noexcept;
94
98 void name(const xtd::string& name) noexcept;
99
107 trace_options trace_output_options() const noexcept;
108
116 void trace_output_options(trace_options trace_output_options) noexcept;
118
120
124 virtual void close();
125
129 virtual void fail(const xtd::string& message) {
130 #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
131 write_line(xtd::string::format("Fail: {}", message));
132 #endif
133 }
134
139 virtual void fail(const xtd::string& message, const xtd::string& detail_message) {
140 #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
141 write_line(xtd::string::format("Fail: {} {}", message, detail_message));
142 #endif
143 }
144
146 virtual void flush() {}
147
156 template<typename object>
157 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) {
158 #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
159 write_line(xtd::string::format("{} {}: {} : {}", source, event_type, id, data));
160 write_event_cache(event_cache);
161 #endif
162 }
163
172 template<typename object>
173 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 std::vector<object>& data) {
174 #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
175 write_line(xtd::string::format("{} {}: {} : {}", source, event_type, id, xtd::string::join(", ", data)));
176 write_event_cache(event_cache);
177 #endif
178 }
179
188 template<typename ...objects>
189 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 objects& ... data) {
190 #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
191 write_line(xtd::string::format("{} {}: {} : {}", source, event_type, id, xtd::string::join(", ", {data...})));
192 write_event_cache(event_cache);
193 #endif
194 }
195
203 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) {
204 #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
205 write_line(xtd::string::format("{} {}: {}", source, event_type, id));
206 write_event_cache(event_cache);
207 #endif
208 }
209
218 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) {
219 #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
220 write_line(xtd::string::format("{} {}: {} : {}", source, event_type, id, message));
221 write_event_cache(event_cache);
222 #endif
223 }
224
234 template<typename ...objects>
235 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) {
236 #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
237 write_line(xtd::string::format("{} {}: {} : {}", source, event_type, id, xtd::string::format(format, args...)));
238 write_event_cache(event_cache);
239 #endif
240 }
241
251 template<typename activity_id_type>
252 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) {
253 #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
254 write_line(xtd::string::format("{} transfer: {} : {}, related_activity_id={}", source, id, message, related_activity_id));
255 write_event_cache(event_cache);
256 #endif
257 }
258
261 template <typename object>
262 void write(const object& o) {
263 #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
264 write(xtd::string::format("{}", o));
265 #endif
266 }
267
271 template <typename object>
272 void write(const object& o, const xtd::string& category) {
273 #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
274 write(xtd::string::format("{} : {}", o, category));
275 #endif
276 }
277
280 virtual void write(const xtd::string& message) = 0;
281
284 template <typename object>
285 void write_line(const object& o) {
286 #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
287 write_line(xtd::string::format("{}", o));
288 #endif
289 }
290
294 template <typename object>
295 void write_line(const object& o, const xtd::string& category) {
296 #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
297 write_line(xtd::string::format("{} : {}", o, category));
298 #endif
299 }
300
303 virtual void write_line(const xtd::string& message) = 0;
305
307 template<typename object>
308 trace_listener& operator <<(object&& message) {
309 #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
310 write_line(message);
311 #endif
312 return *this;
313 }
315
316 protected:
318
322 bool need_indent() const noexcept;
325 void need_indent(bool need_indent) noexcept;
326
329 void thread_safe(bool thread_safe) noexcept;
331
333
337 virtual void write_indent() {
338 #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
339 need_indent_ = false;
340 for (uint32 i = 0; i < indent_level_; ++i)
341 write(xtd::string(indent_size_, ' '));
342 #endif
343 }
345
346 private:
347 void write_event_cache(const trace_event_cache& event_cache);
348
349 uint32 indent_level_ = 0;
350 uint32 indent_size_ = 4;
351 bool is_thread_safe_ = false;
352 xtd::string name_;
353 bool need_indent_ = true;
354 trace_options trace_output_options_ = trace_options::none;
355 };
356 }
357}
Represents text as a sequence of character units.
Definition basic_string.hpp:79
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:2296
Provides trace event data specific to a thread and a process.
Definition trace_event_cache.hpp:30
Provides the abstract base class for the listeners who monitor trace and debug output.
Definition trace_listener.hpp:37
virtual void flush()
When overridden in a derived class, flushes the output buffer.
Definition trace_listener.hpp:146
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 std::vector< object > &data)
Writes trace information, a data object and event information to the listener specific output.
Definition trace_listener.hpp:173
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:203
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:157
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:218
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:235
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:272
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 objects &... data)
Writes trace information, an array of data objects and event information to the listener specific out...
Definition trace_listener.hpp:189
virtual void fail(const xtd::string &message, const xtd::string &detail_message)
Emits the specified error message.
Definition trace_listener.hpp:139
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:285
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:262
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:252
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:295
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.