summaryrefslogtreecommitdiffstats
path: root/yql/essentials/utils/log/profile.cpp
blob: 4dd412b8f639cfd922ce075d595fe1cc897603c7 (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
#include "profile.h"
#include "log.h"

#include <util/stream/format.h>


#define YQL_PERF_LOG(level, file, line) YQL_LOG_IMPL( \
    ::NYql::NLog::YqlLogger(), ::NYql::NLog::EComponent::Perf, level, \
    ::NYql::NLog::TContextPreprocessor, file, line)


namespace NYql {
namespace NLog {

TProfilingScope::~TProfilingScope() {
    if (Name_ == nullptr) {
        return;
    }

    double elapsed = static_cast<double>(::MicroSeconds() - StartedAt_);
    TStringBuf unit("us");
    if (elapsed > 1000000) {
        elapsed /= 1000000;
        unit = TStringBuf("s");
    } else if (elapsed > 1000) {
        elapsed /= 1000;
        unit = TStringBuf("ms");
    }

    auto doLog = [&]() {
        YQL_PERF_LOG(Level_, File_, Line_)
                << TStringBuf("Execution of [") << Name_
                << TStringBuf("] took ") << Prec(elapsed, 3) << unit;
    };

    if (!LogCtxPath_.first.empty() || !LogCtxPath_.second.empty()) {
        YQL_LOG_CTX_ROOT_SESSION_SCOPE(LogCtxPath_);
        doLog();
    } else {
        doLog();
    }
}

} // namspace NLog
} // namspace NYql