debug.h 3.9 KB

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