aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/logging/backends/arcadia/backend.cpp
blob: 3c6ff9f5f585581c48c20c586be340f9dc1526cf (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
76
77
78
79
80
81
82
83
84
85
86
#include "backend.h"

#include <library/cpp/logger/backend.h>
#include <library/cpp/logger/record.h>

#include <library/cpp/yt/assert/assert.h>

#include <library/cpp/yt/logging/logger.h>

namespace NYT::NLogging {
namespace {

////////////////////////////////////////////////////////////////////////////////

ELogLevel ConvertToLogLevel(ELogPriority priority)
{
    switch (priority) {
        case ELogPriority::TLOG_DEBUG:
            return ELogLevel::Debug;
        case ELogPriority::TLOG_INFO:
            [[fallthrough]];
        case ELogPriority::TLOG_NOTICE:
            return ELogLevel::Info;
        case ELogPriority::TLOG_WARNING:
            return ELogLevel::Warning;
        case ELogPriority::TLOG_ERR:
            return ELogLevel::Error;
        case ELogPriority::TLOG_CRIT:
        case ELogPriority::TLOG_ALERT:
            return ELogLevel::Alert;
        case ELogPriority::TLOG_EMERG:
            return ELogLevel::Fatal;
        case ELogPriority::TLOG_RESOURCES:
            return ELogLevel::Maximum;
    }
    YT_ABORT();
}

class TLogBackendBridge
    : public TLogBackend
{
public:
    TLogBackendBridge(const TLogger& logger)
        : Logger_(logger)
    { }

    void WriteData(const TLogRecord& rec) override
    {
        const auto logLevel = ConvertToLogLevel(rec.Priority);
        if (!Logger_.IsLevelEnabled(logLevel)) {
            return;
        }

        // Remove trailing \n, because it will add it.
        TStringBuf message(rec.Data, rec.Len);
        message.ChopSuffix(TStringBuf("\n"));
        // Use low-level api, because it is more convinient here.
        auto loggingContext = GetLoggingContext();
        auto event = NDetail::CreateLogEvent(loggingContext, Logger_, logLevel);
        event.MessageRef = NDetail::BuildLogMessage(loggingContext, Logger_, message).MessageRef;
        event.Family = ELogFamily::PlainText;
        Logger_.Write(std::move(event));
    }

    void ReopenLog() override
    { }

    ELogPriority FiltrationLevel() const override
    {
        return LOG_MAX_PRIORITY;
    }

private:
    const TLogger Logger_;
};

} // namespace

THolder<TLogBackend> CreateArcadiaLogBackend(const TLogger& logger)
{
    return MakeHolder<TLogBackendBridge>(logger);
}

////////////////////////////////////////////////////////////////////////////////

} // namespace NYT::NLogging