backend_creator.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #pragma once
  2. #include "backend.h"
  3. #include <library/cpp/object_factory/object_factory.h>
  4. #include <library/cpp/json/json_value.h>
  5. #include <util/generic/vector.h>
  6. #include <util/generic/ptr.h>
  7. #include <util/string/cast.h>
  8. class ILogBackendCreator {
  9. public:
  10. using TFactory = NObjectFactory::TObjectFactory<ILogBackendCreator, TString>;
  11. class IInitContext {
  12. public:
  13. template<class T>
  14. bool GetValue(TStringBuf name, T& var) const {
  15. TString tmp;
  16. if (!GetValue(name, tmp)) {
  17. return false;
  18. }
  19. var = FromString<T>(tmp);
  20. return true;
  21. }
  22. template<class T>
  23. T GetOrElse(TStringBuf name, const T& def) const {
  24. T res;
  25. return GetValue(name, res) ? res : def;
  26. }
  27. virtual ~IInitContext() = default;
  28. virtual bool GetValue(TStringBuf name, TString& var) const = 0;
  29. virtual TVector<THolder<IInitContext>> GetChildren(TStringBuf name) const = 0;
  30. };
  31. public:
  32. virtual ~ILogBackendCreator() = default;
  33. THolder<TLogBackend> CreateLogBackend() const;
  34. virtual bool Init(const IInitContext& ctx);
  35. NJson::TJsonValue AsJson() const;
  36. virtual void ToJson(NJson::TJsonValue& value) const = 0;
  37. static THolder<ILogBackendCreator> Create(const IInitContext& ctx);
  38. private:
  39. virtual THolder<TLogBackend> DoCreateLogBackend() const = 0;
  40. };
  41. class TLogBackendCreatorBase: public ILogBackendCreator {
  42. public:
  43. TLogBackendCreatorBase(const TString& type);
  44. virtual void ToJson(NJson::TJsonValue& value) const override final;
  45. protected:
  46. virtual void DoToJson(NJson::TJsonValue& value) const = 0;
  47. TString Type;
  48. };