debug.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #include "null.h"
  2. #include "debug.h"
  3. #include <util/string/cast.h>
  4. #include <util/generic/singleton.h>
  5. #include <util/generic/yexception.h>
  6. #include <cstdio>
  7. #include <cstdlib>
  8. void TDebugOutput::DoWrite(const void* buf, size_t len) {
  9. if (len != fwrite(buf, 1, len, stderr)) {
  10. ythrow yexception() << "write failed(" << LastSystemErrorText() << ")";
  11. }
  12. }
  13. namespace {
  14. struct TDbgSelector {
  15. inline TDbgSelector() {
  16. char* dbg = getenv("DBGOUT");
  17. if (dbg) {
  18. Out = &Cerr;
  19. try {
  20. Level = FromString(dbg);
  21. } catch (const yexception&) {
  22. Level = 0;
  23. }
  24. } else {
  25. Out = &Cnull;
  26. Level = 0;
  27. }
  28. }
  29. IOutputStream* Out;
  30. int Level;
  31. };
  32. } // namespace
  33. template <>
  34. struct TSingletonTraits<TDbgSelector> {
  35. static constexpr size_t Priority = 8;
  36. };
  37. IOutputStream& StdDbgStream() noexcept {
  38. return *(Singleton<TDbgSelector>()->Out);
  39. }
  40. int StdDbgLevel() noexcept {
  41. return Singleton<TDbgSelector>()->Level;
  42. }