log_iface.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #pragma once
  2. #include "events.h"
  3. #include "event_local.h"
  4. namespace NActors {
  5. namespace NLog {
  6. using EComponent = int;
  7. enum EPriority : ui16 { // migrate it to EPrio whenever possible
  8. PRI_EMERG /* "EMERG" */,
  9. PRI_ALERT /* "ALERT" */,
  10. PRI_CRIT /* "CRIT" */,
  11. PRI_ERROR /* "ERROR" */,
  12. PRI_WARN /* "WARN" */,
  13. PRI_NOTICE /* "NOTICE" */,
  14. PRI_INFO /* "INFO" */,
  15. PRI_DEBUG /* "DEBUG" */,
  16. PRI_TRACE /* "TRACE" */
  17. };
  18. enum class EPrio : ui16 {
  19. Emerg = 0,
  20. Alert = 1,
  21. Crit = 2,
  22. Error = 3,
  23. Warn = 4,
  24. Notice = 5,
  25. Info = 6,
  26. Debug = 7,
  27. Trace = 8,
  28. };
  29. struct TLevel {
  30. TLevel(ui32 raw)
  31. : Raw(raw)
  32. {
  33. }
  34. TLevel(EPrio prio)
  35. : Raw((ui16(prio) + 1) << 8)
  36. {
  37. }
  38. EPrio ToPrio() const noexcept {
  39. const auto major = Raw >> 8;
  40. return major > 0 ? EPrio(major - 1) : EPrio::Emerg;
  41. }
  42. bool IsUrgentAbortion() const noexcept {
  43. return (Raw >> 8) == 0;
  44. }
  45. /* Generalized monotonic level value composed with major and minor
  46. levels. Minor is used for verbosity within major, basic EPrio
  47. mapped to (EPrio + 1, 0) and Major = 0 is reserved as special
  48. space with meaning like EPrio::Emerg but with extened actions.
  49. Thus logger should map Major = 0 to EPrio::Emerg if it have no
  50. idea how to handle special emergency actions.
  51. */
  52. ui32 Raw = 0; // ((ui16(EPrio) + 1) << 8) | ui8(minor)
  53. };
  54. enum class EEv {
  55. Log = EventSpaceBegin(TEvents::ES_LOGGER),
  56. LevelReq,
  57. LevelResp,
  58. Ignored,
  59. Buffer,
  60. End
  61. };
  62. static_assert(int(EEv::End) < EventSpaceEnd(TEvents::ES_LOGGER), "");
  63. struct TEvLogBufferMainListTag {};
  64. struct TEvLogBufferLevelListTag {};
  65. class TEvLog
  66. : public TEventLocal<TEvLog, int(EEv::Log)>
  67. , public TIntrusiveListItem<TEvLog, TEvLogBufferMainListTag>
  68. , public TIntrusiveListItem<TEvLog, TEvLogBufferLevelListTag>
  69. {
  70. public:
  71. TEvLog(TInstant stamp, TLevel level, EComponent comp, const TString &line)
  72. : Stamp(stamp)
  73. , Level(level)
  74. , Component(comp)
  75. , Line(line)
  76. {
  77. }
  78. TEvLog(TInstant stamp, TLevel level, EComponent comp, TString &&line)
  79. : Stamp(stamp)
  80. , Level(level)
  81. , Component(comp)
  82. , Line(std::move(line))
  83. {
  84. }
  85. TEvLog(EPriority prio, EComponent comp, TString line, TInstant time = TInstant::Now())
  86. : Stamp(time)
  87. , Level(EPrio(prio))
  88. , Component(comp)
  89. , Line(std::move(line))
  90. {
  91. }
  92. const TInstant Stamp = TInstant::Max();
  93. const TLevel Level;
  94. const EComponent Component = 0;
  95. TString Line;
  96. };
  97. }
  98. }