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