aboutsummaryrefslogblamecommitdiffstats
path: root/util/stream/debug.cpp
blob: 1c08e38df97f453c69f3c3eebebed4d49392d52e (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12










                                                         
                                                                               














                                             

                          
 
                           
                  
              
 



                                         
                                        
                                             
 
                            
                                            
#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;
}