summaryrefslogtreecommitdiffstats
path: root/yql/essentials/utils/log/format.cpp
diff options
context:
space:
mode:
authorvvvv <[email protected]>2025-10-06 11:26:09 +0300
committervvvv <[email protected]>2025-10-06 11:53:26 +0300
commit60f45e69a4d7dbc6131208e16c45faf35aa5a985 (patch)
tree4daa45b52c295a178c7620e4c93921465fcf7950 /yql/essentials/utils/log/format.cpp
parent1bded1a65a7e6e9171418f3e1c691d390125b64e (diff)
YQL-20086 utils
init commit_hash:54feccd520ebd0ab23612bc0cb830914dff9d0e8
Diffstat (limited to 'yql/essentials/utils/log/format.cpp')
-rw-r--r--yql/essentials/utils/log/format.cpp258
1 files changed, 129 insertions, 129 deletions
diff --git a/yql/essentials/utils/log/format.cpp b/yql/essentials/utils/log/format.cpp
index 6292235d52d..6cd57fe3fda 100644
--- a/yql/essentials/utils/log/format.cpp
+++ b/yql/essentials/utils/log/format.cpp
@@ -15,160 +15,160 @@
namespace NYql::NLog {
- namespace {
+namespace {
- constexpr size_t MaxRequiredContextKey = static_cast<size_t>(EContextKey::Line);
+constexpr size_t MaxRequiredContextKey = static_cast<size_t>(EContextKey::Line);
- auto RequiredContextAccessor(const TLogRecord& rec) {
- return [&](EContextKey key) -> TStringBuf {
- return rec.MetaFlags.at(static_cast<size_t>(key)).second;
- };
- }
-
- auto OptionalContextAccessor(const TLogRecord& rec) {
- return [&](TStringBuf key) -> TMaybe<TStringBuf> {
- const auto isContextKeyPath = [&](const auto& pair) {
- return pair.first == key;
- };
+auto RequiredContextAccessor(const TLogRecord& rec) {
+ return [&](EContextKey key) -> TStringBuf {
+ return rec.MetaFlags.at(static_cast<size_t>(key)).second;
+ };
+}
- const auto* path = FindIfPtr(
- rec.MetaFlags.begin() + MaxRequiredContextKey + 1,
- rec.MetaFlags.end(),
- isContextKeyPath);
+auto OptionalContextAccessor(const TLogRecord& rec) {
+ return [&](TStringBuf key) -> TMaybe<TStringBuf> {
+ const auto isContextKeyPath = [&](const auto& pair) {
+ return pair.first == key;
+ };
- if (!path) {
- return Nothing();
- }
+ const auto* path = FindIfPtr(
+ rec.MetaFlags.begin() + MaxRequiredContextKey + 1,
+ rec.MetaFlags.end(),
+ isContextKeyPath);
- return path->second;
- };
+ if (!path) {
+ return Nothing();
}
- void PrintBody(TStringBuilder& out, const TLogRecord& rec, size_t flagBegin) {
- out << TStringBuf(rec.Data, rec.Len);
+ return path->second;
+ };
+}
- if (flagBegin < rec.MetaFlags.size()) {
- out << ". Extra context: ";
- }
+void PrintBody(TStringBuilder& out, const TLogRecord& rec, size_t flagBegin) {
+ out << TStringBuf(rec.Data, rec.Len);
- for (size_t i = flagBegin; i < rec.MetaFlags.size(); ++i) {
- const auto& [key, value] = rec.MetaFlags[i];
- out << key << " = " << value;
- if (i + 1 != rec.MetaFlags.size()) {
- out << ", ";
- }
- }
- }
+ if (flagBegin < rec.MetaFlags.size()) {
+ out << ". Extra context: ";
+ }
- TString FallbackFormat(const TLogRecord& rec) {
- TStringBuilder out;
- PrintBody(out, rec, /*flagBegin=*/0);
- return out;
+ for (size_t i = flagBegin; i < rec.MetaFlags.size(); ++i) {
+ const auto& [key, value] = rec.MetaFlags[i];
+ out << key << " = " << value;
+ if (i + 1 != rec.MetaFlags.size()) {
+ out << ", ";
}
+ }
+}
+
+TString FallbackFormat(const TLogRecord& rec) {
+ TStringBuilder out;
+ PrintBody(out, rec, /*flagBegin=*/0);
+ return out;
+}
+
+class TFormattingLogBackend final: public TForwardingLogBackend {
+public:
+ explicit TFormattingLogBackend(TFormatter formatter, bool isStrict, TAutoPtr<TLogBackend> child)
+ : TForwardingLogBackend(std::move(child))
+ , Formatter_(std::move(formatter))
+ , IsStrict_(isStrict)
+ {
+ }
- class TFormattingLogBackend final: public TForwardingLogBackend {
- public:
- explicit TFormattingLogBackend(TFormatter formatter, bool isStrict, TAutoPtr<TLogBackend> child)
- : TForwardingLogBackend(std::move(child))
- , Formatter_(std::move(formatter))
- , IsStrict_(isStrict)
- {
- }
-
- void WriteData(const TLogRecord& rec) final {
- if (rec.MetaFlags.empty()) {
- // NB. For signal handler.
- return TForwardingLogBackend::WriteData(rec);
- }
-
- TString message;
- if (IsSupported(rec.MetaFlags)) {
- message = Formatter_(rec);
- } else if (IsStrict_) {
- TStringBuilder message;
- message << "LogRecord is not supported: ";
- PrintBody(message, rec, /* flagBegin = */ 0);
- ythrow yexception() << std::move(message);
- } else {
- message = FallbackFormat(rec);
- }
- message.append('\n');
-
- const TLogRecord formatted(rec.Priority, message.data(), message.size());
- return TForwardingLogBackend::WriteData(formatted);
- }
+ void WriteData(const TLogRecord& rec) final {
+ if (rec.MetaFlags.empty()) {
+ // NB. For signal handler.
+ return TForwardingLogBackend::WriteData(rec);
+ }
- protected:
- static bool IsSupported(const TLogRecord::TMetaFlags& flags) {
- const auto isSupported = [&](size_t i) -> bool {
- const EContextKey key = static_cast<EContextKey>(i);
+ TString message;
+ if (IsSupported(rec.MetaFlags)) {
+ message = Formatter_(rec);
+ } else if (IsStrict_) {
+ TStringBuilder message;
+ message << "LogRecord is not supported: ";
+ PrintBody(message, rec, /* flagBegin = */ 0);
+ ythrow yexception() << std::move(message);
+ } else {
+ message = FallbackFormat(rec);
+ }
+ message.append('\n');
- const TStringBuf expected = ToStringBuf(key);
- if (flags.size() <= i) {
- return false;
- }
+ const TLogRecord formatted(rec.Priority, message.data(), message.size());
+ return TForwardingLogBackend::WriteData(formatted);
+ }
- const TStringBuf actual = flags[i].first;
- if (actual != expected) {
- return false;
- }
+protected:
+ static bool IsSupported(const TLogRecord::TMetaFlags& flags) {
+ const auto isSupported = [&](size_t i) -> bool {
+ const EContextKey key = static_cast<EContextKey>(i);
- return true;
- };
+ const TStringBuf expected = ToStringBuf(key);
+ if (flags.size() <= i) {
+ return false;
+ }
- return AllOf(std::views::iota(Min<size_t>(), MaxRequiredContextKey), isSupported);
+ const TStringBuf actual = flags[i].first;
+ if (actual != expected) {
+ return false;
}
- private:
- TFormatter Formatter_;
- bool IsStrict_;
+ return true;
};
- } // namespace
-
- TString LegacyFormat(const TLogRecord& rec) {
- const auto get = RequiredContextAccessor(rec);
- const auto opt = OptionalContextAccessor(rec);
-
- TStringBuilder out;
- out << get(EContextKey::DateTime) << ' '
- << get(EContextKey::Level) << ' '
- << get(EContextKey::ProcessName)
- << "(pid=" << get(EContextKey::ProcessID)
- << ", tid=" << get(EContextKey::ThreadID)
- << ") [" << get(EContextKey::Component) << "] "
- << get(EContextKey::FileName)
- << ':' << get(EContextKey::Line) << ": ";
-
- size_t unknownContextBegin = MaxRequiredContextKey + 1;
- if (auto path = opt(ToStringBuf(EContextKey::Path))) {
- out << "{" << *path << "} ";
- unknownContextBegin += 1;
- }
-
- PrintBody(out, rec, unknownContextBegin);
- return out;
+ return AllOf(std::views::iota(Min<size_t>(), MaxRequiredContextKey), isSupported);
}
- TString JsonFormat(const TLogRecord& rec) {
- TStringStream out;
- NJsonWriter::TBuf buf(NJsonWriter::HEM_DONT_ESCAPE_HTML, &out);
- buf.BeginObject();
- buf.WriteKey("message");
- buf.WriteString(TStringBuf(rec.Data, rec.Len));
- buf.WriteKey("@fields");
- buf.BeginObject();
- for (const auto& [key, value] : rec.MetaFlags) {
- buf.WriteKey(key);
- buf.WriteString(value);
- }
- buf.EndObject();
- buf.EndObject();
- return std::move(out.Str());
+private:
+ TFormatter Formatter_;
+ bool IsStrict_;
+};
+
+} // namespace
+
+TString LegacyFormat(const TLogRecord& rec) {
+ const auto get = RequiredContextAccessor(rec);
+ const auto opt = OptionalContextAccessor(rec);
+
+ TStringBuilder out;
+ out << get(EContextKey::DateTime) << ' '
+ << get(EContextKey::Level) << ' '
+ << get(EContextKey::ProcessName)
+ << "(pid=" << get(EContextKey::ProcessID)
+ << ", tid=" << get(EContextKey::ThreadID)
+ << ") [" << get(EContextKey::Component) << "] "
+ << get(EContextKey::FileName)
+ << ':' << get(EContextKey::Line) << ": ";
+
+ size_t unknownContextBegin = MaxRequiredContextKey + 1;
+ if (auto path = opt(ToStringBuf(EContextKey::Path))) {
+ out << "{" << *path << "} ";
+ unknownContextBegin += 1;
}
- TAutoPtr<TLogBackend> MakeFormattingLogBackend(TFormatter formatter, bool isStrict, TAutoPtr<TLogBackend> child) {
- return new TFormattingLogBackend(std::move(formatter), isStrict, std::move(child));
+ PrintBody(out, rec, unknownContextBegin);
+ return out;
+}
+
+TString JsonFormat(const TLogRecord& rec) {
+ TStringStream out;
+ NJsonWriter::TBuf buf(NJsonWriter::HEM_DONT_ESCAPE_HTML, &out);
+ buf.BeginObject();
+ buf.WriteKey("message");
+ buf.WriteString(TStringBuf(rec.Data, rec.Len));
+ buf.WriteKey("@fields");
+ buf.BeginObject();
+ for (const auto& [key, value] : rec.MetaFlags) {
+ buf.WriteKey(key);
+ buf.WriteString(value);
}
+ buf.EndObject();
+ buf.EndObject();
+ return std::move(out.Str());
+}
+
+TAutoPtr<TLogBackend> MakeFormattingLogBackend(TFormatter formatter, bool isStrict, TAutoPtr<TLogBackend> child) {
+ return new TFormattingLogBackend(std::move(formatter), isStrict, std::move(child));
+}
} // namespace NYql::NLog