backend.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. #include "backend.h"
  2. #include <library/cpp/unified_agent_client/enum.h>
  3. #include <library/cpp/logger/record.h>
  4. #include <util/datetime/base.h>
  5. #include <util/generic/guid.h>
  6. #include <util/generic/serialized_enum.h>
  7. namespace NUnifiedAgent {
  8. namespace {
  9. class TDefaultRecordConverter : public IRecordConverter {
  10. public:
  11. TDefaultRecordConverter(bool stripTrailingNewLine)
  12. : StripTrailingNewLine(stripTrailingNewLine)
  13. , PriorityKey("_priority")
  14. {
  15. }
  16. TClientMessage Convert(const TLogRecord& rec) const override {
  17. const auto stripTrailingNewLine = StripTrailingNewLine &&
  18. rec.Len > 0 && rec.Data[rec.Len - 1] == '\n';
  19. THashMap<TString, TString> metaFlags{{PriorityKey, NameOf(rec.Priority)}};
  20. metaFlags.insert(rec.MetaFlags.begin(), rec.MetaFlags.end());
  21. return {
  22. TString(rec.Data, stripTrailingNewLine ? rec.Len - 1 : rec.Len),
  23. std::move(metaFlags)
  24. };
  25. }
  26. private:
  27. const bool StripTrailingNewLine;
  28. const TString PriorityKey;
  29. };
  30. class TClientSessionAdapter: public TLogBackend {
  31. public:
  32. explicit TClientSessionAdapter(const TClientSessionPtr& session, THolder<IRecordConverter> recordConverter)
  33. : Session(session)
  34. , RecordConverter(std::move(recordConverter))
  35. {
  36. }
  37. void WriteData(const TLogRecord& rec) override {
  38. Session->Send(RecordConverter->Convert(rec));
  39. }
  40. void ReopenLog() override {
  41. }
  42. private:
  43. TClientSessionPtr Session;
  44. THolder<IRecordConverter> RecordConverter;
  45. };
  46. class TSessionHolder {
  47. protected:
  48. TSessionHolder(const TClientParameters& parameters, const TSessionParameters& sessionParameters)
  49. : Client(MakeClient(parameters))
  50. , Session(Client->CreateSession(sessionParameters))
  51. {
  52. }
  53. protected:
  54. TClientPtr Client;
  55. TClientSessionPtr Session;
  56. };
  57. class TAgentLogBackend: private TSessionHolder, public TClientSessionAdapter {
  58. public:
  59. TAgentLogBackend(const TClientParameters& parameters,
  60. const TSessionParameters& sessionParameters,
  61. THolder<IRecordConverter> recordConverter)
  62. : TSessionHolder(parameters, sessionParameters)
  63. , TClientSessionAdapter(TSessionHolder::Session, std::move(recordConverter))
  64. {
  65. }
  66. ~TAgentLogBackend() override {
  67. TSessionHolder::Session->Close();
  68. }
  69. };
  70. }
  71. THolder<IRecordConverter> MakeDefaultRecordConverter(bool stripTrailingNewLine) {
  72. return MakeHolder<TDefaultRecordConverter>(stripTrailingNewLine);
  73. }
  74. THolder<TLogBackend> AsLogBackend(const TClientSessionPtr& session, bool stripTrailingNewLine) {
  75. return MakeHolder<TClientSessionAdapter>(session, MakeDefaultRecordConverter(stripTrailingNewLine));
  76. }
  77. THolder<TLogBackend> MakeLogBackend(const TClientParameters& parameters,
  78. const TSessionParameters& sessionParameters,
  79. THolder<IRecordConverter> recordConverter)
  80. {
  81. if (!recordConverter) {
  82. recordConverter = MakeDefaultRecordConverter();
  83. }
  84. return MakeHolder<TAgentLogBackend>(parameters, sessionParameters, std::move(recordConverter));
  85. }
  86. THolder<::TLog> MakeLog(const TClientParameters& parameters,
  87. const TSessionParameters& sessionParameters,
  88. THolder<IRecordConverter> recordConverter)
  89. {
  90. return MakeHolder<::TLog>(MakeLogBackend(parameters, sessionParameters, std::move(recordConverter)));
  91. }
  92. }