Scott Baker | f579f13 | 2019-10-24 14:31:41 -0700 | [diff] [blame] | 1 | /* ****************************************************************** |
| 2 | debug |
| 3 | Part of FSE library |
| 4 | Copyright (C) 2013-present, Yann Collet. |
| 5 | |
| 6 | BSD 2-Clause License (http://www.opensource.org/licenses/bsd-license.php) |
| 7 | |
| 8 | Redistribution and use in source and binary forms, with or without |
| 9 | modification, are permitted provided that the following conditions are |
| 10 | met: |
| 11 | |
| 12 | * Redistributions of source code must retain the above copyright |
| 13 | notice, this list of conditions and the following disclaimer. |
| 14 | * Redistributions in binary form must reproduce the above |
| 15 | copyright notice, this list of conditions and the following disclaimer |
| 16 | in the documentation and/or other materials provided with the |
| 17 | distribution. |
| 18 | |
| 19 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 | |
| 31 | You can contact the author at : |
| 32 | - Source repository : https://github.com/Cyan4973/FiniteStateEntropy |
| 33 | ****************************************************************** */ |
| 34 | |
| 35 | |
| 36 | /* |
| 37 | * The purpose of this header is to enable debug functions. |
| 38 | * They regroup assert(), DEBUGLOG() and RAWLOG() for run-time, |
| 39 | * and DEBUG_STATIC_ASSERT() for compile-time. |
| 40 | * |
| 41 | * By default, DEBUGLEVEL==0, which means run-time debug is disabled. |
| 42 | * |
| 43 | * Level 1 enables assert() only. |
| 44 | * Starting level 2, traces can be generated and pushed to stderr. |
| 45 | * The higher the level, the more verbose the traces. |
| 46 | * |
| 47 | * It's possible to dynamically adjust level using variable g_debug_level, |
| 48 | * which is only declared if DEBUGLEVEL>=2, |
| 49 | * and is a global variable, not multi-thread protected (use with care) |
| 50 | */ |
| 51 | |
| 52 | #ifndef DEBUG_H_12987983217 |
| 53 | #define DEBUG_H_12987983217 |
| 54 | |
| 55 | #if defined (__cplusplus) |
| 56 | extern "C" { |
| 57 | #endif |
| 58 | |
| 59 | |
| 60 | /* static assert is triggered at compile time, leaving no runtime artefact. |
| 61 | * static assert only works with compile-time constants. |
| 62 | * Also, this variant can only be used inside a function. */ |
| 63 | #define DEBUG_STATIC_ASSERT(c) (void)sizeof(char[(c) ? 1 : -1]) |
| 64 | |
| 65 | |
| 66 | /* DEBUGLEVEL is expected to be defined externally, |
| 67 | * typically through compiler command line. |
| 68 | * Value must be a number. */ |
| 69 | #ifndef DEBUGLEVEL |
| 70 | # define DEBUGLEVEL 0 |
| 71 | #endif |
| 72 | |
| 73 | |
| 74 | /* DEBUGFILE can be defined externally, |
| 75 | * typically through compiler command line. |
| 76 | * note : currently useless. |
| 77 | * Value must be stderr or stdout */ |
| 78 | #ifndef DEBUGFILE |
| 79 | # define DEBUGFILE stderr |
| 80 | #endif |
| 81 | |
| 82 | |
| 83 | /* recommended values for DEBUGLEVEL : |
| 84 | * 0 : release mode, no debug, all run-time checks disabled |
| 85 | * 1 : enables assert() only, no display |
| 86 | * 2 : reserved, for currently active debug path |
| 87 | * 3 : events once per object lifetime (CCtx, CDict, etc.) |
| 88 | * 4 : events once per frame |
| 89 | * 5 : events once per block |
| 90 | * 6 : events once per sequence (verbose) |
| 91 | * 7+: events at every position (*very* verbose) |
| 92 | * |
| 93 | * It's generally inconvenient to output traces > 5. |
| 94 | * In which case, it's possible to selectively trigger high verbosity levels |
| 95 | * by modifying g_debug_level. |
| 96 | */ |
| 97 | |
| 98 | #if (DEBUGLEVEL>=1) |
| 99 | # include <assert.h> |
| 100 | #else |
| 101 | # ifndef assert /* assert may be already defined, due to prior #include <assert.h> */ |
| 102 | # define assert(condition) ((void)0) /* disable assert (default) */ |
| 103 | # endif |
| 104 | #endif |
| 105 | |
| 106 | #if (DEBUGLEVEL>=2) |
| 107 | # include <stdio.h> |
| 108 | extern int g_debuglevel; /* the variable is only declared, |
| 109 | it actually lives in debug.c, |
| 110 | and is shared by the whole process. |
| 111 | It's not thread-safe. |
| 112 | It's useful when enabling very verbose levels |
| 113 | on selective conditions (such as position in src) */ |
| 114 | |
| 115 | # define RAWLOG(l, ...) { \ |
| 116 | if (l<=g_debuglevel) { \ |
| 117 | fprintf(stderr, __VA_ARGS__); \ |
| 118 | } } |
| 119 | # define DEBUGLOG(l, ...) { \ |
| 120 | if (l<=g_debuglevel) { \ |
| 121 | fprintf(stderr, __FILE__ ": " __VA_ARGS__); \ |
| 122 | fprintf(stderr, " \n"); \ |
| 123 | } } |
| 124 | #else |
| 125 | # define RAWLOG(l, ...) {} /* disabled */ |
| 126 | # define DEBUGLOG(l, ...) {} /* disabled */ |
| 127 | #endif |
| 128 | |
| 129 | |
| 130 | #if defined (__cplusplus) |
| 131 | } |
| 132 | #endif |
| 133 | |
| 134 | #endif /* DEBUG_H_12987983217 */ |