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.
debug.h
Go to the documentation of this file.
1 #pragma once
5 #include <cstdint>
6 #include <cstdlib>
7 #include <mutex>
8 #include "../add_last_arg_to_command.h"
9 #include "../core_export.h"
10 #include "../environment.h"
11 #include "../static.h"
12 #include "../ustring.h"
13 #include "assert_dialog_result.h"
14 #include "debugger.h"
15 #include "stack_trace.h"
17 
19 namespace xtd {
21  namespace diagnostics {
23  class trace;
46  public:
49 
54  static bool auto_flush();
59  static void auto_flush(bool auto_flush);
60 
64  static uint32_t indent_level();
68  static void indent_level(uint32_t indent_level);
69 
73  static uint32_t indent_size();
77  static void indent_size(uint32_t indent_size);
78 
88  static void listeners(const listener_collection& listeners);
89 
94  static bool show_assert_dialog();
99  static void show_assert_dialog(bool show_assert_dialog);
100 
104  static bool use_global_lock();
108  static void use_global_lock(bool use_global_lock);
109 
115  static void cassert(bool condition);
119  static void cassert(bool condition, const xtd::ustring& message);
124  static void cassert(bool condition, const xtd::ustring& message, const xtd::diagnostics::stack_frame& stack_frame);
128  static void cassert(bool condition, const xtd::diagnostics::stack_frame& stack_frame);
129 
134  static void fail(const xtd::ustring& message) {
135 #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
136  fail__(message);
137 #endif
138  }
144  static void fail(const xtd::ustring& message, const xtd::ustring& detail_message) {
145 #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
146  fail__(message, detail_message);
147 #endif
148  }
149 
151  static void flush() {
152 #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
153  flush_();
154 #endif
155  }
156 
158  static void indent();
159 
163  static void print(const xtd::ustring& message) {
164 #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
165  write_line_(message);
166 #endif
167  }
172  template<typename ...args_t>
173  static void print(const xtd::ustring& format, args_t&&... args) {
174 #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
175  write_line_(xtd::ustring::format(format, args...));
176 #endif
177  }
179  template<typename ...args_t>
180  static void print(const char* format, args_t&&... args) {
181 #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
182  write_line_(xtd::ustring::format(format, args...));
183 #endif
184  }
186 
190  static void trace_error(const xtd::ustring& message) {
191 #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
192  trace_event_(trace_event_type::error, message);
193 #endif
194  }
199  template<typename ...objects_t>
200  static void trace_error(const xtd::ustring& message, const objects_t& ... args) {
201 #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
202  trace_event_(trace_event_type::error, xtd::ustring::format(message, args...));
203 #endif
204  }
205 
209  static void trace_information(const xtd::ustring& message) {
210 #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
211  trace_event_(trace_event_type::information, message);
212 #endif
213  }
218  template<typename ...objects_t>
219  static void trace_information(const xtd::ustring& message, const objects_t& ... args) {
220 #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
221  trace_event_(trace_event_type::information, xtd::ustring::format(message, args...));
222 #endif
223  }
224 
228  static void trace_warning(const xtd::ustring& message) {
229 #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
230  trace_event_(trace_event_type::warning, message);
231 #endif
232  }
237  template<typename ...objects_t>
238  static void trace_warning(const xtd::ustring& message, const objects_t& ... args) {
239 #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
240  trace_event_(trace_event_type::warning, xtd::ustring::format(message, args...));
241 #endif
242  }
243 
245  static void unindent();
246 
251  static void write(const xtd::ustring& message) {
252 #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
253  write_(message);
254 #endif
255  }
260  template<typename object_t>
261  static void write(const object_t& message) {
262 #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
263  write_(xtd::ustring::format("", message));
264 #endif
265  }
272  template<typename object_t>
273  static void write(const object_t& message, const xtd::ustring& category) {
274 #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
275  write_(xtd::ustring::format("", message), category);
276 #endif
277  }
283  template<typename ...args_t>
284  static void write(const xtd::ustring& format, args_t&&... args) {
285 #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
286  write_(ustring::format(format, args...));
287 #endif
288  }
290  template<typename ...args_t>
291  static void write(const char* format, args_t&&... args) {
292 #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
293  write_(ustring::format(format, args...));
294 #endif
295  }
297 
303  static void write_if(bool condition, const xtd::ustring& message) {
304 #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
305  if (condition) write_(message);
306 #endif
307  }
308  template<typename object_t>
314  static void write_if(bool condition, const object_t& message) {
315 #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
316  if (condition) write_(xtd::ustring::format("", message));
317 #endif
318  }
325  template<typename object_t>
326  static void write_if(bool condition, const object_t& message, const xtd::ustring& category) {
327 #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
328  if (condition) write_(xtd::ustring::format("", message), category);
329 #endif
330  }
331 
336  static void write_line() {
337 #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
338  write_line_("");
339 #endif
340  }
345  static void write_line(const xtd::ustring& message) {
346 #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
347  write_line_(message);
348 #endif
349  }
354  template<typename object_t>
355  static void write_line(const object_t& message) {
356 #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
357  write_line_(xtd::ustring::format("", message));
358 #endif
359  }
366  template<typename object_t>
367  static void write_line(const object_t& message, const xtd::ustring& category) {
368 #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
369  write_line_(xtd::ustring::format("", message), category);
370 #endif
371  }
376  template<typename ...args_t>
377  static void write_line(const xtd::ustring& format, args_t&&... args) {
378 #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
379  write_line_(ustring::format(format, args...));
380 #endif
381  }
383  template<typename ...args_t>
384  static void write_line(const char* format, args_t&&... args) {
385 #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
386  write_line_(ustring::format(format, args...));
387 #endif
388  }
390 
396  static void write_line_if(bool condition, const xtd::ustring& message) {
397 #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
398  if (condition) write_line_(message);
399 #endif
400  }
406  template<typename object_t>
407  static void write_line_if(bool condition, const object_t& message) {
408 #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
409  if (condition) write_line_(message);
410 #endif
411  }
418  template<typename object_t>
419  static void write_line_if(bool condition, const object_t& message, const xtd::ustring& category) {
420 #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
421  if (condition) write_line_(message, category);
422 #endif
423  }
424 
426  static inline bool __should_aborted__(bool condition) {return __should_aborted__(condition, "", csf_);}
427  static inline bool __should_aborted__(bool condition, const xtd::ustring& message) {return __should_aborted__(condition, message, csf_);}
428  static inline bool __should_aborted__(bool condition, const xtd::diagnostics::stack_frame& stack_frame) {return __should_aborted__(condition, "", stack_frame);}
429  static inline bool __should_aborted__(bool condition, const xtd::ustring& message, const xtd::diagnostics::stack_frame& stack_frame) {
430 #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
431  auto result = xtd::diagnostics::debug::assert_dialog(condition, message, stack_frame);
433  if (result == xtd::diagnostics::assert_dialog_result::retry) return true;
434 #endif
435  return false;
436  }
438 
439  private:
440  friend trace;
441  static xtd::diagnostics::assert_dialog_result assert_dialog(bool condition, const xtd::ustring& message, const xtd::diagnostics::stack_frame& stack_frame);
442  static void fail__(const xtd::ustring& message);
443  static void fail__(const xtd::ustring& message, const xtd::ustring& detail_message);
444  static void flush_();
445  static void trace_event_(trace_event_type trace_event_type, const xtd::ustring& message);
446  static void write_(const xtd::ustring& message);
447  static void write_(const xtd::ustring& message, const xtd::ustring& category);
448  static void write_line_(const xtd::ustring& message);
449  static void write_line_(const xtd::ustring& message, const xtd::ustring& category);
450 
451  inline static bool auto_flush_ = false;
452  inline static unsigned int indent_level_ = 0;
453  inline static unsigned int indent_size_ = 4;
454  static listener_collection& listeners_;
455  static bool& show_assert_dialog_;
456  inline static bool use_global_lock_ = true;
457  static std::mutex global_lock_;
458  static xtd::ustring source_name_;
459  };
460  }
461 }
462 
479 #define cassert_(...) \
480  add_last_arg_to_command_(cassert, (csf_), __VA_ARGS__)
481 
494 #define assert_(...) \
495  if (xtd::diagnostics::debug::add_last_arg_to_command_(__should_aborted__, (csf_), __VA_ARGS__)) debug_break_()
Contains xtd::diagnostics::assert_dialog_result enum class.
Provides a set of methods and properties that help you debug the execution of your code....
Definition: debug.h:45
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:303
static bool show_assert_dialog()
Gets a value indicating whether the assert dialog should be show.
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:367
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:209
static uint32_t indent_level()
Gets the indent level.
static void indent()
Increases the current indent_level by one.
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:396
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 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:419
static listener_collection & listeners()
Gets the collection of listeners that is monitoring the trace output.
static void fail(const xtd::ustring &message)
Emits the specified error message.
Definition: debug.h:134
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:173
static void write_line()
Writes a line terminator to the trace listeners in the listeners collection.
Definition: debug.h:336
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:190
static void listeners(const listener_collection &listeners)
Sets the collection of listeners that is monitoring the trace output. @paral$m listeners A xtd::diagn...
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:219
static void write(const xtd::ustring &message)
Writes a message to the trace listeners in the listeners collection.
Definition: debug.h:251
static void indent_level(uint32_t indent_level)
Sets the indent level.
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:407
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:355
static void show_assert_dialog(bool show_assert_dialog)
Sets a value indicating whether the assert dialog should be show.
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:284
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:238
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:326
static void auto_flush(bool auto_flush)
Sets whether xtd::diagnostics::debug::flush should be called on the xtd::diagnostics::debug::Listener...
static uint32_t indent_size()
Gets the number of spaces in an indent.
static bool use_global_lock()
Gets a value indicating whether the global lock should be used.
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:228
static void unindent()
Decreases the current indent_level by one.
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:314
static void indent_size(uint32_t indent_size)
Sets the number of spaces in an indent.
static bool auto_flush()
Gets whether xtd::diagnostics::debug::flush should be called on the xtd::diagnostics::debug::Listener...
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:377
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:273
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:345
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 write(const object_t &message)
Writes a message to the trace listeners in the listeners collection.
Definition: debug.h:261
static void flush()
Flushes the output buffer and causes buffered data to write to the listeners collection.
Definition: debug.h:151
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:200
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 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 fail(const xtd::ustring &message, const xtd::ustring &detail_message)
Emits an error message and a detailed error message.
Definition: debug.h:144
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:163
static void use_global_lock(bool use_global_lock)
Sets a value indicating whether the global lock should be used.
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
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::debugger 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
assert_dialog_result
Specifies identifiers to indicate the return value of an assert dialog box.
Definition: assert_dialog_result.h:18
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
Contains xtd::diagnostics::stack_trace class.
Contains xtd::diagnostics::trace_listener_collection class.