/
ssulog.cpp
57 lines (47 loc) · 1.48 KB
/
ssulog.cpp
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
/**
* @file ssulog.cpp
* @copyright 2013 Jolla Ltd.
* @author Bernd Wachter <bwachter@lart.info>
* @date 2013
*/
#include <QFile>
#include <QTextStream>
#include "ssulog_p.h"
#include "ssucoreconfig_p.h"
SsuLog *SsuLog::ssuLog = 0;
SsuLog *SsuLog::instance()
{
if (!ssuLog) {
ssuLog = new SsuLog;
ssuLog->fallbackLogPath = "/tmp/ssu.log";
ssuLog->ssuLogLevel = -1;
}
return ssuLog;
}
void SsuLog::print(int priority, QString message)
{
QByteArray ba = message.toUtf8();
const char *ca = ba.constData();
// directly go through qsettings here to avoid recursive invocation
// of coreconfig / ssulog
if (ssuLogLevel == -1) {
QSettings settings(SSU_CONFIGURATION, QSettings::IniFormat);
if (settings.contains("loglevel"))
ssuLog->ssuLogLevel = settings.value("loglevel").toInt();
else
ssuLog->ssuLogLevel = LOG_ERR;
}
// this is rather dirty, but since systemd does not seem to allow to enable debug
// logging only for specific services probably best way for now
if (priority > ssuLogLevel)
return;
if (sd_journal_print(priority, "ssu: %s", ca) < 0 && fallbackLogPath != "") {
QFile logfile;
QTextStream logstream;
logfile.setFileName(fallbackLogPath);
logfile.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append);
logstream.setDevice(&logfile);
logstream << message << "\n";
logstream.flush();
}
}