162306a36Sopenharmony_ci/* ******************************************************************
262306a36Sopenharmony_ci * debug
362306a36Sopenharmony_ci * Part of FSE library
462306a36Sopenharmony_ci * Copyright (c) Yann Collet, Facebook, Inc.
562306a36Sopenharmony_ci *
662306a36Sopenharmony_ci * You can contact the author at :
762306a36Sopenharmony_ci * - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
862306a36Sopenharmony_ci *
962306a36Sopenharmony_ci * This source code is licensed under both the BSD-style license (found in the
1062306a36Sopenharmony_ci * LICENSE file in the root directory of this source tree) and the GPLv2 (found
1162306a36Sopenharmony_ci * in the COPYING file in the root directory of this source tree).
1262306a36Sopenharmony_ci * You may select, at your option, one of the above-listed licenses.
1362306a36Sopenharmony_ci****************************************************************** */
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ci
1662306a36Sopenharmony_ci/*
1762306a36Sopenharmony_ci * The purpose of this header is to enable debug functions.
1862306a36Sopenharmony_ci * They regroup assert(), DEBUGLOG() and RAWLOG() for run-time,
1962306a36Sopenharmony_ci * and DEBUG_STATIC_ASSERT() for compile-time.
2062306a36Sopenharmony_ci *
2162306a36Sopenharmony_ci * By default, DEBUGLEVEL==0, which means run-time debug is disabled.
2262306a36Sopenharmony_ci *
2362306a36Sopenharmony_ci * Level 1 enables assert() only.
2462306a36Sopenharmony_ci * Starting level 2, traces can be generated and pushed to stderr.
2562306a36Sopenharmony_ci * The higher the level, the more verbose the traces.
2662306a36Sopenharmony_ci *
2762306a36Sopenharmony_ci * It's possible to dynamically adjust level using variable g_debug_level,
2862306a36Sopenharmony_ci * which is only declared if DEBUGLEVEL>=2,
2962306a36Sopenharmony_ci * and is a global variable, not multi-thread protected (use with care)
3062306a36Sopenharmony_ci */
3162306a36Sopenharmony_ci
3262306a36Sopenharmony_ci#ifndef DEBUG_H_12987983217
3362306a36Sopenharmony_ci#define DEBUG_H_12987983217
3462306a36Sopenharmony_ci
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_ci
3762306a36Sopenharmony_ci/* static assert is triggered at compile time, leaving no runtime artefact.
3862306a36Sopenharmony_ci * static assert only works with compile-time constants.
3962306a36Sopenharmony_ci * Also, this variant can only be used inside a function. */
4062306a36Sopenharmony_ci#define DEBUG_STATIC_ASSERT(c) (void)sizeof(char[(c) ? 1 : -1])
4162306a36Sopenharmony_ci
4262306a36Sopenharmony_ci
4362306a36Sopenharmony_ci/* DEBUGLEVEL is expected to be defined externally,
4462306a36Sopenharmony_ci * typically through compiler command line.
4562306a36Sopenharmony_ci * Value must be a number. */
4662306a36Sopenharmony_ci#ifndef DEBUGLEVEL
4762306a36Sopenharmony_ci#  define DEBUGLEVEL 0
4862306a36Sopenharmony_ci#endif
4962306a36Sopenharmony_ci
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_ci/* recommended values for DEBUGLEVEL :
5262306a36Sopenharmony_ci * 0 : release mode, no debug, all run-time checks disabled
5362306a36Sopenharmony_ci * 1 : enables assert() only, no display
5462306a36Sopenharmony_ci * 2 : reserved, for currently active debug path
5562306a36Sopenharmony_ci * 3 : events once per object lifetime (CCtx, CDict, etc.)
5662306a36Sopenharmony_ci * 4 : events once per frame
5762306a36Sopenharmony_ci * 5 : events once per block
5862306a36Sopenharmony_ci * 6 : events once per sequence (verbose)
5962306a36Sopenharmony_ci * 7+: events at every position (*very* verbose)
6062306a36Sopenharmony_ci *
6162306a36Sopenharmony_ci * It's generally inconvenient to output traces > 5.
6262306a36Sopenharmony_ci * In which case, it's possible to selectively trigger high verbosity levels
6362306a36Sopenharmony_ci * by modifying g_debug_level.
6462306a36Sopenharmony_ci */
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ci#if (DEBUGLEVEL>=1)
6762306a36Sopenharmony_ci#  define ZSTD_DEPS_NEED_ASSERT
6862306a36Sopenharmony_ci#  include "zstd_deps.h"
6962306a36Sopenharmony_ci#else
7062306a36Sopenharmony_ci#  ifndef assert   /* assert may be already defined, due to prior #include <assert.h> */
7162306a36Sopenharmony_ci#    define assert(condition) ((void)0)   /* disable assert (default) */
7262306a36Sopenharmony_ci#  endif
7362306a36Sopenharmony_ci#endif
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_ci#if (DEBUGLEVEL>=2)
7662306a36Sopenharmony_ci#  define ZSTD_DEPS_NEED_IO
7762306a36Sopenharmony_ci#  include "zstd_deps.h"
7862306a36Sopenharmony_ciextern int g_debuglevel; /* the variable is only declared,
7962306a36Sopenharmony_ci                            it actually lives in debug.c,
8062306a36Sopenharmony_ci                            and is shared by the whole process.
8162306a36Sopenharmony_ci                            It's not thread-safe.
8262306a36Sopenharmony_ci                            It's useful when enabling very verbose levels
8362306a36Sopenharmony_ci                            on selective conditions (such as position in src) */
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_ci#  define RAWLOG(l, ...) {                                       \
8662306a36Sopenharmony_ci                if (l<=g_debuglevel) {                           \
8762306a36Sopenharmony_ci                    ZSTD_DEBUG_PRINT(__VA_ARGS__);               \
8862306a36Sopenharmony_ci            }   }
8962306a36Sopenharmony_ci#  define DEBUGLOG(l, ...) {                                     \
9062306a36Sopenharmony_ci                if (l<=g_debuglevel) {                           \
9162306a36Sopenharmony_ci                    ZSTD_DEBUG_PRINT(__FILE__ ": " __VA_ARGS__); \
9262306a36Sopenharmony_ci                    ZSTD_DEBUG_PRINT(" \n");                     \
9362306a36Sopenharmony_ci            }   }
9462306a36Sopenharmony_ci#else
9562306a36Sopenharmony_ci#  define RAWLOG(l, ...)      {}    /* disabled */
9662306a36Sopenharmony_ci#  define DEBUGLOG(l, ...)    {}    /* disabled */
9762306a36Sopenharmony_ci#endif
9862306a36Sopenharmony_ci
9962306a36Sopenharmony_ci
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_ci#endif /* DEBUG_H_12987983217 */
102