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