log_level.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #pragma once
  2. #include <library/cpp/logger/priority.h>
  3. #include <util/generic/strbuf.h>
  4. #include <util/generic/yexception.h>
  5. namespace NYql {
  6. namespace NLog {
  7. enum class ELevel {
  8. FATAL = TLOG_EMERG,
  9. ERROR = TLOG_ERR,
  10. WARN = TLOG_WARNING,
  11. NOTICE = TLOG_NOTICE,
  12. INFO = TLOG_INFO,
  13. DEBUG = TLOG_DEBUG,
  14. TRACE = TLOG_RESOURCES,
  15. };
  16. struct ELevelHelpers {
  17. static constexpr bool Lte(ELevel l1, ELevel l2) {
  18. return ToInt(l1) <= ToInt(l2);
  19. }
  20. static constexpr ELogPriority ToLogPriority(ELevel level) {
  21. return static_cast<ELogPriority>(ToInt(level));
  22. }
  23. static ELevel FromLogPriority(ELogPriority priority) {
  24. return FromInt(static_cast<int>(priority));
  25. }
  26. static constexpr int ToInt(ELevel level) {
  27. return static_cast<int>(level);
  28. }
  29. static ELevel FromInt(int level) {
  30. switch (level) {
  31. case TLOG_EMERG:
  32. case TLOG_ALERT:
  33. case TLOG_CRIT: return ELevel::FATAL;
  34. case TLOG_ERR: return ELevel::ERROR;
  35. case TLOG_WARNING: return ELevel::WARN;
  36. case TLOG_NOTICE:
  37. case TLOG_INFO: return ELevel::INFO;
  38. case TLOG_DEBUG: return ELevel::DEBUG;
  39. case TLOG_RESOURCES: return ELevel::TRACE;
  40. default:
  41. return ELevel::INFO;
  42. }
  43. }
  44. static TStringBuf ToString(ELevel level) {
  45. // aligned 5-letters string
  46. switch (level) {
  47. case ELevel::FATAL: return TStringBuf("FATAL");
  48. case ELevel::ERROR: return TStringBuf("ERROR");
  49. case ELevel::WARN: return TStringBuf("WARN ");
  50. case ELevel::NOTICE:return TStringBuf("NOTE ");
  51. case ELevel::INFO: return TStringBuf("INFO ");
  52. case ELevel::DEBUG: return TStringBuf("DEBUG");
  53. case ELevel::TRACE: return TStringBuf("TRACE");
  54. }
  55. ythrow yexception() << "unknown log level: " << ToInt(level);
  56. }
  57. static ELevel FromString(TStringBuf str) {
  58. // aligned 5-letters string
  59. if (str == TStringBuf("FATAL")) return ELevel::FATAL;
  60. if (str == TStringBuf("ERROR")) return ELevel::ERROR;
  61. if (str == TStringBuf("WARN ")) return ELevel::WARN;
  62. if (str == TStringBuf("NOTE ")) return ELevel::NOTICE;
  63. if (str == TStringBuf("INFO ")) return ELevel::INFO;
  64. if (str == TStringBuf("DEBUG")) return ELevel::DEBUG;
  65. if (str == TStringBuf("TRACE")) return ELevel::TRACE;
  66. ythrow yexception() << "unknown log level: " << str;
  67. }
  68. template <typename TFunctor>
  69. static void ForEach(TFunctor&& f) {
  70. static const int minValue = ToInt(ELevel::FATAL);
  71. static const int maxValue = ToInt(ELevel::TRACE);
  72. for (int l = minValue; l <= maxValue; l++) {
  73. f(FromInt(l));
  74. }
  75. }
  76. };
  77. } // namspace NLog
  78. } // namspace NYql