summaryrefslogtreecommitdiff
path: root/src/syncevo/LogSyslog.cpp
blob: a24d77374a12642824ec897d4f55a7b2c78fa6a1 (plain)
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
/*
 * Copyright (C) 2012 Intel Corporation
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) version 3.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301  USA
 */

#include <syncevo/LogSyslog.h>
#include <string.h>
#include <errno.h>
#include <syslog.h>

#include <boost/bind.hpp>

#include <syncevo/declarations.h>
using namespace std;
SE_BEGIN_CXX

LoggerSyslog::LoggerSyslog(const std::string &processName) :
    m_processName(processName),
    m_parentLogger(Logger::instance())
{
    // valgrind tells us that openlog() does not copy the string.
    // Must provide pointer to a permanent copy.
    openlog(m_processName.c_str(), LOG_CONS | LOG_NDELAY | LOG_PID, LOG_USER);
    Logger::pushLogger(this);
}

LoggerSyslog::~LoggerSyslog()
{
    closelog();
    Logger::popLogger();
}

static void printToSyslog(int sysloglevel, std::string &chunk, size_t expectedTotal)
{
    if (!expectedTotal) {
        // Might contain line breaks in the middle, split it.
        size_t pos = 0;
        while(true) {
            size_t next = chunk.find('\n', pos);
            if (next == chunk.npos) {
                // Line break is guaranteed to be last character,
                // so we have printed everything now.
                return;
            }
            chunk[next] = 0;
            syslog(sysloglevel, "%s", chunk.c_str() + pos);
            pos = next + 1;
        }
    } else {
        // Single line. We can print the trailing line break.
        syslog(sysloglevel, "%s", chunk.c_str());
    }
}

void LoggerSyslog::messagev(const MessageOptions &options,
                            const char *format,
                            va_list args)
{
    // always to parent first (usually stdout):
    // if the parent is a LogRedirect instance, then
    // it'll flush its own output first, which ensures
    // that the new output comes later (as desired)
    {
        va_list argscopy;
        va_copy(argscopy, args);
        m_parentLogger.messagev(options, format, argscopy);
        va_end(argscopy);
    }

    if (options.m_level <= getLevel()) {
        const std::string none;
        formatLines(options.m_level, getLevel(),
                    &none, // Process name is set when opening the syslog, don't repeat it.
                    options.m_prefix,
                    format, args,
                    boost::bind(printToSyslog, getSyslogLevel(options.m_level), _1, _2));
    }
}

int LoggerSyslog::getSyslogLevel(Level level)
{
    switch (level) {
    case ERROR:
        return LOG_ERR;
    case WARNING:
        return LOG_WARNING;
    case SHOW:
        return LOG_NOTICE;
    case INFO:
    case DEV:
        return LOG_INFO;
    case DEBUG:
    default:
        return LOG_DEBUG;
    }
}

SE_END_CXX