aboutsummaryrefslogtreecommitdiffstats
path: root/yt/cpp/mapreduce/interface/logging/logger.cpp
blob: bfa56b94f6d57c2cb3419ac2d696625ed6772072 (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include "logger.h"

#include <util/datetime/base.h>

#include <util/stream/file.h>
#include <util/stream/format.h>
#include <util/stream/printf.h>
#include <util/stream/str.h>

#include <util/system/mutex.h>
#include <util/system/rwlock.h>
#include <util/system/thread.h>

namespace NYT {

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

static TStringBuf StripFileName(TStringBuf path) {
    TStringBuf l, r;
    if (path.TryRSplit('/', l, r) || path.TryRSplit('\\', l, r)) {
        return r;
    } else {
        return path;
    }
}

static char GetLogLevelCode(ILogger::ELevel level) {
    switch (level) {
        case ILogger::FATAL: return 'F';
        case ILogger::ERROR: return 'E';
        case ILogger::INFO: return 'I';
        case ILogger::DEBUG: return 'D';
    }
    Y_UNREACHABLE();
}

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

class TNullLogger
    : public ILogger
{
public:
    void Log(ELevel level, const TSourceLocation& sourceLocation, const char* format, va_list args) override
    {
        Y_UNUSED(level);
        Y_UNUSED(sourceLocation);
        Y_UNUSED(format);
        Y_UNUSED(args);
    }
};

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

class TLoggerBase
    : public ILogger
{
public:
    TLoggerBase(ELevel cutLevel)
        : CutLevel_(cutLevel)
    { }

    virtual void OutputLine(const TString& line) = 0;

    void Log(ELevel level, const TSourceLocation& sourceLocation, const char* format, va_list args) override
    {
        if (level > CutLevel_) {
            return;
        }

        TStringStream stream;
        stream << TInstant::Now().ToStringLocal()
            << " " << GetLogLevelCode(level)
            << " [" << Hex(TThread::CurrentThreadId(), HF_FULL) << "] ";
        Printf(stream, format, args);
        stream << " - " << StripFileName(sourceLocation.File) << ':' << sourceLocation.Line << Endl;

        TGuard<TMutex> guard(Mutex_);
        OutputLine(stream.Str());
    }

private:
    ELevel CutLevel_;
    TMutex Mutex_;
};

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

class TStdErrLogger
    : public TLoggerBase
{
public:
    TStdErrLogger(ELevel cutLevel)
        : TLoggerBase(cutLevel)
    { }

    void OutputLine(const TString& line) override
    {
        Cerr << line;
    }
};

ILoggerPtr CreateStdErrLogger(ILogger::ELevel cutLevel)
{
    return new TStdErrLogger(cutLevel);
}

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

class TFileLogger
    : public TLoggerBase
{
public:
    TFileLogger(ELevel cutLevel, const TString& path, bool append)
        : TLoggerBase(cutLevel)
        , Stream_(TFile(path, OpenAlways | WrOnly | Seq | (append ? ForAppend : EOpenMode())))
    { }

    void OutputLine(const TString& line) override
    {
        Stream_ << line;
    }

private:
    TUnbufferedFileOutput Stream_;
};

ILoggerPtr CreateFileLogger(ILogger::ELevel cutLevel, const TString& path, bool append)
{
    return new TFileLogger(cutLevel, path, append);
}
////////////////////////////////////////////////////////////////////////////////

class TBufferedFileLogger
    : public TLoggerBase
{
public:
    TBufferedFileLogger(ELevel cutLevel, const TString& path, bool append)
        : TLoggerBase(cutLevel)
        , Stream_(TFile(path, OpenAlways | WrOnly | Seq | (append ? ForAppend : EOpenMode())))
    { }

    void OutputLine(const TString& line) override
    {
        Stream_ << line;
    }

private:
    TFileOutput Stream_;
};

ILoggerPtr CreateBufferedFileLogger(ILogger::ELevel cutLevel, const TString& path, bool append)
{
    return new TBufferedFileLogger(cutLevel, path, append);
}

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

static TRWMutex LoggerMutex;
static ILoggerPtr Logger;

struct TLoggerInitializer
{
    TLoggerInitializer()
    {
        Logger = new TNullLogger;
    }
} LoggerInitializer;

void SetLogger(ILoggerPtr logger)
{
    auto guard = TWriteGuard(LoggerMutex);
    if (logger) {
        Logger = logger;
    } else {
        Logger = new TNullLogger;
    }
}

ILoggerPtr GetLogger()
{
    auto guard = TReadGuard(LoggerMutex);
    return Logger;
}

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

}