debug.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* ******************************************************************
  2. * debug
  3. * Part of FSE library
  4. * Copyright (c) Meta Platforms, Inc. and affiliates.
  5. *
  6. * You can contact the author at :
  7. * - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
  8. *
  9. * This source code is licensed under both the BSD-style license (found in the
  10. * LICENSE file in the root directory of this source tree) and the GPLv2 (found
  11. * in the COPYING file in the root directory of this source tree).
  12. * You may select, at your option, one of the above-listed licenses.
  13. ****************************************************************** */
  14. /*
  15. * The purpose of this header is to enable debug functions.
  16. * They regroup assert(), DEBUGLOG() and RAWLOG() for run-time,
  17. * and DEBUG_STATIC_ASSERT() for compile-time.
  18. *
  19. * By default, DEBUGLEVEL==0, which means run-time debug is disabled.
  20. *
  21. * Level 1 enables assert() only.
  22. * Starting level 2, traces can be generated and pushed to stderr.
  23. * The higher the level, the more verbose the traces.
  24. *
  25. * It's possible to dynamically adjust level using variable g_debug_level,
  26. * which is only declared if DEBUGLEVEL>=2,
  27. * and is a global variable, not multi-thread protected (use with care)
  28. */
  29. #ifndef DEBUG_H_12987983217
  30. #define DEBUG_H_12987983217
  31. #if defined (__cplusplus)
  32. extern "C" {
  33. #endif
  34. /* static assert is triggered at compile time, leaving no runtime artefact.
  35. * static assert only works with compile-time constants.
  36. * Also, this variant can only be used inside a function. */
  37. #define DEBUG_STATIC_ASSERT(c) (void)sizeof(char[(c) ? 1 : -1])
  38. /* DEBUGLEVEL is expected to be defined externally,
  39. * typically through compiler command line.
  40. * Value must be a number. */
  41. #ifndef DEBUGLEVEL
  42. # define DEBUGLEVEL 0
  43. #endif
  44. /* recommended values for DEBUGLEVEL :
  45. * 0 : release mode, no debug, all run-time checks disabled
  46. * 1 : enables assert() only, no display
  47. * 2 : reserved, for currently active debug path
  48. * 3 : events once per object lifetime (CCtx, CDict, etc.)
  49. * 4 : events once per frame
  50. * 5 : events once per block
  51. * 6 : events once per sequence (verbose)
  52. * 7+: events at every position (*very* verbose)
  53. *
  54. * It's generally inconvenient to output traces > 5.
  55. * In which case, it's possible to selectively trigger high verbosity levels
  56. * by modifying g_debug_level.
  57. */
  58. #if (DEBUGLEVEL>=1)
  59. # define ZSTD_DEPS_NEED_ASSERT
  60. # include "zstd_deps.h"
  61. #else
  62. # ifndef assert /* assert may be already defined, due to prior #include <assert.h> */
  63. # define assert(condition) ((void)0) /* disable assert (default) */
  64. # endif
  65. #endif
  66. #if (DEBUGLEVEL>=2)
  67. # define ZSTD_DEPS_NEED_IO
  68. # include "zstd_deps.h"
  69. extern int g_debuglevel; /* the variable is only declared,
  70. it actually lives in debug.c,
  71. and is shared by the whole process.
  72. It's not thread-safe.
  73. It's useful when enabling very verbose levels
  74. on selective conditions (such as position in src) */
  75. # define RAWLOG(l, ...) \
  76. do { \
  77. if (l<=g_debuglevel) { \
  78. ZSTD_DEBUG_PRINT(__VA_ARGS__); \
  79. } \
  80. } while (0)
  81. #define STRINGIFY(x) #x
  82. #define TOSTRING(x) STRINGIFY(x)
  83. #define LINE_AS_STRING TOSTRING(__LINE__)
  84. # define DEBUGLOG(l, ...) \
  85. do { \
  86. if (l<=g_debuglevel) { \
  87. ZSTD_DEBUG_PRINT(__FILE__ ":" LINE_AS_STRING ": " __VA_ARGS__); \
  88. ZSTD_DEBUG_PRINT(" \n"); \
  89. } \
  90. } while (0)
  91. #else
  92. # define RAWLOG(l, ...) do { } while (0) /* disabled */
  93. # define DEBUGLOG(l, ...) do { } while (0) /* disabled */
  94. #endif
  95. #if defined (__cplusplus)
  96. }
  97. #endif
  98. #endif /* DEBUG_H_12987983217 */