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