trace.h 1.7 KB

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