/* ******************************************************************1* debug2* Part of FSE library3* Copyright (c) Meta Platforms, Inc. and affiliates.4*5* You can contact the author at :6* - Source repository : https://github.com/Cyan4973/FiniteStateEntropy7*8* This source code is licensed under both the BSD-style license (found in the9* LICENSE file in the root directory of this source tree) and the GPLv2 (found10* in the COPYING file in the root directory of this source tree).11* You may select, at your option, one of the above-listed licenses.12****************************************************************** */131415/*16* The purpose of this header is to enable debug functions.17* They regroup assert(), DEBUGLOG() and RAWLOG() for run-time,18* and DEBUG_STATIC_ASSERT() for compile-time.19*20* By default, DEBUGLEVEL==0, which means run-time debug is disabled.21*22* Level 1 enables assert() only.23* Starting level 2, traces can be generated and pushed to stderr.24* The higher the level, the more verbose the traces.25*26* It's possible to dynamically adjust level using variable g_debug_level,27* which is only declared if DEBUGLEVEL>=2,28* and is a global variable, not multi-thread protected (use with care)29*/3031#ifndef DEBUG_H_1298798321732#define DEBUG_H_12987983217333435/* static assert is triggered at compile time, leaving no runtime artefact.36* static assert only works with compile-time constants.37* Also, this variant can only be used inside a function. */38#define DEBUG_STATIC_ASSERT(c) (void)sizeof(char[(c) ? 1 : -1])394041/* DEBUGLEVEL is expected to be defined externally,42* typically through compiler command line.43* Value must be a number. */44#ifndef DEBUGLEVEL45# define DEBUGLEVEL 046#endif474849/* recommended values for DEBUGLEVEL :50* 0 : release mode, no debug, all run-time checks disabled51* 1 : enables assert() only, no display52* 2 : reserved, for currently active debug path53* 3 : events once per object lifetime (CCtx, CDict, etc.)54* 4 : events once per frame55* 5 : events once per block56* 6 : events once per sequence (verbose)57* 7+: events at every position (*very* verbose)58*59* It's generally inconvenient to output traces > 5.60* In which case, it's possible to selectively trigger high verbosity levels61* by modifying g_debug_level.62*/6364#if (DEBUGLEVEL>=1)65# define ZSTD_DEPS_NEED_ASSERT66# include "zstd_deps.h"67#else68# ifndef assert /* assert may be already defined, due to prior #include <assert.h> */69# define assert(condition) ((void)0) /* disable assert (default) */70# endif71#endif7273#if (DEBUGLEVEL>=2)74# define ZSTD_DEPS_NEED_IO75# include "zstd_deps.h"76extern int g_debuglevel; /* the variable is only declared,77it actually lives in debug.c,78and is shared by the whole process.79It's not thread-safe.80It's useful when enabling very verbose levels81on selective conditions (such as position in src) */8283# define RAWLOG(l, ...) \84do { \85if (l<=g_debuglevel) { \86ZSTD_DEBUG_PRINT(__VA_ARGS__); \87} \88} while (0)8990#define STRINGIFY(x) #x91#define TOSTRING(x) STRINGIFY(x)92#define LINE_AS_STRING TOSTRING(__LINE__)9394# define DEBUGLOG(l, ...) \95do { \96if (l<=g_debuglevel) { \97ZSTD_DEBUG_PRINT(__FILE__ ":" LINE_AS_STRING ": " __VA_ARGS__); \98ZSTD_DEBUG_PRINT(" \n"); \99} \100} while (0)101#else102# define RAWLOG(l, ...) do { } while (0) /* disabled */103# define DEBUGLOG(l, ...) do { } while (0) /* disabled */104#endif105106#endif /* DEBUG_H_12987983217 */107108109