aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/logger/system.cpp
blob: 0e9a647d281b2817dac1d265c3dd18ef982c312f (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
#include <util/stream/output.h>
#include <util/stream/null.h>
#include <util/system/compat.h>
#include <util/system/yassert.h>
#include <util/system/defaults.h>
#include <util/generic/singleton.h>
#include <util/generic/utility.h>

#if defined(_unix_)
#include <syslog.h>
#endif

#include "system.h"
#include "record.h"
#include "stream.h"

TSysLogBackend::TSysLogBackend(const char* ident, EFacility facility, int flags)
    : Ident(ident)
    , Facility(facility)
    , Flags(flags)
{
#if defined(_unix_)
    Y_ASSERT(TSYSLOG_LOCAL0 <= facility && facility <= TSYSLOG_LOCAL7);

    static const int f2sf[] = {
        LOG_LOCAL0,
        LOG_LOCAL1,
        LOG_LOCAL2,
        LOG_LOCAL3,
        LOG_LOCAL4,
        LOG_LOCAL5,
        LOG_LOCAL6,
        LOG_LOCAL7};

    int sysflags = LOG_NDELAY | LOG_PID;

    if (flags & LogPerror) {
        sysflags |= LOG_PERROR;
    }

    if (flags & LogCons) {
        sysflags |= LOG_CONS;
    }

    openlog(Ident.data(), sysflags, f2sf[(size_t)facility]);
#endif
}

TSysLogBackend::~TSysLogBackend() { 
#if defined(_unix_)
    closelog();
#endif
}

void TSysLogBackend::WriteData(const TLogRecord& rec) {
#if defined(_unix_)
    syslog(ELogPriority2SyslogPriority(rec.Priority), "%.*s", (int)rec.Len, rec.Data);
#else
    Y_UNUSED(rec);
#endif
}

void TSysLogBackend::ReopenLog() {
}

int TSysLogBackend::ELogPriority2SyslogPriority(ELogPriority priority) {
#if defined(_unix_)
     return Min(int(priority), (int)LOG_PRIMASK);
#else
    // trivial conversion
    return int(priority);
#endif
}

namespace {
    class TSysLogInstance: public TLog {
    public:
        inline TSysLogInstance()
            : TLog(MakeHolder<TStreamLogBackend>(&Cnull))
        {
        }
    };
}

TLog& SysLogInstance() {
    return *Singleton<TSysLogInstance>();
}