0

I have a QtQuick/QML application running on a remote embedded target system. I have syslog configured on the target to direct log messages to a log server.

Now, I'd like to have the standard out and err console output also redirected to the local syslog so I can get all of my application feedback in one place.

Is there a "best practices" way to do this? Or will I want/need to obtain all this output within my application and log it through "normal channels"?

Edit: I can do this with bash redirection per this question/answer, but I would still prefer to do it from within the application, if possible.

Edit: I suppose I should make more clear that I'm not asking about how to get the messages that go through the application's logging API to go to syslog. If there are errors in my QtQuick QML, the Qt runtime generates error and warning messages that get printed to stderr. It's those messages that I want to get redirected to the system logging facility.

alpartis
  • 1,086
  • 14
  • 29

1 Answers1

3

Mind that all Qt and QML log will be streamed through this channel.

#include <syslog.h>
#include <QtGlobal>

/// Consider https://linux.die.net/man/3/openlog

/// Qt Log Message handler
static void qtLogMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{

    QByteArray loc = msg.toUtf8();

    switch (type) {
    case QtDebugMsg:
        syslog (LOG_DEBUG, "%s", loc.constData());
        break;
    case QtInfoMsg:
        syslog (LOG_INFO, "%s", loc.constData());
        break;
    case QtWarningMsg:
        syslog (LOG_WARNING, "%s", loc.constData());
        break;
    case QtCriticalMsg:
        syslog (LOG_CRIT, "%s", loc.constData());
        break;
    case QtFatalMsg:
        syslog (LOG_ERR, "%s", loc.constData());
        break;
    }
}

int main(int argc, char* argv[])
{
   /// When starting the process
   openlog("MY_APP_LOG", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_USER);

   /// Install Qt Log Message Handler in main.cpp
   qInstallMessageHandler(qtLogMessageHandler);

   /// The Qt app GUI start can be
   QApplication app(argc, argv);
   app.exec();

   /// When quitting the process
   closelog();
}
Alexander V
  • 8,351
  • 4
  • 38
  • 47
  • I did not try to compile but just composed this solution by quoting from my projects. The proper semantics is of course depend on the app but likely all can be done in main.cpp for that Qt/QML object before `app.exec()` call. – Alexander V Nov 15 '21 at 17:54
  • I suppose I should make more clear that I'm not asking about how to get the messages that go through the application's logging API to go to syslog. If there are errors in my QtQuick QML, the Qt runtime generates error and warning messages that get printed to stderr. It's those messages that I want to get redirected to the system logging facility. I'll edit/refine my original post for more clarity. – alpartis Nov 16 '21 at 20:11
  • From Qt docs: Use console.log, console.debug, console.info, console.warn, or console.error to print debugging information to the console. We should expect ERROR level log so you could filter that out in qtLogMessageHandler() function as above to go through syslog. Consider Qt log level of QtCriticalMsg, QtFatalMsg, QtSystemMsg and see which one is raised due to log you try to redirect. This is an assumption but generally all Qt log is like that. – Alexander V Nov 16 '21 at 22:29