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