xtd - Reference Guide 0.2.0
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;
50  class core_export_ debug static_ {
51  public:
53 
58 
60 
66  static bool auto_flush();
71  static void auto_flush(bool auto_flush);
72 
76  static uint32_t indent_level();
80  static void indent_level(uint32_t indent_level);
81 
85  static uint32_t indent_size();
89  static void indent_size(uint32_t indent_size);
90 
95  static listener_collection& listeners();
100  static void listeners(const listener_collection& listeners);
101 
106  static bool show_assert_dialog();
111  static void show_assert_dialog(bool show_assert_dialog);
112 
116  static bool use_global_lock();
120  static void use_global_lock(bool use_global_lock);
122 
124 
131  static void cassert(bool condition);
135  static void cassert(bool condition, const xtd::ustring& message);
140  static void cassert(bool condition, const xtd::ustring& message, const xtd::diagnostics::stack_frame& stack_frame);
144  static void cassert(bool condition, const xtd::diagnostics::stack_frame& stack_frame);
145 
150  static void fail(const xtd::ustring& message) {
151  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
152  fail__(message);
153  #endif
154  }
160  static void fail(const xtd::ustring& message, const xtd::ustring& detail_message) {
161  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
162  fail__(message, detail_message);
163  #endif
164  }
165 
167  static void flush() {
168  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
169  flush_();
170  #endif
171  }
172 
174  static void indent();
175 
179  static void print(const xtd::ustring& message) {
180  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
181  write_line_(message);
182  #endif
183  }
188  template<typename ...args_t>
189  static void print(const xtd::ustring& format, args_t&& ... args) {
190  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
191  write_line_(xtd::ustring::format(format, args...));
192  #endif
193  }
195  template<typename ...args_t>
196  static void print(const char* format, args_t&& ... args) {
197  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
198  write_line_(xtd::ustring::format(format, args...));
199  #endif
200  }
202 
206  static void trace_error(const xtd::ustring& message) {
207  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
208  trace_event_(trace_event_type::error, message);
209  #endif
210  }
215  template<typename ...objects_t>
216  static void trace_error(const xtd::ustring& message, const objects_t& ... args) {
217  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
218  trace_event_(trace_event_type::error, xtd::ustring::format(message, args...));
219  #endif
220  }
221 
225  static void trace_information(const xtd::ustring& message) {
226  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
227  trace_event_(trace_event_type::information, message);
228  #endif
229  }
234  template<typename ...objects_t>
235  static void trace_information(const xtd::ustring& message, const objects_t& ... args) {
236  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
237  trace_event_(trace_event_type::information, xtd::ustring::format(message, args...));
238  #endif
239  }
240 
244  static void trace_warning(const xtd::ustring& message) {
245  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
246  trace_event_(trace_event_type::warning, message);
247  #endif
248  }
253  template<typename ...objects_t>
254  static void trace_warning(const xtd::ustring& message, const objects_t& ... args) {
255  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
256  trace_event_(trace_event_type::warning, xtd::ustring::format(message, args...));
257  #endif
258  }
259 
261  static void unindent();
262 
267  static void write(const xtd::ustring& message) {
268  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
269  write_(message);
270  #endif
271  }
276  template<typename object_t>
277  static void write(const object_t& message) {
278  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
279  write_(xtd::ustring::format("", message));
280  #endif
281  }
288  template<typename object_t>
289  static void write(const object_t& message, const xtd::ustring& category) {
290  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
291  write_(xtd::ustring::format("", message), category);
292  #endif
293  }
299  template<typename ...args_t>
300  static void write(const xtd::ustring& format, args_t&& ... args) {
301  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
302  write_(ustring::format(format, args...));
303  #endif
304  }
306  template<typename ...args_t>
307  static void write(const char* format, args_t&& ... args) {
308  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
309  write_(ustring::format(format, args...));
310  #endif
311  }
313 
319  static void write_if(bool condition, const xtd::ustring& message) {
320  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
321  if (condition) write_(message);
322  #endif
323  }
324  template<typename object_t>
330  static void write_if(bool condition, const object_t& message) {
331  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
332  if (condition) write_(xtd::ustring::format("", message));
333  #endif
334  }
341  template<typename object_t>
342  static void write_if(bool condition, const object_t& message, const xtd::ustring& category) {
343  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
344  if (condition) write_(xtd::ustring::format("", message), category);
345  #endif
346  }
347 
352  static void write_line() {
353  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
354  write_line_("");
355  #endif
356  }
361  static void write_line(const xtd::ustring& message) {
362  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
363  write_line_(message);
364  #endif
365  }
370  template<typename object_t>
371  static void write_line(const object_t& message) {
372  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
373  write_line_(xtd::ustring::format("", message));
374  #endif
375  }
382  template<typename object_t>
383  static void write_line(const object_t& message, const xtd::ustring& category) {
384  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
385  write_line_(xtd::ustring::format("", message), category);
386  #endif
387  }
392  template<typename ...args_t>
393  static void write_line(const xtd::ustring& format, args_t&& ... args) {
394  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
395  write_line_(ustring::format(format, args...));
396  #endif
397  }
399  template<typename ...args_t>
400  static void write_line(const char* format, args_t&& ... args) {
401  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
402  write_line_(ustring::format(format, args...));
403  #endif
404  }
406 
412  static void write_line_if(bool condition, const xtd::ustring& message) {
413  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
414  if (condition) write_line_(message);
415  #endif
416  }
422  template<typename object_t>
423  static void write_line_if(bool condition, const object_t& message) {
424  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
425  if (condition) write_line_(message);
426  #endif
427  }
434  template<typename object_t>
435  static void write_line_if(bool condition, const object_t& message, const xtd::ustring& category) {
436  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
437  if (condition) write_line_(message, category);
438  #endif
439  }
440 
442  static inline bool __should_aborted__(bool condition) {return __should_aborted__(condition, "", csf_);}
443  static inline bool __should_aborted__(bool condition, const xtd::ustring& message) {return __should_aborted__(condition, message, csf_);}
444  static inline bool __should_aborted__(bool condition, const xtd::diagnostics::stack_frame& stack_frame) {return __should_aborted__(condition, "", stack_frame);}
445  static inline bool __should_aborted__(bool condition, const xtd::ustring& message, const xtd::diagnostics::stack_frame& stack_frame) {
446  #if !defined(NDEBUG) || defined(DEBUG) || defined(TRACE)
447  auto result = xtd::diagnostics::debug::assert_dialog(condition, message, stack_frame);
449  if (result == xtd::diagnostics::assert_dialog_result::retry) return true;
450  #endif
451  return false;
452  }
455 
456  private:
457  friend trace;
458  static xtd::diagnostics::assert_dialog_result assert_dialog(bool condition, const xtd::ustring& message, const xtd::diagnostics::stack_frame& stack_frame);
459  static void fail__(const xtd::ustring& message);
460  static void fail__(const xtd::ustring& message, const xtd::ustring& detail_message);
461  static void flush_();
462  static void trace_event_(trace_event_type trace_event_type, const xtd::ustring& message);
463  static void write_(const xtd::ustring& message);
464  static void write_(const xtd::ustring& message, const xtd::ustring& category);
465  static void write_line_(const xtd::ustring& message);
466  static void write_line_(const xtd::ustring& message, const xtd::ustring& category);
467 
468  inline static bool auto_flush_ = false;
469  inline static unsigned int indent_level_ = 0;
470  inline static unsigned int indent_size_ = 4;
471  static listener_collection& listeners_;
472  static bool& show_assert_dialog_;
473  inline static bool use_global_lock_ = true;
474  static std::mutex global_lock_;
475  static xtd::ustring source_name_;
476  };
477  }
478 }
479 
496 #define cassert_(...) \
497  add_last_arg_to_command_(cassert, (csf_), __VA_ARGS__)
498 
511 #define assert_(...) \
512  if (xtd::diagnostics::debug::add_last_arg_to_command_(__should_aborted__, (csf_), __VA_ARGS__)) debug_break_()
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:244
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:319
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:189
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:206
#define static_
This keyword is use to represent a static object. A static object can&#39;t be instantiated (constructors...
Definition: static.h:38
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:216
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:300
#define csf_
Provides information about the current stack frame.
Definition: stack_frame.h:231
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:342
Represents text as a sequence of UTF-8 code units.
Definition: ustring.h:50
Warning Noncritical problem.
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:423
The assert dialog return value is Abort (usually sent from a button labeled Abort).
static void fail(const xtd::ustring &message)
Emits the specified error message.
Definition: debug.h:150
assert_dialog_result
Specifies identifiers to indicate the return value of an assert dialog box.
Definition: assert_dialog_result.h:21
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:289
static void flush()
Flushes the output buffer and causes buffered data to write to the listeners collection.
Definition: debug.h:167
static void exit(int exit_code)
Terminates this process and returns an exit code to the operating system.
Definition: environment.h:342
Represents a collection of xtd::diagnostics::trace_listener.
Definition: trace_listener_collection.h:24
Provides information about a xtd::diagnostics::stack_frame, which represents a function call on the c...
Definition: stack_frame.h:34
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:330
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:361
static void fail(const xtd::ustring &message, const xtd::ustring &detail_message)
Emits an error message and a detailed error message.
Definition: debug.h:160
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:435
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:393
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:371
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:383
Provides a set of methods and properties that help you debug the execution of your code...
Definition: trace.h:36
Provides a set of methods and properties that help you debug the execution of your code...
Definition: debug.h:50
static void write(const xtd::ustring &message)
Writes a message to the trace listeners in the listeners collection.
Definition: debug.h:267
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:179
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:708
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:225
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:254
static void write(const object_t &message)
Writes a message to the trace listeners in the listeners collection.
Definition: debug.h:277
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:352
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:412
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:235
trace_event_type
Identifies the type of event that has caused the trace.
Definition: trace_event_type.h:21