aboutsummaryrefslogtreecommitdiffstats
path: root/util/stream/debug.cpp
blob: 1c08e38df97f453c69f3c3eebebed4d49392d52e (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
49
50
#include "null.h"
#include "debug.h"

#include <util/string/cast.h>
#include <util/generic/singleton.h>
#include <util/generic/yexception.h>

#include <cstdio>
#include <cstdlib>

void TDebugOutput::DoWrite(const void* buf, size_t len) {
    if (len != fwrite(buf, 1, len, stderr)) {
        ythrow yexception() << "write failed(" << LastSystemErrorText() << ")";
    }
}

namespace {
    struct TDbgSelector {
        inline TDbgSelector() {
            char* dbg = getenv("DBGOUT");
            if (dbg) {
                Out = &Cerr;
                try {
                    Level = FromString(dbg);
                } catch (const yexception&) {
                    Level = 0;
                }
            } else {
                Out = &Cnull;
                Level = 0;
            }
        }

        IOutputStream* Out;
        int Level;
    };
} // namespace

template <>
struct TSingletonTraits<TDbgSelector> {
    static constexpr size_t Priority = 8;
};

IOutputStream& StdDbgStream() noexcept {
    return *(Singleton<TDbgSelector>()->Out);
}

int StdDbgLevel() noexcept {
    return Singleton<TDbgSelector>()->Level;
}