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 "../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 {
42 class core_export_ trace_listener abstract_ {
43 public:
45
54 trace_listener() = default;
55
57 trace_listener(const trace_listener& tl) = delete;
58 trace_listener& operator =(const trace_listener& tl) = delete;
61
66
68
73 uint32 indent_level() const noexcept;
74
79
83 uint32 indent_size() const noexcept;
84
89
93 virtual bool is_thread_safe() const noexcept;
94
98 const xtd::string& name() const noexcept;
99
103 void name(const xtd::string& name) noexcept;
104
113
123
125
129 virtual void close();
130
134 virtual void fail(const xtd::string& message) {
135 #if DEBUG || TRACE
136 write_line(xtd::string::format("Fail: {}", message));
137 #endif
138 }
139
144 virtual void fail(const xtd::string& message, const xtd::string& detail_message) {
145 #if DEBUG || TRACE
146 write_line(xtd::string::format("Fail: {} {}", message, detail_message));
147 #endif
148 }
149
151 virtual void flush() {}
152
161 template<class objelassct>
162 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) {
163 #if DEBUG || TRACE
164 write_line(xtd::string::format("{} {}: {} : {}", source, event_type, id, data));
165 write_event_cache(event_cache);
166 #endif
167 }
168
177 template<class objelassct>
179 #if DEBUG || TRACE
180 write_line(xtd::string::format("{} {}: {} : {}", source, event_type, id, xtd::string::join(", ", data)));
181 write_event_cache(event_cache);
182 #endif
183 }
184
193 template<class ...objects>
194 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) {
195 #if DEBUG || TRACE
196 write_line(xtd::string::format("{} {}: {} : {}", source, event_type, id, xtd::string::join(", ", {std::forward<objects>(data)...})));
197 write_event_cache(event_cache);
198 #endif
199 }
200
208 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) {
209 #if DEBUG || TRACE
210 write_line(xtd::string::format("{} {}: {}", source, event_type, id));
211 write_event_cache(event_cache);
212 #endif
213 }
214
223 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) {
224 #if DEBUG || TRACE
225 write_line(xtd::string::format("{} {}: {} : {}", source, event_type, id, message));
226 write_event_cache(event_cache);
227 #endif
228 }
229
239 template<class ...objects>
240 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) {
241 #if DEBUG || TRACE
242 write_line(xtd::string::format("{} {}: {} : {}", source, event_type, id, xtd::string::format(format, args...)));
243 write_event_cache(event_cache);
244 #endif
245 }
246
256 template<class activity_id_type>
257 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) {
258 #if DEBUG || TRACE
259 write_line(xtd::string::format("{} transfer: {} : {}, related_activity_id={}", source, id, message, related_activity_id));
260 write_event_cache(event_cache);
261 #endif
262 }
263
266 template <class object>
267 void write(const object& o) {
268 #if DEBUG || TRACE
269 write(xtd::string::format("{}", o));
270 #endif
271 }
272
276 template <class object>
277 void write(const object& o, const xtd::string& category) {
278 #if DEBUG || TRACE
279 write(xtd::string::format("{} : {}", o, category));
280 #endif
281 }
282
285 virtual void write(const xtd::string& message) = 0;
286
289 template <class object>
290 void write_line(const object& o) {
291 #if DEBUG || TRACE
292 write_line(xtd::string::format("{}", o));
293 #endif
294 }
295
299 template <class object>
300 void write_line(const object& o, const xtd::string& category) {
301 #if DEBUG || TRACE
302 write_line(xtd::string::format("{} : {}", o, category));
303 #endif
304 }
305
308 virtual void write_line(const xtd::string& message) = 0;
310
312 template <class object>
313 trace_listener& operator <<(object&& message) {
314 #if DEBUG || TRACE
315 write_line(message);
316 #endif
317 return *this;
318 }
320
321 protected:
323
327 bool need_indent() const noexcept;
330 void need_indent(bool need_indent) noexcept;
331
334 void thread_safe(bool thread_safe) noexcept;
336
338
342 virtual void write_indent() {
343 #if DEBUG || TRACE
344 need_indent_ = false;
345 for (uint32 i = 0; i < indent_level_; ++i)
346 write(xtd::string(indent_size_, ' '));
347 #endif
348 }
349
350
351 private:
352 void write_event_cache(const trace_event_cache& event_cache);
353
354 uint32 indent_level_ = 0;
355 uint32 indent_size_ = 4;
356 bool is_thread_safe_ = false;
357 xtd::string name_;
358 bool need_indent_ = true;
359 trace_options trace_output_options_ = trace_options::none;
360 };
361 }
362}
Contains xtd::abstract_object class.
Contains xtd::array class.
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition array.hpp:63
Provides trace event data specific to a thread and a process.
Definition trace_event_cache.hpp:29
void thread_safe(bool thread_safe) noexcept
Sets a value indicating whether the trace listener is thread safe.
virtual void flush()
When overridden in a derived class, flushes the output buffer.
Definition trace_listener.hpp:151
trace_options trace_output_options() const noexcept
Gets the trace output options.
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:208
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:162
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:223
const xtd::string & name() const noexcept
Gets or sets a name for this TraceListener.
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:240
trace_listener(const xtd::string &name)
Initializes a new instance of the trace_listener class using the specified name as the listener.
virtual bool is_thread_safe() const noexcept
Gets a value indicating whether the trace listener is thread safe.
virtual void close()
When overridden in a derived class, closes the output stream so it no longer receives tracing or debu...
bool need_indent() const noexcept
Gets a value indicating whether to indent the output.
uint32 indent_size() const noexcept
Gets the number of spaces in an indent.
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:277
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:194
virtual void fail(const xtd::string &message)
Emits an error message to the listener you create when you implement the TraceListener class.
Definition trace_listener.hpp:134
virtual void write_indent()
Writes the indent to the listener you create when you implement this class, and resets the NeedIndent...
Definition trace_listener.hpp:342
virtual void fail(const xtd::string &message, const xtd::string &detail_message)
Emits the specified error message.
Definition trace_listener.hpp:144
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:290
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:267
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:257
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:178
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:300
trace_options
Specifies trace data options to be written to the trace output.
Definition trace_options.hpp:25
@ none
Do not write any elements.
Definition trace_options.hpp:27
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 core_export_
Define shared library export.
Definition core_export.hpp:13
xtd::basic_string< char > string
Represents text as a sequence of UTF-8 code units.
Definition __string_definitions.hpp:43
std::uint32_t uint32
Represents a 32-bit unsigned integer.
Definition uint32.hpp:23
std::int32_t int32
Represents a 32-bit signed integer.
Definition int32.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.
Definition console_key.hpp:104
@ o
The O key.
Definition console_key.hpp:116
The xtd::diagnostics namespace provides classes that allow you to interact with system processes,...
Definition assert_dialog_result.hpp:10
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8
constexpr const_pointer data() const noexcept
Gets direct access to the underlying contiguous storage.
Definition read_only_span.hpp:201
Contains xtd::string alias.
Contains xtd::diagnostics::trace_event_cache class.
Contains xtd::diagnostics::trace_event_type enum class.
Contains xtd::diagnostics::trace_options enum class.