debug.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /* ******************************************************************
  2. debug
  3. Part of FSE library
  4. Copyright (C) 2013-present, Yann Collet.
  5. BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php)
  6. Redistribution and use in source and binary forms, with or without
  7. modification, are permitted provided that the following conditions are
  8. met:
  9. * Redistributions of source code must retain the above copyright
  10. notice, this list of conditions and the following disclaimer.
  11. * Redistributions in binary form must reproduce the above
  12. copyright notice, this list of conditions and the following disclaimer
  13. in the documentation and/or other materials provided with the
  14. distribution.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  16. "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  17. LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  18. A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  19. OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  22. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  23. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  25. OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. You can contact the author at :
  27. - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
  28. ****************************************************************** */
  29. /*
  30. * The purpose of this header is to enable debug functions.
  31. * They regroup assert(), DEBUGLOG() and RAWLOG() for run-time,
  32. * and DEBUG_STATIC_ASSERT() for compile-time.
  33. *
  34. * By default, DEBUGLEVEL==0, which means run-time debug is disabled.
  35. *
  36. * Level 1 enables assert() only.
  37. * Starting level 2, traces can be generated and pushed to stderr.
  38. * The higher the level, the more verbose the traces.
  39. *
  40. * It's possible to dynamically adjust level using variable g_debug_level,
  41. * which is only declared if DEBUGLEVEL>=2,
  42. * and is a global variable, not multi-thread protected (use with care)
  43. */
  44. #ifndef DEBUG_H_12987983217
  45. #define DEBUG_H_12987983217
  46. #if defined (__cplusplus)
  47. extern "C" {
  48. #endif
  49. /* static assert is triggered at compile time, leaving no runtime artefact.
  50. * static assert only works with compile-time constants.
  51. * Also, this variant can only be used inside a function. */
  52. #define DEBUG_STATIC_ASSERT(c) (void)sizeof(char[(c) ? 1 : -1])
  53. /* DEBUGLEVEL is expected to be defined externally,
  54. * typically through compiler command line.
  55. * Value must be a number. */
  56. #ifndef DEBUGLEVEL
  57. # define DEBUGLEVEL 0
  58. #endif
  59. /* DEBUGFILE can be defined externally,
  60. * typically through compiler command line.
  61. * note : currently useless.
  62. * Value must be stderr or stdout */
  63. #ifndef DEBUGFILE
  64. # define DEBUGFILE stderr
  65. #endif
  66. /* recommended values for DEBUGLEVEL :
  67. * 0 : release mode, no debug, all run-time checks disabled
  68. * 1 : enables assert() only, no display
  69. * 2 : reserved, for currently active debug path
  70. * 3 : events once per object lifetime (CCtx, CDict, etc.)
  71. * 4 : events once per frame
  72. * 5 : events once per block
  73. * 6 : events once per sequence (verbose)
  74. * 7+: events at every position (*very* verbose)
  75. *
  76. * It's generally inconvenient to output traces > 5.
  77. * In which case, it's possible to selectively trigger high verbosity levels
  78. * by modifying g_debug_level.
  79. */
  80. #if (DEBUGLEVEL>=1)
  81. # include <assert.h>
  82. #else
  83. # ifndef assert /* assert may be already defined, due to prior #include <assert.h> */
  84. # define assert(condition) ((void)0) /* disable assert (default) */
  85. # endif
  86. #endif
  87. #if (DEBUGLEVEL>=2)
  88. # include <stdio.h>
  89. extern int g_debuglevel; /* the variable is only declared,
  90. it actually lives in debug.c,
  91. and is shared by the whole process.
  92. It's not thread-safe.
  93. It's useful when enabling very verbose levels
  94. on selective conditions (such as position in src) */
  95. # define RAWLOG(l, ...) { \
  96. if (l<=g_debuglevel) { \
  97. fprintf(stderr, __VA_ARGS__); \
  98. } }
  99. # define DEBUGLOG(l, ...) { \
  100. if (l<=g_debuglevel) { \
  101. fprintf(stderr, __FILE__ ": " __VA_ARGS__); \
  102. fprintf(stderr, " \n"); \
  103. } }
  104. #else
  105. # define RAWLOG(l, ...) {} /* disabled */
  106. # define DEBUGLOG(l, ...) {} /* disabled */
  107. #endif
  108. #if defined (__cplusplus)
  109. }
  110. #endif
  111. #endif /* DEBUG_H_12987983217 */