aboutsummaryrefslogtreecommitdiffstats
path: root/util/stream/trace.h
blob: af5dc1039747d284a693bcc5f2131ac8d811bfd1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#pragma once

#include "debug.h"
#include <util/system/defaults.h>

/**
 * Debug level, as set via `DBGOUT` environment variable.
 */
enum ETraceLevel: ui8 {
    TRACE_ERR = 1,
    TRACE_WARN = 2,
    TRACE_NOTICE = 3,
    TRACE_INFO = 4,
    TRACE_DEBUG = 5,
    TRACE_DETAIL = 6,
    TRACE_VERBOSE = 7
};

#if !defined(NDEBUG) && !defined(Y_ENABLE_TRACE)
    #define Y_ENABLE_TRACE
#endif

/**
 * Writes the given data into standard debug stream if current debug level set
 * via `DBGOUT` environment variable permits it.
 *
 * Example usage:
 * @code
 * Y_DBGTRACE(DEBUG, "Advance from " << node1 << " to " << node2);
 * @endcode
 *
 * @param elevel                        Debug level of this trace command, e.g.
 *                                      `WARN` or `DEBUG`. Basically a suffix of
 *                                      one of the values of `ETraceLevel` enum.
 * @param args                          Argument chain to be written out into
 *                                      standard debug stream, joined with `<<`
 *                                      operator.
 * @see ETraceLevel
 */
#define Y_DBGTRACE(elevel, args) Y_DBGTRACE0(int(TRACE_##elevel), args)
#define Y_DBGTRACE0(level, args)                \
    do {                                        \
        if constexpr (Y_IS_DEBUG_BUILD) {       \
            if ((level) <= StdDbgLevel()) {     \
                StdDbgStream() << args << Endl; \
            }                                   \
        }                                       \
    } while (false)