element.h 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #pragma once
  2. #include "priority.h"
  3. #include "record.h"
  4. #include <util/string/cast.h>
  5. #include <util/stream/tempbuf.h>
  6. class TLog;
  7. /// @warning Better don't use directly.
  8. class TLogElement: public TTempBufOutput {
  9. public:
  10. explicit TLogElement(const TLog* parent);
  11. explicit TLogElement(const TLog* parent, ELogPriority priority);
  12. TLogElement(TLogElement&&) noexcept = default;
  13. TLogElement& operator=(TLogElement&&) noexcept = default;
  14. ~TLogElement() override;
  15. template <class T>
  16. inline TLogElement& operator<<(const T& t) {
  17. static_cast<IOutputStream&>(*this) << t;
  18. return *this;
  19. }
  20. /// @note For pretty usage: logger << TLOG_ERROR << "Error description";
  21. inline TLogElement& operator<<(ELogPriority priority) {
  22. Flush();
  23. Priority_ = priority;
  24. return *this;
  25. }
  26. template<typename T>
  27. TLogElement& With(const TStringBuf key, const T value) {
  28. Context_.emplace_back(key, ToString(value));
  29. return *this;
  30. }
  31. ELogPriority Priority() const noexcept {
  32. return Priority_;
  33. }
  34. protected:
  35. void DoFlush() override;
  36. protected:
  37. const TLog* Parent_ = nullptr;
  38. ELogPriority Priority_;
  39. TLogRecord::TMetaFlags Context_;
  40. };