#include "backend.h" #include #include #include #include #include namespace NUnifiedAgent { namespace { class TDefaultRecordConverter : public IRecordConverter { public: TDefaultRecordConverter(bool stripTrailingNewLine) : StripTrailingNewLine(stripTrailingNewLine) , PriorityKey("_priority") { } TClientMessage Convert(const TLogRecord& rec) const override { const auto stripTrailingNewLine = StripTrailingNewLine && rec.Len > 0 && rec.Data[rec.Len - 1] == '\n'; THashMap metaFlags{{PriorityKey, NameOf(rec.Priority)}}; metaFlags.insert(rec.MetaFlags.begin(), rec.MetaFlags.end()); return { TString(rec.Data, stripTrailingNewLine ? rec.Len - 1 : rec.Len), std::move(metaFlags) }; } private: const bool StripTrailingNewLine; const TString PriorityKey; }; class TClientSessionAdapter: public TLogBackend { public: explicit TClientSessionAdapter(const TClientSessionPtr& session, THolder recordConverter) : Session(session) , RecordConverter(std::move(recordConverter)) { } void WriteData(const TLogRecord& rec) override { Session->Send(RecordConverter->Convert(rec)); } void ReopenLog() override { } private: TClientSessionPtr Session; THolder RecordConverter; }; class TSessionHolder { protected: TSessionHolder(const TClientParameters& parameters, const TSessionParameters& sessionParameters) : Client(MakeClient(parameters)) , Session(Client->CreateSession(sessionParameters)) { } protected: TClientPtr Client; TClientSessionPtr Session; }; class TAgentLogBackend: private TSessionHolder, public TClientSessionAdapter { public: TAgentLogBackend(const TClientParameters& parameters, const TSessionParameters& sessionParameters, THolder recordConverter) : TSessionHolder(parameters, sessionParameters) , TClientSessionAdapter(TSessionHolder::Session, std::move(recordConverter)) { } ~TAgentLogBackend() override { TSessionHolder::Session->Close(); } }; } THolder MakeDefaultRecordConverter(bool stripTrailingNewLine) { return MakeHolder(stripTrailingNewLine); } THolder AsLogBackend(const TClientSessionPtr& session, bool stripTrailingNewLine) { return MakeHolder(session, MakeDefaultRecordConverter(stripTrailingNewLine)); } THolder MakeLogBackend(const TClientParameters& parameters, const TSessionParameters& sessionParameters, THolder recordConverter) { if (!recordConverter) { recordConverter = MakeDefaultRecordConverter(); } return MakeHolder(parameters, sessionParameters, std::move(recordConverter)); } THolder<::TLog> MakeLog(const TClientParameters& parameters, const TSessionParameters& sessionParameters, THolder recordConverter) { return MakeHolder<::TLog>(MakeLogBackend(parameters, sessionParameters, std::move(recordConverter))); } }