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