xtd - Reference Guide  0.1.1
Modern c++17/20 framework to create console, GUI and unit test applications on Windows, macOS, Linux, iOS and android.
trace.h
Go to the documentation of this file.
1 #pragma once
5 #include "debug.h"
6 
8 namespace xtd {
10  namespace diagnostics {
32  public:
35 
40  static bool auto_flush();
45  static void auto_flush(bool auto_flush);
46 
50  static uint32_t indent_level();
54  static void indent_level(uint32_t indent_level);
55 
59  static uint32_t indent_size();
63  static void indent_size(uint32_t indent_size);
64 
74  static void listeners(const listener_collection& listeners);
75 
80  static bool show_assert_dialog();
85  static void show_assert_dialog(bool show_assert_dialog);
86 
90  static bool use_global_lock();
94  static void use_global_lock(bool use_global_lock);
95 
101  static void cassert(bool condition);
105  static void cassert(bool condition, const xtd::ustring& message);
110  static void cassert(bool condition, const xtd::ustring& message, const xtd::diagnostics::stack_frame& stack_frame);
114  static void cassert(bool condition, const xtd::diagnostics::stack_frame& stack_frame);
115 
120  static void fail(const xtd::ustring& message) {
121 #if defined(TRACE)
122  fail__(message);
123 #endif
124  }
130  static void fail(const xtd::ustring& message, const xtd::ustring& detail_message) {
131 #if defined(TRACE)
132  fail__(message, detail_message);
133 #endif
134  }
135 
137  static void flush() {
138 #if defined(TRACE)
139  flush_();
140 #endif
141  }
142 
144  static void indent();
145 
149  static void print(const xtd::ustring& message) {
150 #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
151  write_line_(message);
152 #endif
153  }
158  template<typename ...args_t>
159  static void print(const xtd::ustring& format, args_t&&... args) {
160 #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
161  write_line_(xtd::ustring::format(format, args...));
162 #endif
163  }
165  template<typename ...args_t>
166  static void print(const char* format, args_t&&... args) {
167 #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
168  write_line_(xtd::ustring::format(format, args...));
169 #endif
170  }
172 
176  static void trace_error(const xtd::ustring& message) {
177 #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
178  trace_event_(trace_event_type::error, message);
179 #endif
180  }
185  template<typename ...objects>
186  static void trace_error(const xtd::ustring& message, const objects& ... args) {
187 #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
188  trace_event_(trace_event_type::error, message, args...);
189 #endif
190  }
191 
195  static void trace_information(const xtd::ustring& message) {
196 #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
197  trace_event_(trace_event_type::information, message);
198 #endif
199  }
204  template<typename ...objects>
205  static void trace_information(const xtd::ustring& message, const objects& ... args) {
206 #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
207  trace_event_(trace_event_type::information, xtd::ustring::format(message, args...));
208 #endif
209  }
210 
214  static void trace_warning(const xtd::ustring& message) {
215 #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
216  trace_event_(trace_event_type::warning, message);
217 #endif
218  }
223  template<typename ...objects>
224  static void trace_warning(const xtd::ustring& message, const objects& ... args) {
225 #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
226  trace_event_(trace_event_type::warning, xtd::ustring::format(message, args...));
227 #endif
228  }
229 
231  static void unindent();
232 
237  static void write(const xtd::ustring& message) {
238 #if defined(TRACE)
239  write_(message);
240 #endif
241  }
242 
247  template<typename object>
248  static void write(const object& message) {
249 #if defined(TRACE)
250  write_(xtd::ustring::format("", message));
251 #endif
252  }
259  template<typename object>
260  static void write(const object& message, const xtd::ustring& category) {
261 #if defined(TRACE)
262  write_(xtd::ustring::format("", message), category);
263 #endif
264  }
270  template<typename ...args_t>
271  static void write(const xtd::ustring& format, args_t&&... args) {
272 #if defined(TRACE)
273  write_(ustring::format(format, args...));
274 #endif
275  }
277  template<typename ...args_t>
278  static void write(const char* format, args_t&&... args) {
279 #if defined(TRACE)
280  write_(ustring::format(format, args...));
281 #endif
282  }
284 
290  static void write_if(bool condition, const xtd::ustring& message) {
291 #if defined(TRACE)
292  if (condition) write_(message);
293 #endif
294  }
300  template<typename object>
301  static void write_if(bool condition, const object& message) {
302 #if defined(TRACE)
303  if (condition) write_(xtd::ustring::format("", message));
304 #endif
305  }
312  template<typename object>
313  static void write_if(bool condition, const object& message, const xtd::ustring& category) {
314 #if defined(TRACE)
315  if (condition) write_(xtd::ustring::format("", message), category);
316 #endif
317  }
318 
323  static void write_line() {
324 #if defined(TRACE)
325  write_line_("");
326 #endif
327  }
332  static void write_line(const xtd::ustring& message) {
333 #if defined(TRACE)
334  write_line_(message);
335 #endif
336  }
341  template<typename object>
342  static void write_line(const object& message) {
343 #if defined(TRACE)
344  write_line_(xtd::ustring::format("", message));
345 #endif
346  }
353  template<typename object>
354  static void write_line(const object& message, const xtd::ustring& category) {
355 #if defined(TRACE)
356  write_line_(xtd::ustring::format("", message), category);
357 #endif
358  }
363  template<typename ...args_t>
364  static void write_line(const xtd::ustring& format, args_t&&... args) {
365 #if defined(TRACE)
366  write_line_(xtd::ustring::format(format, args...));
367 #endif
368  }
370  template<typename ...args_t>
371  static void write_line(const char* format, args_t&&... args) {
372 #if defined(TRACE)
373  write_line_(xtd::ustring::format(format, args...));
374 #endif
375  }
377 
383  static void write_line_if(bool condition, const xtd::ustring& message) {
384 #if defined(TRACE)
385  if (condition) write_line_(message);
386 #endif
387  }
393  template<typename object>
394  static void write_line_if(bool condition, const object& message) {
395 #if defined(TRACE)
396  if (condition) write_line_(xtd::ustring::format("", message));
397 #endif
398  }
405  template<typename object>
406  static void write_line_if(bool condition, const object& message, const xtd::ustring& category) {
407 #if defined(TRACE)
408  if (condition) write_line_(xtd::ustring::format("", message), category);
409 #endif
410  }
411 
413  static inline bool __should_aborted__(bool condition) { return __should_aborted__(condition, "", csf_); }
414  static inline bool __should_aborted__(bool condition, const xtd::ustring& message) { return __should_aborted__(condition, message, csf_); }
415  static inline bool __should_aborted__(bool condition, const xtd::diagnostics::stack_frame& stack_frame) { return __should_aborted__(condition, "", stack_frame); }
416  static inline bool __should_aborted__(bool condition, const xtd::ustring& message, const xtd::diagnostics::stack_frame& stack_frame) {
417 #if defined(TRACE)
418  auto result = xtd::diagnostics::debug::assert_dialog(condition, message, stack_frame);
420  if (result == xtd::diagnostics::assert_dialog_result::retry) return true;
421 #endif
422  return false;
423  }
425 
426  private:
427  static void fail__(const xtd::ustring& message);
428  static void fail__(const xtd::ustring& message, const xtd::ustring& detail_message);
429  static void flush_();
430  static void trace_event_(trace_event_type trace_event_type, const xtd::ustring& message);
431  static void write_(const xtd::ustring& message);
432  static void write_(const xtd::ustring& message, const xtd::ustring& category);
433  static void write_line_(const xtd::ustring& message);
434  static void write_line_(const xtd::ustring& message, const xtd::ustring& category);
435 
436  inline static bool auto_flush_ = false;
437  inline static unsigned int indent_level_ = 0;
438  inline static unsigned int indent_size_ = 4;
439  static listener_collection& listeners_;
440  static bool& show_assert_dialog_;
441  inline static bool use_global_lock_ = true;
442  static std::mutex global_lock_;
443  static xtd::ustring source_name_;
444  };
445  }
446 }
Provides information about a xtd::diagnostics::stack_frame, which represents a function call on the c...
Definition: stack_frame.h:29
Represents a collection of xtd::diagnostics::trace_listener.
Definition: trace_listener_collection.h:19
Provides a set of methods and properties that help you debug the execution of your code....
Definition: trace.h:31
static void cassert(bool condition, const xtd::diagnostics::stack_frame &stack_frame)
Checks for a condition; if the condition is false, displays a message box that shows the call stack.
static void write_line()
Writes a line terminator to the trace listeners in the listeners collection.
Definition: trace.h:323
static void write(const object &message, const xtd::ustring &category)
Writes a category name and message to the trace listeners in the listeners collection.
Definition: trace.h:260
static void write(const xtd::ustring &format, args_t &&... args)
Writes a formatted string to the trace listeners in the listeners collection.
Definition: trace.h:271
static void trace_information(const xtd::ustring &message, const objects &... args)
Writes an informational message to the trace listeners in the listeners collection using the specifie...
Definition: trace.h:205
static void fail(const xtd::ustring &message, const xtd::ustring &detail_message)
Emits an error message and a detailed error message.
Definition: trace.h:130
static listener_collection & listeners()
Gets the collection of listeners that is monitoring the trace output.
static void listeners(const listener_collection &listeners)
Sets the collection of listeners that is monitoring the trace output.
static void print(const xtd::ustring &message)
Writes a message followed by a line terminator to the trace listeners in the listeners collection.
Definition: trace.h:149
static void write_line(const object &message)
Writes a message followed by a line terminator to the trace listeners in the listeners collection.
Definition: trace.h:342
static void indent()
Increases the current indent_level by one.
static void fail(const xtd::ustring &message)
Emits the specified error message.
Definition: trace.h:120
static void write(const xtd::ustring &message)
Writes a message to the trace listeners in the listeners collection.
Definition: trace.h:237
static void trace_error(const xtd::ustring &message, const objects &... args)
Writes an error message to the trace listeners in the listeners collection using the specified array ...
Definition: trace.h:186
static bool use_global_lock()
Gets a value indicating whether the global lock should be used.
static void write_line(const xtd::ustring &message)
Writes a message followed by a line terminator to the trace listeners in the listeners collection.
Definition: trace.h:332
static void trace_warning(const xtd::ustring &message)
Writes a warning message to the trace listeners in the listeners collection using the specified messa...
Definition: trace.h:214
static void cassert(bool condition)
Checks for a condition; if the condition is false, displays a message box that shows the call stack.
static void show_assert_dialog(bool show_assert_dialog)
Sets a value indicating whether the assert dialog should be show.
static void trace_error(const xtd::ustring &message)
Writes an error message to the trace listeners in the Listeners collection using the specified messag...
Definition: trace.h:176
static void indent_size(uint32_t indent_size)
Sets the number of spaces in an indent.
static void indent_level(uint32_t indent_level)
Sets the indent level.
static void write(const object &message)
Writes a message to the trace listeners in the listeners collection.
Definition: trace.h:248
static void print(const xtd::ustring &format, args_t &&... args)
Writes a formatted string followed by a line terminator to the trace listeners in the listeners colle...
Definition: trace.h:159
static void write_if(bool condition, const object &message, const xtd::ustring &category)
Writes a category name and message to the trace listeners in the Listeners collection if a condition ...
Definition: trace.h:313
static void write_line(const object &message, const xtd::ustring &category)
Writes a category name and message followed by a line terminator to the trace listeners in the listen...
Definition: trace.h:354
static void trace_warning(const xtd::ustring &message, const objects &... args)
Writes a warning message to the trace listeners in the listeners collection using the specified array...
Definition: trace.h:224
static uint32_t indent_level()
Gets the indent level.
static bool show_assert_dialog()
Gets a value indicating whether the assert dialog should be show.
static void use_global_lock(bool use_global_lock)
Sets a value indicating whether the global lock should be used.
static void cassert(bool condition, const xtd::ustring &message)
Checks for a condition; if the condition is false, displays a message box that shows the call stack.
static void flush()
Flushes the output buffer and causes buffered data to write to the listeners collection.
Definition: trace.h:137
static void cassert(bool condition, const xtd::ustring &message, const xtd::diagnostics::stack_frame &stack_frame)
Checks for a condition; if the condition is false, displays a message box that shows the call stack.
static void write_line_if(bool condition, const object &message, const xtd::ustring &category)
Writes a category name and message followed by a line terminator to the trace listeners in the Listen...
Definition: trace.h:406
static void auto_flush(bool auto_flush)
Sets whether Flush should be called on the Listeners after every write.
static bool auto_flush()
Get whether Flush should be called on the Listeners after every write.
static uint32_t indent_size()
Gets the number of spaces in an indent.
static void trace_information(const xtd::ustring &message)
Writes an informational message to the trace listeners in the listeners collection using the specifie...
Definition: trace.h:195
static void write_if(bool condition, const object &message)
Writes a message to the trace listeners in the Listeners collection if a condition is true.
Definition: trace.h:301
static void write_line_if(bool condition, const object &message)
Writes a message followed by a line terminator to the trace listeners in the Listeners collection if ...
Definition: trace.h:394
static void write_line_if(bool condition, const xtd::ustring &message)
Writes a message followed by a line terminator to the trace listeners in the Listeners collection if ...
Definition: trace.h:383
static void write_if(bool condition, const xtd::ustring &message)
Writes a message to the trace listeners in the Listeners collection if a condition is true.
Definition: trace.h:290
static void unindent()
Decreases the current indent_level by one.
static void write_line(const xtd::ustring &format, args_t &&... args)
Writes a formatted string followed by a line terminator to the trace listeners in the listeners colle...
Definition: trace.h:364
static void exit(int exit_code)
Terminates this process and returns an exit code to the operating system.
Definition: environment.h:321
Represents text as a sequence of UTF-8 code units.
Definition: ustring.h:48
Contains xtd::diagnostics::debug class.
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:689
#define static_
This keyword is use to represent a static object. A static object can't be instantiated (constructors...
Definition: static.h:38
#define core_export_
Define shared library export.
Definition: core_export.h:13
#define csf_
Provides information about the current stack frame.
Definition: stack_frame.h:213
trace_event_type
Identifies the type of event that has caused the trace.
Definition: trace_event_type.h:18
@ retry
The assert dialog return value is Retry (usually sent from a button labeled Retry).
@ abort
The assert dialog return value is Abort (usually sent from a button labeled Abort).
@ warning
Warning Noncritical problem.
@ information
Informational message.
@ print
The PRINT key.
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition: system_report.h:17