Path: blob/main/sys/contrib/openzfs/module/zstd/lib/common/debug.h
48774 views
// SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0-only1/* ******************************************************************2* debug3* Part of FSE library4* Copyright (c) 2013-2020, Yann Collet, Facebook, Inc.5*6* You can contact the author at :7* - Source repository : https://github.com/Cyan4973/FiniteStateEntropy8*9* This source code is licensed under both the BSD-style license (found in the10* LICENSE file in the root directory of this source tree) and the GPLv2 (found11* 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****************************************************************** */141516/*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*/3132#ifndef DEBUG_H_1298798321733#define DEBUG_H_129879832173435#if defined (__cplusplus)36extern "C" {37#endif383940/* 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])444546/* DEBUGLEVEL is expected to be defined externally,47* typically through compiler command line.48* Value must be a number. */49#ifndef DEBUGLEVEL50# define DEBUGLEVEL 051#endif525354/* DEBUGFILE can be defined externally,55* typically through compiler command line.56* note : currently useless.57* Value must be stderr or stdout */58#ifndef DEBUGFILE59# define DEBUGFILE stderr60#endif616263/* recommended values for DEBUGLEVEL :64* 0 : release mode, no debug, all run-time checks disabled65* 1 : enables assert() only, no display66* 2 : reserved, for currently active debug path67* 3 : events once per object lifetime (CCtx, CDict, etc.)68* 4 : events once per frame69* 5 : events once per block70* 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 levels75* by modifying g_debug_level.76*/7778#if (DEBUGLEVEL>=1)79# include <assert.h>80#else81# ifndef assert /* assert may be already defined, due to prior #include <assert.h> */82# define assert(condition) ((void)0) /* disable assert (default) */83# endif84#endif8586#if (DEBUGLEVEL>=2)87# include <stdio.h>88extern int g_debuglevel; /* the variable is only declared,89it actually lives in debug.c,90and is shared by the whole process.91It's not thread-safe.92It's useful when enabling very verbose levels93on selective conditions (such as position in src) */9495# define RAWLOG(l, ...) { \96if (l<=g_debuglevel) { \97fprintf(stderr, __VA_ARGS__); \98} }99# define DEBUGLOG(l, ...) { \100if (l<=g_debuglevel) { \101fprintf(stderr, __FILE__ ": " __VA_ARGS__); \102fprintf(stderr, " \n"); \103} }104#else105# define RAWLOG(l, ...) {} /* disabled */106# define DEBUGLOG(l, ...) {} /* disabled */107#endif108109110#if defined (__cplusplus)111}112#endif113114#endif /* DEBUG_H_12987983217 */115116117