aboutsummaryrefslogtreecommitdiff
path: root/src/debugspew.hpp
blob: 88bdbccad4bdb9b08b36ce2c77d80b8d3080c031 (plain)
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
49
50
51
52
53
54
55
56
57
58
59
60
#pragma once

#include "lanesconf.h"
#include "universe.hpp"

// #################################################################################################

#if USE_DEBUG_SPEW()

class DebugSpewIndentScope final
{
    private:
    Universe* const U{};

    public:
    static std::string_view const debugspew_indent;

    DebugSpewIndentScope(Universe* U_)
    : U{ U_ }
    {
        if (U) {
            U->debugspewIndentDepth.fetch_add(1, std::memory_order_relaxed);
        }
    }

    ~DebugSpewIndentScope()
    {
        if (U) {
            U->debugspewIndentDepth.fetch_sub(1, std::memory_order_relaxed);
        }
    }
};

// #################################################################################################

inline std::string_view DebugSpewIndent(Universe const* const U_)
{
    return DebugSpewIndentScope::debugspew_indent.substr(0, static_cast<size_t>(U_->debugspewIndentDepth.load(std::memory_order_relaxed)));
}

inline auto& DebugSpew(Universe const* const U_)
{
    if (!U_) {
        return std::cerr;
    }
    return std::cerr << DebugSpewIndent(U_) << " ";
}
#define DEBUGSPEW_CODE(_code) _code
#define DEBUGSPEW_OR_NOT(a_, b_) a_
#define DEBUGSPEW_PARAM_COMMA(param_) param_,
#define DEBUGSPEW_COMMA_PARAM(param_) , param_

#else // USE_DEBUG_SPEW()

#define DEBUGSPEW_CODE(_code)
#define DEBUGSPEW_OR_NOT(a_, b_) b_
#define DEBUGSPEW_PARAM_COMMA(param_)
#define DEBUGSPEW_COMMA_PARAM(param_)

#endif // USE_DEBUG_SPEW()