logger-inl.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. #ifndef LOGGER_INL_H_
  2. #error "Direct inclusion of this file is not allowed, include logger.h"
  3. // For the sake of sane code completion.
  4. #include "logger.h"
  5. #endif
  6. #undef LOGGER_INL_H_
  7. #include <library/cpp/yt/yson_string/convert.h>
  8. #include <library/cpp/yt/yson_string/string.h>
  9. #include <library/cpp/yt/misc/tls.h>
  10. namespace NYT::NLogging {
  11. ////////////////////////////////////////////////////////////////////////////////
  12. inline bool TLogger::IsAnchorUpToDate(const TLoggingAnchor& anchor) const
  13. {
  14. return
  15. !Category_ ||
  16. anchor.CurrentVersion == Category_->ActualVersion->load(std::memory_order::relaxed);
  17. }
  18. template <class... TArgs>
  19. void TLogger::AddTag(const char* format, TArgs&&... args)
  20. {
  21. AddRawTag(Format(TRuntimeFormat{format}, std::forward<TArgs>(args)...));
  22. }
  23. template <class TType>
  24. void TLogger::AddStructuredTag(TStringBuf key, TType value)
  25. {
  26. auto* state = GetMutableCoWState();
  27. state->StructuredTags.emplace_back(key, NYson::ConvertToYsonString(value));
  28. }
  29. template <class... TArgs>
  30. TLogger TLogger::WithTag(const char* format, TArgs&&... args) const
  31. {
  32. auto result = *this;
  33. result.AddTag(format, std::forward<TArgs>(args)...);
  34. return result;
  35. }
  36. template <class TType>
  37. TLogger TLogger::WithStructuredTag(TStringBuf key, TType value) const
  38. {
  39. auto result = *this;
  40. result.AddStructuredTag(key, value);
  41. return result;
  42. }
  43. Y_FORCE_INLINE ELogLevel TLogger::GetEffectiveLoggingLevel(ELogLevel level, const TLoggingAnchor& anchor)
  44. {
  45. // Check if anchor is suppressed.
  46. if (anchor.Suppressed.load(std::memory_order::relaxed)) {
  47. return ELogLevel::Minimum;
  48. }
  49. // Compute the actual level taking anchor override into account.
  50. return anchor.LevelOverride.load(std::memory_order::relaxed).value_or(level);
  51. }
  52. Y_FORCE_INLINE bool TLogger::IsLevelEnabled(ELogLevel level) const
  53. {
  54. // This is the first check which is intended to be inlined next to
  55. // logging invocation point. Check below is almost zero-cost due
  56. // to branch prediction (which requires inlining for proper work).
  57. if (level < MinLevel_) {
  58. return false;
  59. }
  60. // Next check is heavier and requires full log manager definition which
  61. // is undesirable in -inl.h header file. This is why we extract it
  62. // to a separate method which is implemented in cpp file.
  63. return IsLevelEnabledHeavy(level);
  64. }
  65. Y_FORCE_INLINE const TLogger& TLogger::operator()() const
  66. {
  67. return *this;
  68. }
  69. ////////////////////////////////////////////////////////////////////////////////
  70. namespace NDetail {
  71. struct TMessageStringBuilderContext
  72. {
  73. TSharedMutableRef Chunk;
  74. };
  75. struct TMessageBufferTag
  76. { };
  77. class TMessageStringBuilder
  78. : public TStringBuilderBase
  79. {
  80. public:
  81. TSharedRef Flush();
  82. // For testing only.
  83. static void DisablePerThreadCache();
  84. protected:
  85. void DoReset() override;
  86. void DoReserve(size_t newLength) override;
  87. private:
  88. TSharedMutableRef Buffer_;
  89. static constexpr size_t ChunkSize = 128_KB - 64;
  90. };
  91. inline bool HasMessageTags(
  92. const TLoggingContext& loggingContext,
  93. const TLogger& logger)
  94. {
  95. if (!logger.GetTag().empty()) {
  96. return true;
  97. }
  98. if (!loggingContext.TraceLoggingTag.empty()) {
  99. return true;
  100. }
  101. return false;
  102. }
  103. inline void AppendMessageTags(
  104. TStringBuilderBase* builder,
  105. const TLoggingContext& loggingContext,
  106. const TLogger& logger)
  107. {
  108. bool printComma = false;
  109. if (const auto& loggerTag = logger.GetTag(); !loggerTag.empty()) {
  110. builder->AppendString(loggerTag);
  111. printComma = true;
  112. }
  113. if (const auto& traceLoggingTag = loggingContext.TraceLoggingTag; !traceLoggingTag.empty()) {
  114. if (printComma) {
  115. builder->AppendString(TStringBuf(", "));
  116. }
  117. builder->AppendString(traceLoggingTag);
  118. }
  119. }
  120. inline void AppendLogMessage(
  121. TStringBuilderBase* builder,
  122. const TLoggingContext& loggingContext,
  123. const TLogger& logger,
  124. TRef message)
  125. {
  126. if (HasMessageTags(loggingContext, logger)) {
  127. if (message.Size() >= 1 && message[message.Size() - 1] == ')') {
  128. builder->AppendString(TStringBuf(message.Begin(), message.Size() - 1));
  129. builder->AppendString(TStringBuf(", "));
  130. } else {
  131. builder->AppendString(TStringBuf(message.Begin(), message.Size()));
  132. builder->AppendString(TStringBuf(" ("));
  133. }
  134. AppendMessageTags(builder, loggingContext, logger);
  135. builder->AppendChar(')');
  136. } else {
  137. builder->AppendString(TStringBuf(message.Begin(), message.Size()));
  138. }
  139. }
  140. template <class... TArgs>
  141. void AppendLogMessageWithFormat(
  142. TStringBuilderBase* builder,
  143. const TLoggingContext& loggingContext,
  144. const TLogger& logger,
  145. TStringBuf format,
  146. TArgs&&... args)
  147. {
  148. if (HasMessageTags(loggingContext, logger)) {
  149. if (format.size() >= 2 && format[format.size() - 1] == ')') {
  150. builder->AppendFormat(format.substr(0, format.size() - 1), std::forward<TArgs>(args)...);
  151. builder->AppendString(TStringBuf(", "));
  152. } else {
  153. builder->AppendFormat(format, std::forward<TArgs>(args)...);
  154. builder->AppendString(TStringBuf(" ("));
  155. }
  156. AppendMessageTags(builder, loggingContext, logger);
  157. builder->AppendChar(')');
  158. } else {
  159. builder->AppendFormat(format, std::forward<TArgs>(args)...);
  160. }
  161. }
  162. struct TLogMessage
  163. {
  164. TSharedRef MessageRef;
  165. TStringBuf Anchor;
  166. };
  167. template <class... TArgs>
  168. TLogMessage BuildLogMessage(
  169. const TLoggingContext& loggingContext,
  170. const TLogger& logger,
  171. TFormatString<TArgs...> format,
  172. TArgs&&... args)
  173. {
  174. TMessageStringBuilder builder;
  175. AppendLogMessageWithFormat(&builder, loggingContext, logger, format.Get(), std::forward<TArgs>(args)...);
  176. return {builder.Flush(), format.Get()};
  177. }
  178. template <CFormattable T>
  179. requires (!CStringLiteral<std::remove_cvref_t<T>>)
  180. TLogMessage BuildLogMessage(
  181. const TLoggingContext& loggingContext,
  182. const TLogger& logger,
  183. const T& obj)
  184. {
  185. TMessageStringBuilder builder;
  186. FormatValue(&builder, obj, TStringBuf("v"));
  187. if (HasMessageTags(loggingContext, logger)) {
  188. builder.AppendString(TStringBuf(" ("));
  189. AppendMessageTags(&builder, loggingContext, logger);
  190. builder.AppendChar(')');
  191. }
  192. if constexpr (std::same_as<TStringBuf, std::remove_cvref_t<T>>) {
  193. // NB(arkady-e1ppa): This is the overload where TStringBuf
  194. // falls as well as zero-argument format strings.
  195. // Formerly (before static analysis) there was a special overload
  196. // which guaranteed that Anchor is set to the value of said TStringBuf
  197. // object. Now we have overload for TFormatString<> which fordids
  198. // us having overload for TStringBuf (both have implicit ctors from
  199. // string literals) thus we have to accommodate TStringBuf specifics
  200. // in this if constexpr part.
  201. return {builder.Flush(), obj};
  202. } else {
  203. return {builder.Flush(), TStringBuf()};
  204. }
  205. }
  206. inline TLogMessage BuildLogMessage(
  207. const TLoggingContext& loggingContext,
  208. const TLogger& logger,
  209. TFormatString<> format)
  210. {
  211. return BuildLogMessage(
  212. loggingContext,
  213. logger,
  214. format.Get());
  215. }
  216. inline TLogMessage BuildLogMessage(
  217. const TLoggingContext& loggingContext,
  218. const TLogger& logger,
  219. TRuntimeFormat format)
  220. {
  221. return BuildLogMessage(
  222. loggingContext,
  223. logger,
  224. format.Get());
  225. }
  226. inline TLogMessage BuildLogMessage(
  227. const TLoggingContext& loggingContext,
  228. const TLogger& logger,
  229. TSharedRef&& message)
  230. {
  231. if (HasMessageTags(loggingContext, logger)) {
  232. TMessageStringBuilder builder;
  233. AppendLogMessage(&builder, loggingContext, logger, message);
  234. return {builder.Flush(), TStringBuf()};
  235. } else {
  236. return {std::move(message), TStringBuf()};
  237. }
  238. }
  239. inline TLogEvent CreateLogEvent(
  240. const TLoggingContext& loggingContext,
  241. const TLogger& logger,
  242. ELogLevel level)
  243. {
  244. TLogEvent event;
  245. event.Instant = loggingContext.Instant;
  246. event.Category = logger.GetCategory();
  247. event.Essential = logger.IsEssential();
  248. event.Level = level;
  249. event.ThreadId = loggingContext.ThreadId;
  250. event.ThreadName = loggingContext.ThreadName;
  251. event.FiberId = loggingContext.FiberId;
  252. event.TraceId = loggingContext.TraceId;
  253. event.RequestId = loggingContext.RequestId;
  254. return event;
  255. }
  256. void OnCriticalLogEvent(
  257. const TLogger& logger,
  258. const TLogEvent& event);
  259. inline void LogEventImpl(
  260. const TLoggingContext& loggingContext,
  261. const TLogger& logger,
  262. ELogLevel level,
  263. ::TSourceLocation sourceLocation,
  264. TLoggingAnchor* anchor,
  265. TSharedRef message)
  266. {
  267. auto event = CreateLogEvent(loggingContext, logger, level);
  268. event.MessageKind = ELogMessageKind::Unstructured;
  269. event.MessageRef = std::move(message);
  270. event.Family = ELogFamily::PlainText;
  271. event.SourceFile = sourceLocation.File;
  272. event.SourceLine = sourceLocation.Line;
  273. event.Anchor = anchor;
  274. if (Y_UNLIKELY(event.Level >= ELogLevel::Alert)) {
  275. OnCriticalLogEvent(logger, event);
  276. }
  277. logger.Write(std::move(event));
  278. }
  279. } // namespace NDetail
  280. ////////////////////////////////////////////////////////////////////////////////
  281. } // namespace NYT::NLogging