aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/lwtrace/start.cpp
blob: ec971ce184d5a434c0e051fefe15a438f77c759b (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
#include "start.h" 

#include "all.h" 
 
#include <google/protobuf/text_format.h>

#include <util/generic/singleton.h>
#include <util/stream/file.h>
#include <util/stream/output.h> 
#include <util/system/env.h>

#include <stdlib.h> 

using namespace NLWTrace;

namespace {
    struct TTraceManagerHolder {
        TManager TraceManager;
        TTraceManagerHolder()
            : TraceManager(*Singleton<TProbeRegistry>(), true)
        {
        }
    };

    void TraceFromEnv(TString path) {
        TString script = TUnbufferedFileInput(path).ReadAll();
        TQuery query;
        bool ok = google::protobuf::TextFormat::ParseFromString(script, &query);
        Y_VERIFY(ok, "failed to parse protobuf");
        Singleton<TTraceManagerHolder>()->TraceManager.New("env", query);
    }

} // anonymous namespace

void NLWTrace::StartLwtraceFromEnv() {
    static bool started = false;
    if (started) {
        return;
    } else {
        started = true;
    }

    TString path = GetEnv("LWTRACE");
    if (!path) {
        return;
    }

    try {
        TraceFromEnv(path);
    } catch (...) {
        Cerr << "failed to load lwtrace script: " << CurrentExceptionMessage() << "\n";
        abort();
    }
}

void NLWTrace::StartLwtraceFromEnv(std::function<void(TManager&)> prepare) {
    TString path = GetEnv("LWTRACE");
    if (Y_LIKELY(!path)) {
        return;
    }

    try {
        prepare(Singleton<TTraceManagerHolder>()->TraceManager);
        TraceFromEnv(path);
    } catch (...) {
        Cerr << "failed to load lwtrace script: " << CurrentExceptionMessage() << "\n";
        abort();
    }
}