debug.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. /* static assert is triggered at compile time, leaving no runtime artefact.
  32. * static assert only works with compile-time constants.
  33. * Also, this variant can only be used inside a function. */
  34. #define DEBUG_STATIC_ASSERT(c) (void)sizeof(char[(c) ? 1 : -1])
  35. /* DEBUGLEVEL is expected to be defined externally,
  36. * typically through compiler command line.
  37. * Value must be a number. */
  38. #ifndef DEBUGLEVEL
  39. # define DEBUGLEVEL 0
  40. #endif
  41. /* recommended values for DEBUGLEVEL :
  42. * 0 : release mode, no debug, all run-time checks disabled
  43. * 1 : enables assert() only, no display
  44. * 2 : reserved, for currently active debug path
  45. * 3 : events once per object lifetime (CCtx, CDict, etc.)
  46. * 4 : events once per frame
  47. * 5 : events once per block
  48. * 6 : events once per sequence (verbose)
  49. * 7+: events at every position (*very* verbose)
  50. *
  51. * It's generally inconvenient to output traces > 5.
  52. * In which case, it's possible to selectively trigger high verbosity levels
  53. * by modifying g_debug_level.
  54. */
  55. #if (DEBUGLEVEL>=1)
  56. # define ZSTD_DEPS_NEED_ASSERT
  57. # include "zstd_deps.h"
  58. #else
  59. # ifndef assert /* assert may be already defined, due to prior #include <assert.h> */
  60. # define assert(condition) ((void)0) /* disable assert (default) */
  61. # endif
  62. #endif
  63. #if (DEBUGLEVEL>=2)
  64. # define ZSTD_DEPS_NEED_IO
  65. # include "zstd_deps.h"
  66. extern int g_debuglevel; /* the variable is only declared,
  67. it actually lives in debug.c,
  68. and is shared by the whole process.
  69. It's not thread-safe.
  70. It's useful when enabling very verbose levels
  71. on selective conditions (such as position in src) */
  72. # define RAWLOG(l, ...) \
  73. do { \
  74. if (l<=g_debuglevel) { \
  75. ZSTD_DEBUG_PRINT(__VA_ARGS__); \
  76. } \
  77. } while (0)
  78. #define STRINGIFY(x) #x
  79. #define TOSTRING(x) STRINGIFY(x)
  80. #define LINE_AS_STRING TOSTRING(__LINE__)
  81. # define DEBUGLOG(l, ...) \
  82. do { \
  83. if (l<=g_debuglevel) { \
  84. ZSTD_DEBUG_PRINT(__FILE__ ":" LINE_AS_STRING ": " __VA_ARGS__); \
  85. ZSTD_DEBUG_PRINT(" \n"); \
  86. } \
  87. } while (0)
  88. #else
  89. # define RAWLOG(l, ...) do { } while (0) /* disabled */
  90. # define DEBUGLOG(l, ...) do { } while (0) /* disabled */
  91. #endif
  92. #endif /* DEBUG_H_12987983217 */