ssu
ssulog.cpp
Go to the documentation of this file.
1 
8 #include <QFile>
9 #include <QTextStream>
10 
11 #include "ssulog_p.h"
12 #include "ssucoreconfig_p.h"
13 
14 SsuLog *SsuLog::ssuLog = 0;
15 
16 SsuLog *SsuLog::instance()
17 {
18  if (!ssuLog) {
19  ssuLog = new SsuLog;
20  ssuLog->fallbackLogPath = "/tmp/ssu.log";
21  ssuLog->ssuLogLevel = -1;
22  }
23 
24  return ssuLog;
25 }
26 
27 void SsuLog::print(int priority, const QString &message)
28 {
29  // directly go through qsettings here to avoid recursive invocation
30  // of coreconfig / ssulog
31  if (ssuLogLevel == -1) {
32  QSettings settings(SSU_CONFIGURATION, QSettings::IniFormat);
33 
34  if (settings.contains("loglevel"))
35  ssuLog->ssuLogLevel = settings.value("loglevel").toInt();
36  else
37  ssuLog->ssuLogLevel = LOG_ERR;
38  }
39 
40  // this is rather dirty, but since systemd does not seem to allow to enable debug
41  // logging only for specific services probably best way for now
42  if (priority > ssuLogLevel)
43  return;
44 
45  QByteArray ba = message.toUtf8();
46  const char *ca = ba.constData();
47 
48  if (sd_journal_print(priority, "ssu: %s", ca) < 0 && !fallbackLogPath.isEmpty()) {
49  QFile logfile;
50  QTextStream logstream;
51  logfile.setFileName(fallbackLogPath);
52  logfile.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append);
53  logstream.setDevice(&logfile);
54  logstream << message << "\n";
55  logstream.flush();
56  }
57 }
SsuLog
Definition: ssulog_p.h:15
ssucoreconfig_p.h
SsuLog::print
void print(int priority, const QString &message)
Definition: ssulog.cpp:27
SSU_CONFIGURATION
#define SSU_CONFIGURATION
Path to the main ssu configuration file.
Definition: ssucoreconfig_p.h:17
ssulog_p.h