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 /* DEBUGFILE can be defined externally,
55  * typically through compiler command line.
56  * note : currently useless.
57  * Value must be stderr or stdout */
58 #ifndef DEBUGFILE
59 #  define DEBUGFILE stderr
60 #endif
61 
62 
63 /* recommended values for DEBUGLEVEL :
64  * 0 : release mode, no debug, all run-time checks disabled
65  * 1 : enables assert() only, no display
66  * 2 : reserved, for currently active debug path
67  * 3 : events once per object lifetime (CCtx, CDict, etc.)
68  * 4 : events once per frame
69  * 5 : events once per block
70  * 6 : events once per sequence (verbose)
71  * 7+: events at every position (*very* verbose)
72  *
73  * It's generally inconvenient to output traces > 5.
74  * In which case, it's possible to selectively trigger high verbosity levels
75  * by modifying g_debug_level.
76  */
77 
78 #if (DEBUGLEVEL>=1)
79 #  include <assert.h>
80 #else
81 #  ifndef assert   /* assert may be already defined, due to prior #include <assert.h> */
82 #    define assert(condition) ((void)0)   /* disable assert (default) */
83 #  endif
84 #endif
85 
86 #if (DEBUGLEVEL>=2)
87 #  include <stdio.h>
88 extern int g_debuglevel; /* the variable is only declared,
89                             it actually lives in debug.c,
90                             and is shared by the whole process.
91                             It's not thread-safe.
92                             It's useful when enabling very verbose levels
93                             on selective conditions (such as position in src) */
94 
95 #  define RAWLOG(l, ...) {                                      \
96                 if (l<=g_debuglevel) {                          \
97                     fprintf(stderr, __VA_ARGS__);               \
98             }   }
99 #  define DEBUGLOG(l, ...) {                                    \
100                 if (l<=g_debuglevel) {                          \
101                     fprintf(stderr, __FILE__ ": " __VA_ARGS__); \
102                     fprintf(stderr, " \n");                     \
103             }   }
104 #else
105 #  define RAWLOG(l, ...)      {}    /* disabled */
106 #  define DEBUGLOG(l, ...)    {}    /* disabled */
107 #endif
108 
109 
110 #if defined (__cplusplus)
111 }
112 #endif
113 
114 #endif /* DEBUG_H_12987983217 */
115