log.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #pragma once
  2. #include "backend.h"
  3. #include "element.h"
  4. #include "priority.h"
  5. #include "record.h"
  6. #include "thread.h"
  7. #include <util/generic/fwd.h>
  8. #include <util/generic/ptr.h>
  9. #include <functional>
  10. #include <cstdarg>
  11. using TLogFormatter = std::function<TString(ELogPriority priority, TStringBuf)>;
  12. // Logging facilities interface.
  13. //
  14. // ```cpp
  15. // TLog base;
  16. // ...
  17. // auto log = base;
  18. // log.SetFormatter([reqId](ELogPriority p, TStringBuf msg) {
  19. // return TStringBuilder() << "reqid=" << reqId << "; " << msg;
  20. // });
  21. //
  22. // log.Write(TLOG_INFO, "begin");
  23. // HandleRequest(...);
  24. // log.Write(TLOG_INFO, "end");
  25. // ```
  26. //
  27. // Users are encouraged to copy `TLog` instance.
  28. class TLog {
  29. public:
  30. // Construct empty logger all writes will be spilled.
  31. TLog();
  32. // Construct file logger.
  33. TLog(const TString& fname, ELogPriority priority = LOG_MAX_PRIORITY);
  34. // Construct any type of logger
  35. TLog(THolder<TLogBackend> backend);
  36. TLog(const TLog&);
  37. TLog(TLog&&);
  38. ~TLog();
  39. TLog& operator=(const TLog&);
  40. TLog& operator=(TLog&&);
  41. // Change underlying backend.
  42. // NOTE: not thread safe.
  43. void ResetBackend(THolder<TLogBackend> backend) noexcept;
  44. // Reset underlying backend, `IsNullLog()` will return `true` after this call.
  45. // NOTE: not thread safe.
  46. THolder<TLogBackend> ReleaseBackend() noexcept;
  47. // Check if underlying backend is defined and is not null.
  48. // NOTE: not thread safe with respect to `ResetBackend` and `ReleaseBackend`.
  49. bool IsNullLog() const noexcept;
  50. bool IsNotNullLog() const noexcept {
  51. return !IsNullLog();
  52. }
  53. // Write message to the log.
  54. //
  55. // @param[in] priority Message priority to use.
  56. // @param[in] message Message to write.
  57. // @param[in] metaFlags Message meta flags.
  58. void Write(ELogPriority priority, TStringBuf message, TLogRecord::TMetaFlags metaFlags = {}) const;
  59. // Write message to the log using `DefaultPriority()`.
  60. void Write(const char* data, size_t len, TLogRecord::TMetaFlags metaFlags = {}) const;
  61. // Write message to the log, but pass the message in a c-style.
  62. void Write(ELogPriority priority, const char* data, size_t len, TLogRecord::TMetaFlags metaFlags = {}) const;
  63. // Write message to the log in a c-like printf style.
  64. void Y_PRINTF_FORMAT(3, 4) AddLog(ELogPriority priority, const char* format, ...) const;
  65. // Write message to the log in a c-like printf style with `DefaultPriority()` priority.
  66. void Y_PRINTF_FORMAT(2, 3) AddLog(const char* format, ...) const;
  67. // Call `ReopenLog()` of the underlying backend.
  68. void ReopenLog();
  69. // Call `ReopenLogNoFlush()` of the underlying backend.
  70. void ReopenLogNoFlush();
  71. // Call `QueueSize()` of the underlying backend.
  72. size_t BackEndQueueSize() const;
  73. // Set log default priority.
  74. // NOTE: not thread safe.
  75. void SetDefaultPriority(ELogPriority priority) noexcept;
  76. // Get default priority
  77. ELogPriority DefaultPriority() const noexcept;
  78. // Call `FiltrationLevel()` of the underlying backend.
  79. ELogPriority FiltrationLevel() const noexcept;
  80. // Set current log formatter.
  81. void SetFormatter(TLogFormatter formatter) noexcept;
  82. template <class T>
  83. inline TLogElement operator<<(const T& t) const {
  84. TLogElement ret(this);
  85. ret << t;
  86. return ret;
  87. }
  88. public:
  89. // These methods are deprecated and present here only for compatibility reasons (for 13 years
  90. // already ...). Do not use them.
  91. bool OpenLog(const char* path, ELogPriority lp = LOG_MAX_PRIORITY);
  92. bool IsOpen() const noexcept;
  93. void AddLogVAList(const char* format, va_list lst);
  94. void CloseLog();
  95. private:
  96. class TImpl;
  97. TIntrusivePtr<TImpl> Impl_;
  98. TLogFormatter Formatter_;
  99. };
  100. THolder<TLogBackend> CreateLogBackend(const TString& fname, ELogPriority priority = LOG_MAX_PRIORITY, bool threaded = false);
  101. THolder<TLogBackend> CreateFilteredOwningThreadedLogBackend(const TString& fname, ELogPriority priority = LOG_MAX_PRIORITY, size_t queueLen = 0);
  102. THolder<TOwningThreadedLogBackend> CreateOwningThreadedLogBackend(const TString& fname, size_t queueLen = 0);