summaryrefslogtreecommitdiffstats
path: root/util/stream/debug.cpp
diff options
context:
space:
mode:
authorDevtools Arcadia <[email protected]>2022-02-07 18:08:42 +0300
committerDevtools Arcadia <[email protected]>2022-02-07 18:08:42 +0300
commit1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch)
treee26c9fed0de5d9873cce7e00bc214573dc2195b7 /util/stream/debug.cpp
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'util/stream/debug.cpp')
-rw-r--r--util/stream/debug.cpp50
1 files changed, 50 insertions, 0 deletions
diff --git a/util/stream/debug.cpp b/util/stream/debug.cpp
new file mode 100644
index 00000000000..afd5b3e1c73
--- /dev/null
+++ b/util/stream/debug.cpp
@@ -0,0 +1,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;
+ };
+}
+
+template <>
+struct TSingletonTraits<TDbgSelector> {
+ static constexpr size_t Priority = 8;
+};
+
+IOutputStream& StdDbgStream() noexcept {
+ return *(Singleton<TDbgSelector>()->Out);
+}
+
+int StdDbgLevel() noexcept {
+ return Singleton<TDbgSelector>()->Level;
+}