xtd 0.2.0
debug.h
Go to the documentation of this file.
1 #pragma once
5 #include <cstdint>
6 #include <cstdlib>
7 #include "../add_last_arg_to_command.h"
8 #include "../core_export.h"
9 #include "../environment.h"
10 #include "../static.h"
11 #include "../ustring.h"
12 #include "assert_dialog_result.h"
13 #include "debugger.h"
14 #include "stack_trace.h"
16 
18 namespace xtd {
20  namespace forms {
21  class assert_dialog;
22  }
24 
26  namespace diagnostics {
28  class trace;
57  class core_export_ debug static_ {
58  public:
60 
65 
67 
73  static bool auto_flush() noexcept;
78  static void auto_flush(bool auto_flush) noexcept;
79 
83  static uint32 indent_level() noexcept;
87  static void indent_level(uint32 indent_level) noexcept;
88 
92  static uint32 indent_size() noexcept;
96  static void indent_size(uint32 indent_size) noexcept;
97 
102  static listener_collection& listeners() noexcept;
107  static void listeners(const listener_collection& listeners) noexcept;
108 
113  static bool show_assert_dialog() noexcept;
118  static void show_assert_dialog(bool show_assert_dialog) noexcept;
119 
123  static bool use_global_lock() noexcept;
127  static void use_global_lock(bool use_global_lock) noexcept;
129 
131 
138  static void cassert(bool condition);
142  static void cassert(bool condition, const xtd::ustring& message);
147  static void cassert(bool condition, const xtd::ustring& message, const xtd::diagnostics::stack_frame& stack_frame);
152  static void cassert(bool condition, const xtd::ustring& message, const xtd::ustring& detail_message);
158  static void cassert(bool condition, const xtd::ustring& message, const xtd::ustring& detail_message, const xtd::diagnostics::stack_frame& stack_frame);
162  static void cassert(bool condition, const xtd::diagnostics::stack_frame& stack_frame);
163 
168  static void fail(const xtd::ustring& message) {
169  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
170  fail__(message);
171  #endif
172  }
178  static void fail(const xtd::ustring& message, const xtd::ustring& detail_message) {
179  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
180  fail__(message, detail_message);
181  #endif
182  }
183 
185  static void flush() {
186  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
187  flush_();
188  #endif
189  }
190 
192  static void indent() noexcept;
193 
197  static void print(const xtd::ustring& message) {
198  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
199  write_line_(message);
200  #endif
201  }
206  template<typename ...args_t>
207  static void print(const xtd::ustring& format, args_t&& ... args) {
208  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
209  write_line_(xtd::ustring::format(format, args...));
210  #endif
211  }
213  template<typename ...args_t>
214  static void print(const char* format, args_t&& ... args) {
215  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
216  write_line_(xtd::ustring::format(format, args...));
217  #endif
218  }
220 
224  static void trace_error(const xtd::ustring& message) {
225  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
226  trace_event_(trace_event_type::error, message);
227  #endif
228  }
233  template<typename ...objects_t>
234  static void trace_error(const xtd::ustring& message, const objects_t& ... args) {
235  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
236  trace_event_(trace_event_type::error, xtd::ustring::format(message, args...));
237  #endif
238  }
239 
243  static void trace_information(const xtd::ustring& message) {
244  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
245  trace_event_(trace_event_type::information, message);
246  #endif
247  }
252  template<typename ...objects_t>
253  static void trace_information(const xtd::ustring& message, const objects_t& ... args) {
254  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
255  trace_event_(trace_event_type::information, xtd::ustring::format(message, args...));
256  #endif
257  }
258 
262  static void trace_warning(const xtd::ustring& message) {
263  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
264  trace_event_(trace_event_type::warning, message);
265  #endif
266  }
271  template<typename ...objects_t>
272  static void trace_warning(const xtd::ustring& message, const objects_t& ... args) {
273  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
274  trace_event_(trace_event_type::warning, xtd::ustring::format(message, args...));
275  #endif
276  }
277 
279  static void unindent() noexcept;
280 
285  static void write(const xtd::ustring& message) {
286  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
287  write_(message);
288  #endif
289  }
294  template<typename object_t>
295  static void write(const object_t& message) {
296  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
297  write_(xtd::ustring::format("{}", message));
298  #endif
299  }
306  template<typename object_t>
307  static void write(const object_t& message, const xtd::ustring& category) {
308  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
309  write_(xtd::ustring::format("{}", message), category);
310  #endif
311  }
317  template<typename ...args_t>
318  static void write(const xtd::ustring& format, args_t&& ... args) {
319  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
320  write_(ustring::format(format, args...));
321  #endif
322  }
324  template<typename ...args_t>
325  static void write(const char* format, args_t&& ... args) {
326  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
327  write_(ustring::format(format, args...));
328  #endif
329  }
331 
337  static void write_if(bool condition, const xtd::ustring& message) {
338  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
339  if (condition) write_(message);
340  #endif
341  }
342  template<typename object_t>
348  static void write_if(bool condition, const object_t& message) {
349  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
350  if (condition) write_(xtd::ustring::format("{}", message));
351  #endif
352  }
359  template<typename object_t>
360  static void write_if(bool condition, const object_t& message, const xtd::ustring& category) {
361  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
362  if (condition) write_(xtd::ustring::format("{}", message), category);
363  #endif
364  }
365 
370  static void write_line() {
371  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
372  write_line_("");
373  #endif
374  }
379  static void write_line(const xtd::ustring& message) {
380  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
381  write_line_(message);
382  #endif
383  }
388  template<typename object_t>
389  static void write_line(const object_t& message) {
390  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
391  write_line_(xtd::ustring::format("{}", message));
392  #endif
393  }
400  template<typename object_t>
401  static void write_line(const object_t& message, const xtd::ustring& category) {
402  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
403  write_line_(xtd::ustring::format("{}", message), category);
404  #endif
405  }
410  template<typename ...args_t>
411  static void write_line(const xtd::ustring& format, args_t&& ... args) {
412  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
413  write_line_(ustring::format(format, args...));
414  #endif
415  }
417  template<typename ...args_t>
418  static void write_line(const char* format, args_t&& ... args) {
419  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
420  write_line_(ustring::format(format, args...));
421  #endif
422  }
424 
430  static void write_line_if(bool condition, const xtd::ustring& message) {
431  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
432  if (condition) write_line_(message);
433  #endif
434  }
440  template<typename object_t>
441  static void write_line_if(bool condition, const object_t& message) {
442  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
443  if (condition) write_line_(message);
444  #endif
445  }
452  template<typename object_t>
453  static void write_line_if(bool condition, const object_t& message, const xtd::ustring& category) {
454  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
455  if (condition) write_line_(message, category);
456  #endif
457  }
458 
460  static inline bool __should_aborted__(bool condition) {return __should_aborted__(condition, "", csf_);}
461  static inline bool __should_aborted__(bool condition, const xtd::ustring& message) {return __should_aborted__(condition, message, csf_);}
462  static inline bool __should_aborted__(bool condition, const xtd::diagnostics::stack_frame& stack_frame) {return __should_aborted__(condition, "", stack_frame);}
463  static inline bool __should_aborted__(bool condition, const xtd::ustring& message, const xtd::diagnostics::stack_frame& stack_frame) {return __should_aborted__(condition, message, "", stack_frame);}
464  static inline bool __should_aborted__(bool condition, const xtd::ustring& message, const xtd::ustring& detail_message) {return __should_aborted__(condition, message, detail_message, csf_);}
465  static inline bool __should_aborted__(bool condition, const xtd::ustring& message, const xtd::ustring& detail_message, const xtd::diagnostics::stack_frame& stack_frame) {
466  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
467  auto result = xtd::diagnostics::debug::assert_dialog(condition, message, detail_message, stack_frame);
469  if (result == xtd::diagnostics::assert_dialog_result::retry) return true;
470  #endif
471  return false;
472  }
475 
476  private:
477  friend trace;
479  static xtd::diagnostics::assert_dialog_result assert_dialog(bool condition, const xtd::ustring& message, const xtd::ustring& detail_message, const xtd::diagnostics::stack_frame& stack_frame);
480  static xtd::ustring assert_dialog_caption();
481  static void fail__(const xtd::ustring& message);
482  static void fail__(const xtd::ustring& message, const xtd::ustring& detail_message);
483  static void flush_();
484  static void trace_event_(trace_event_type trace_event_type, const xtd::ustring& message);
485  static void write_(const xtd::ustring& message);
486  static void write_(const xtd::ustring& message, const xtd::ustring& category);
487  static void write_line_(const xtd::ustring& message);
488  static void write_line_(const xtd::ustring& message, const xtd::ustring& category);
489 
490  inline static bool auto_flush_ = false;
491  inline static uint32 indent_level_ = 0;
492  inline static uint32 indent_size_ = 4;
493  static listener_collection& listeners_;
494  static bool& show_assert_dialog_;
495  static xtd::ustring source_name_;
496  };
497  }
498 }
499 
520 #define cassert_(...) \
521  add_last_arg_to_command_(cassert, (csf_), __VA_ARGS__)
Contains xtd::diagnostics::stack_trace class.
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: debug.h:262
Contains xtd::diagnostics::debugger class.
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: debug.h:337
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: debug.h:207
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: debug.h:224
#define static_
This keyword is use to represent a static object. A static object can&#39;t be instantiated (constructors...
Definition: static.h:37
static void trace_error(const xtd::ustring &message, const objects_t &... args)
Writes an error message to the trace listeners in the listeners collection using the specified array ...
Definition: debug.h:234
The xtd namespace contains all fundamental classes to access Hardware, Os, System, and more.
Definition: system_report.h:17
static void write(const xtd::ustring &format, args_t &&... args)
Writes a formatted string to the trace listeners in the listeners collection.
Definition: debug.h:318
#define csf_
Provides information about the current stack frame.
Definition: current_stack_frame.h:30
static void write_if(bool condition, const object_t &message, const xtd::ustring &category)
Writes a category name and message to the trace listeners in the Listeners collection if a condition ...
Definition: debug.h:360
Represents text as a sequence of UTF-8 code units.
Definition: ustring.h:46
static void write_line_if(bool condition, const object_t &message)
Writes a message followed by a line terminator to the trace listeners in the Listeners collection if ...
Definition: debug.h:441
static void exit()
Terminates this process and returns an exit code to the operating system.
The assert dialog return value is Abort (usually sent from a button labeled Abort).
The message box contains a symbol consisting of white X in a circle with a red background.
assert_dialog_result
Specifies identifiers to indicate the return value of an assert dialog box.
Definition: assert_dialog_result.h:23
static void write(const object_t &message, const xtd::ustring &category)
Writes a category name and message to the trace listeners in the listeners collection.
Definition: debug.h:307
static void flush()
Flushes the output buffer and causes buffered data to write to the listeners collection.
Definition: debug.h:185
Represents a collection of xtd::diagnostics::trace_listener.
Definition: trace_listener_collection.h:26
Provides information about a xtd::diagnostics::stack_frame, which represents a function call on the c...
Definition: stack_frame.h:37
Contains xtd::diagnostics::trace_listener_collection class.
The assert dialog return value is Retry (usually sent from a button labeled Retry).
static void write_if(bool condition, const object_t &message)
Writes a message to the trace listeners in the Listeners collection if a condition is true...
Definition: debug.h:348
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: debug.h:379
static void fail(const xtd::ustring &message, const xtd::ustring &detail_message)
Emits an error message and a detailed error message.
Definition: debug.h:178
static void write_line_if(bool condition, const object_t &message, const xtd::ustring &category)
Writes a category name and message followed by a line terminator to the trace listeners in the Listen...
Definition: debug.h:453
The message box contains a symbol consisting of a lowercase letter i in a circle. ...
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: debug.h:411
static void write_line(const object_t &message)
Writes a message followed by a line terminator to the trace listeners in the listeners collection...
Definition: debug.h:389
static void write_line(const object_t &message, const xtd::ustring &category)
Writes a category name and message followed by a line terminator to the trace listeners in the listen...
Definition: debug.h:401
Provides a set of methods and properties that help you debug the execution of your code...
Definition: trace.h:38
Provides a set of methods and properties that help you debug the execution of your code...
Definition: debug.h:57
static void write(const xtd::ustring &message)
Writes a message to the trace listeners in the listeners collection.
Definition: debug.h:285
static void print(const xtd::ustring &message)
Writes a message followed by a line terminator to the trace listeners in the listeners collection...
Definition: debug.h:197
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:744
The xtd::forms namespace contains classes for creating Windows-based applications that take full adva...
Definition: about_box.h:13
static void trace_information(const xtd::ustring &message)
Writes an informational message to the trace listeners in the listeners collection using the specifie...
Definition: debug.h:243
static void trace_warning(const xtd::ustring &message, const objects_t &... args)
Writes a warning message to the trace listeners in the listeners collection using the specified array...
Definition: debug.h:272
Represents a common dialog box that displays assert dialog.
Definition: assert_dialog.h:33
static void write(const object_t &message)
Writes a message to the trace listeners in the listeners collection.
Definition: debug.h:295
uint_least32_t uint32
Represents a 32-bit unsigned integer.
Definition: types.h:239
Contains xtd::diagnostics::assert_dialog_result enum class.
static void write_line()
Writes a line terminator to the trace listeners in the listeners collection.
Definition: debug.h:370
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: debug.h:430
static void trace_information(const xtd::ustring &message, const objects_t &... args)
Writes an informational message to the trace listeners in the listeners collection using the specifie...
Definition: debug.h:253
The message box contains a symbol consisting of an exclamation point in a triangle with a yellow back...
trace_event_type
Identifies the type of event that has caused the trace.
Definition: trace_event_type.h:23