debug.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. * but can only work with compile-time constants.
  51. * 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. /* recommended values for DEBUGLEVEL :
  60. * 0 : no debug, all run-time functions disabled
  61. * 1 : no display, enables assert() only
  62. * 2 : reserved, for currently active debug path
  63. * 3 : events once per object lifetime (CCtx, CDict, etc.)
  64. * 4 : events once per frame
  65. * 5 : events once per block
  66. * 6 : events once per sequence (verbose)
  67. * 7+: events at every position (*very* verbose)
  68. *
  69. * It's generally inconvenient to output traces > 5.
  70. * In which case, it's possible to selectively enable higher verbosity levels
  71. * by modifying g_debug_level.
  72. */
  73. #if (DEBUGLEVEL>=1)
  74. # include <assert.h>
  75. #else
  76. # ifndef assert /* assert may be already defined, due to prior #include <assert.h> */
  77. # define assert(condition) ((void)0) /* disable assert (default) */
  78. # endif
  79. #endif
  80. #if (DEBUGLEVEL>=2)
  81. # include <stdio.h>
  82. extern int g_debuglevel; /* here, this variable is only declared,
  83. it actually lives in debug.c,
  84. and is shared by the whole process.
  85. It's typically used to enable very verbose levels
  86. on selective conditions (such as position in src) */
  87. # define RAWLOG(l, ...) { \
  88. if (l<=g_debuglevel) { \
  89. fprintf(stderr, __VA_ARGS__); \
  90. } }
  91. # define DEBUGLOG(l, ...) { \
  92. if (l<=g_debuglevel) { \
  93. fprintf(stderr, __FILE__ ": " __VA_ARGS__); \
  94. fprintf(stderr, " \n"); \
  95. } }
  96. #else
  97. # define RAWLOG(l, ...) {} /* disabled */
  98. # define DEBUGLOG(l, ...) {} /* disabled */
  99. #endif
  100. #if defined (__cplusplus)
  101. }
  102. #endif
  103. #endif /* DEBUG_H_12987983217 */