1 /* ******************************************************************
2  * debug
3  * Part of FSE library
4  * Copyright (c) 2013-2020, Yann Collet, Facebook, Inc.
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 #if defined (__cplusplus)
36 extern "C" {
37 #endif
38 
39 
40 /* static assert is triggered at compile time, leaving no runtime artefact.
41  * static assert only works with compile-time constants.
42  * Also, this variant can only be used inside a function. */
43 #define DEBUG_STATIC_ASSERT(c) (void)sizeof(char[(c) ? 1 : -1])
44 
45 
46 /* DEBUGLEVEL is expected to be defined externally,
47  * typically through compiler command line.
48  * Value must be a number. */
49 #ifndef DEBUGLEVEL
50 #  define DEBUGLEVEL 0
51 #endif
52 
53 
54 /* recommended values for DEBUGLEVEL :
55  * 0 : release mode, no debug, all run-time checks disabled
56  * 1 : enables assert() only, no display
57  * 2 : reserved, for currently active debug path
58  * 3 : events once per object lifetime (CCtx, CDict, etc.)
59  * 4 : events once per frame
60  * 5 : events once per block
61  * 6 : events once per sequence (verbose)
62  * 7+: events at every position (*very* verbose)
63  *
64  * It's generally inconvenient to output traces > 5.
65  * In which case, it's possible to selectively trigger high verbosity levels
66  * by modifying g_debug_level.
67  */
68 
69 #if (DEBUGLEVEL>=1)
70 #  define ZSTD_DEPS_NEED_ASSERT
71 #  include "zstd_deps.h"
72 #else
73 #  ifndef assert   /* assert may be already defined, due to prior #include <assert.h> */
74 #    define assert(condition) ((void)0)   /* disable assert (default) */
75 #  endif
76 #endif
77 
78 #if (DEBUGLEVEL>=2)
79 #  define ZSTD_DEPS_NEED_IO
80 #  include "zstd_deps.h"
81 extern int g_debuglevel; /* the variable is only declared,
82                             it actually lives in debug.c,
83                             and is shared by the whole process.
84                             It's not thread-safe.
85                             It's useful when enabling very verbose levels
86                             on selective conditions (such as position in src) */
87 
88 #  define RAWLOG(l, ...) {                                       \
89                 if (l<=g_debuglevel) {                           \
90                     ZSTD_DEBUG_PRINT(__VA_ARGS__);               \
91             }   }
92 #  define DEBUGLOG(l, ...) {                                     \
93                 if (l<=g_debuglevel) {                           \
94                     ZSTD_DEBUG_PRINT(__FILE__ ": " __VA_ARGS__); \
95                     ZSTD_DEBUG_PRINT(" \n");                     \
96             }   }
97 #else
98 #  define RAWLOG(l, ...)      {}    /* disabled */
99 #  define DEBUGLOG(l, ...)    {}    /* disabled */
100 #endif
101 
102 
103 #if defined (__cplusplus)
104 }
105 #endif
106 
107 #endif /* DEBUG_H_12987983217 */
108