You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

debug.h 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /* ******************************************************************
  2. * debug
  3. * Part of FSE library
  4. * Copyright (c) 2013-2020, Yann Collet, Facebook, Inc.
  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. /* DEBUGFILE can be defined externally,
  45. * typically through compiler command line.
  46. * note : currently useless.
  47. * Value must be stderr or stdout */
  48. #ifndef DEBUGFILE
  49. # define DEBUGFILE stderr
  50. #endif
  51. /* recommended values for DEBUGLEVEL :
  52. * 0 : release mode, no debug, all run-time checks disabled
  53. * 1 : enables assert() only, no display
  54. * 2 : reserved, for currently active debug path
  55. * 3 : events once per object lifetime (CCtx, CDict, etc.)
  56. * 4 : events once per frame
  57. * 5 : events once per block
  58. * 6 : events once per sequence (verbose)
  59. * 7+: events at every position (*very* verbose)
  60. *
  61. * It's generally inconvenient to output traces > 5.
  62. * In which case, it's possible to selectively trigger high verbosity levels
  63. * by modifying g_debug_level.
  64. */
  65. #if (DEBUGLEVEL>=1)
  66. # include <assert.h>
  67. #else
  68. # ifndef assert /* assert may be already defined, due to prior #include <assert.h> */
  69. # define assert(condition) ((void)0) /* disable assert (default) */
  70. # endif
  71. #endif
  72. #if (DEBUGLEVEL>=2)
  73. # include <stdio.h>
  74. extern int g_debuglevel; /* the variable is only declared,
  75. it actually lives in debug.c,
  76. and is shared by the whole process.
  77. It's not thread-safe.
  78. It's useful when enabling very verbose levels
  79. on selective conditions (such as position in src) */
  80. # define RAWLOG(l, ...) { \
  81. if (l<=g_debuglevel) { \
  82. fprintf(stderr, __VA_ARGS__); \
  83. } }
  84. # define DEBUGLOG(l, ...) { \
  85. if (l<=g_debuglevel) { \
  86. fprintf(stderr, __FILE__ ": " __VA_ARGS__); \
  87. fprintf(stderr, " \n"); \
  88. } }
  89. #else
  90. # define RAWLOG(l, ...) {} /* disabled */
  91. # define DEBUGLOG(l, ...) {} /* disabled */
  92. #endif
  93. #if defined (__cplusplus)
  94. }
  95. #endif
  96. #endif /* DEBUG_H_12987983217 */