blob: ad6e60d609e54688806bf35fd382adb9af0f1c1c [file] [log] [blame]
Brandon Maierdbe88da2023-01-12 10:27:45 -06001/* ******************************************************************
2 * debug
3 * Part of FSE library
4 * Copyright (c) Yann Collet, Facebook, Inc.
5 *
6 * You can contact the author at :
7 * - Source repository : https://github.com/Cyan4973/FiniteStateEntropy
8 *
9 * This source code is licensed under both the BSD-style license (found in the
10 * LICENSE file in the root directory of this source tree) and the GPLv2 (found
11 * 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****************************************************************** */
14
Brandon Maierdbe88da2023-01-12 10:27:45 -060015/*
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 */
30
31#ifndef DEBUG_H_12987983217
32#define DEBUG_H_12987983217
33
Brandon Maierdbe88da2023-01-12 10:27:45 -060034/* static assert is triggered at compile time, leaving no runtime artefact.
35 * static assert only works with compile-time constants.
36 * Also, this variant can only be used inside a function. */
37#define DEBUG_STATIC_ASSERT(c) (void)sizeof(char[(c) ? 1 : -1])
38
Brandon Maierdbe88da2023-01-12 10:27:45 -060039/* DEBUGLEVEL is expected to be defined externally,
40 * typically through compiler command line.
41 * Value must be a number. */
42#ifndef DEBUGLEVEL
43# define DEBUGLEVEL 0
44#endif
45
Brandon Maierdbe88da2023-01-12 10:27:45 -060046/* recommended values for DEBUGLEVEL :
47 * 0 : release mode, no debug, all run-time checks disabled
48 * 1 : enables assert() only, no display
49 * 2 : reserved, for currently active debug path
50 * 3 : events once per object lifetime (CCtx, CDict, etc.)
51 * 4 : events once per frame
52 * 5 : events once per block
53 * 6 : events once per sequence (verbose)
54 * 7+: events at every position (*very* verbose)
55 *
56 * It's generally inconvenient to output traces > 5.
57 * In which case, it's possible to selectively trigger high verbosity levels
58 * by modifying g_debug_level.
59 */
60
61#if (DEBUGLEVEL>=2)
62# define ZSTD_DEPS_NEED_IO
63# include "zstd_deps.h"
64extern int g_debuglevel; /* the variable is only declared,
65 it actually lives in debug.c,
66 and is shared by the whole process.
67 It's not thread-safe.
68 It's useful when enabling very verbose levels
69 on selective conditions (such as position in src) */
70
71# define RAWLOG(l, ...) { \
72 if (l<=g_debuglevel) { \
73 ZSTD_DEBUG_PRINT(__VA_ARGS__); \
74 } }
75# define DEBUGLOG(l, ...) { \
76 if (l<=g_debuglevel) { \
77 ZSTD_DEBUG_PRINT(__FILE__ ": " __VA_ARGS__); \
78 ZSTD_DEBUG_PRINT(" \n"); \
79 } }
80#else
81# define RAWLOG(l, ...) {} /* disabled */
82# define DEBUGLOG(l, ...) {} /* disabled */
83#endif
84
Brandon Maierdbe88da2023-01-12 10:27:45 -060085#endif /* DEBUG_H_12987983217 */