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