aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/logger/system.h
blob: b8c60b30235c5a5672af986e5cf160e88693f5a6 (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
#pragma once

#include "log.h"
#include "backend.h"
#include "priority.h"

#define YSYSLOG(priority, ...) SysLogInstance().AddLog((priority), __VA_ARGS__)
#define YSYSLOGINIT_FLAGS(ident, facility, flags)                                                                                    \
    struct TLogIniter {                                                                                                              \
        TLogIniter() {                                                                                                               \
            SysLogInstance().ResetBackend(THolder<TLogBackend>(                                                                      \
                (ident) ? (TLogBackend*)(new TSysLogBackend((ident), (facility), (flags))) : (TLogBackend*)(new TNullLogBackend())));\
        }                                                                                                                            \
    } Y_CAT(loginit, __LINE__);

#define YSYSLOGINIT(ident, facility) YSYSLOGINIT_FLAGS((ident), (facility), 0)

class TSysLogBackend: public TLogBackend {
public:
    enum EFacility {
        TSYSLOG_LOCAL0 = 0,
        TSYSLOG_LOCAL1 = 1,
        TSYSLOG_LOCAL2 = 2,
        TSYSLOG_LOCAL3 = 3,
        TSYSLOG_LOCAL4 = 4,
        TSYSLOG_LOCAL5 = 5,
        TSYSLOG_LOCAL6 = 6,
        TSYSLOG_LOCAL7 = 7
    };

    enum EFlags {
        LogPerror = 1,
        LogCons = 2
    };

    TSysLogBackend(const char* ident, EFacility facility, int flags = 0);
    ~TSysLogBackend() override;

    void WriteData(const TLogRecord& rec) override;
    void ReopenLog() override;

    virtual TString GetIdent() const {
        return Ident;
    }

    virtual EFacility GetFacility() const {
        return Facility;
    }

    virtual int GetFlags() const {
        return Flags;
    }

protected:
    int ELogPriority2SyslogPriority(ELogPriority priority);

    TString Ident;
    EFacility Facility;
    int Flags;
};

/*
 * return system-wide logger instance
 * better do not use in real programs(instead of robot, of course)
 */
TLog& SysLogInstance();