Skip to content

Instantly share code, notes, and snippets.

@jendas1
Created December 8, 2015 16:54
Show Gist options
  • Save jendas1/5e19242c312cd9240068 to your computer and use it in GitHub Desktop.
Save jendas1/5e19242c312cd9240068 to your computer and use it in GitHub Desktop.
Qt syslog logger
#include <QString>
#include <QDebug>
#include <QCoreApplication>
#include <QFileInfo>
#include <syslog.h>
void syslogMessageHandler(QtMsgType type, const QMessageLogContext & context, const QString & clean) {
#ifdef APP_NAME
const char * app_name = APP_NAME;
#else
const char * app_name = QFileInfo( QCoreApplication::applicationFilePath() ).fileName().toLatin1().data();
#endif
openlog(app_name, (LOG_CONS|LOG_PERROR|LOG_PID), LOG_DAEMON);
int log_flag;
switch (type) {
case QtDebugMsg:
log_flag = LOG_DEBUG;
break;
case QtInfoMsg:
log_flag = LOG_INFO;
break;
case QtWarningMsg:
log_flag = LOG_WARNING;
break;
case QtCriticalMsg:
log_flag = LOG_CRIT;
break;
case QtFatalMsg:
log_flag = LOG_ERR;
break;
}
if (!clean.isEmpty()) {
syslog(log_flag, "%s", qPrintable(clean));
}
closelog();
if (log_flag == LOG_ERR) {
abort();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment