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