aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/lwtrace/custom_action.h
blob: 92a3c66b84595c250f8402b19bf8d40f52ec0371 (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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#pragma once

#include "probe.h"

#include <library/cpp/lwtrace/protos/lwtrace.pb.h>

#include <util/generic/hash.h>

#include <functional>

namespace NLWTrace {
    class TSession;

    // Custom action can save any stuff (derived from IResource) in TSession object
    // IMPORTANT: Derived class will be used from multiple threads! (see example3)
    class IResource: public TAtomicRefCount<IResource> {
    public:
        virtual ~IResource() {
        }
    };
    using TResourcePtr = TIntrusivePtr<IResource>;

    // Trace resources that is used to hold/get/create any stuff
    class TTraceResources: public THashMap<TString, TResourcePtr> {
    public:
        template <class T>
        T& Get(const TString& name) {
            auto iter = find(name);
            if (iter == end()) {
                iter = insert(value_type(name, TResourcePtr(new T()))).first;
            }
            return *static_cast<T*>(iter->second.Get());
        }

        template <class T>
        const T* GetOrNull(const TString& name) const {
            auto iter = find(name);
            if (iter == end()) {
                return nullptr;
            }
            return *iter->second;
        }
    };

    // Base class of all custom actions
    class TCustomActionExecutor: public IExecutor {
    protected:
        TProbe* const Probe;
        bool Destructive;

    public:
        TCustomActionExecutor(TProbe* probe, bool destructive)
            : IExecutor()
            , Probe(probe)
            , Destructive(destructive)
        {
        }

        bool IsDestructive() {
            return Destructive;
        }
    };

    // Factory to produce custom action executors
    class TCustomActionFactory {
    public:
        using TCallback = std::function<TCustomActionExecutor*(TProbe* probe, const TCustomAction& action, TSession* trace)>;
        TCustomActionExecutor* Create(TProbe* probe, const TCustomAction& action, TSession* trace) const;
        void Register(const TString& name, const TCallback& callback);

    private:
        THashMap<TString, TCallback> Callbacks;
    };

}