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