trace.h 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. #pragma once
  2. #include "debug.h"
  3. #include <util/system/defaults.h>
  4. /**
  5. * Debug level, as set via `DBGOUT` environment variable.
  6. */
  7. enum ETraceLevel: ui8 {
  8. TRACE_ERR = 1,
  9. TRACE_WARN = 2,
  10. TRACE_NOTICE = 3,
  11. TRACE_INFO = 4,
  12. TRACE_DEBUG = 5,
  13. TRACE_DETAIL = 6,
  14. TRACE_VERBOSE = 7
  15. };
  16. #if !defined(NDEBUG) && !defined(Y_ENABLE_TRACE)
  17. #define Y_ENABLE_TRACE
  18. #endif
  19. /**
  20. * Writes the given data into standard debug stream if current debug level set
  21. * via `DBGOUT` environment variable permits it.
  22. *
  23. * Example usage:
  24. * @code
  25. * Y_DBGTRACE(DEBUG, "Advance from " << node1 << " to " << node2);
  26. * @endcode
  27. *
  28. * @param elevel Debug level of this trace command, e.g.
  29. * `WARN` or `DEBUG`. Basically a suffix of
  30. * one of the values of `ETraceLevel` enum.
  31. * @param args Argument chain to be written out into
  32. * standard debug stream, joined with `<<`
  33. * operator.
  34. * @see ETraceLevel
  35. */
  36. #define Y_DBGTRACE(elevel, args) Y_DBGTRACE0(int(TRACE_##elevel), args)
  37. #define Y_DBGTRACE0(level, args) \
  38. do { \
  39. if constexpr (Y_IS_DEBUG_BUILD) { \
  40. if ((level) <= StdDbgLevel()) { \
  41. StdDbgStream() << args << Endl; \
  42. } \
  43. } \
  44. } while (false)