Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.31 KB | None | 0 0
  1. #include <QCoreApplication>
  2. #include <QDateTime>
  3.  
  4. FILE *flogfile;
  5.  
  6. void msg_handler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
  7. {
  8.     // https://doc.qt.io/qt-5/qtglobal.html#qInstallMessageHandler
  9.  
  10.     const char *func = context.function ? context.function : "unknown function";
  11.     const char *file = context.file ? context.file : "";
  12.  
  13.     QString dt = QDateTime::currentDateTime().toString();
  14.     fprintf(flogfile, "[%s] ", qPrintable(dt));
  15.     switch (type) {
  16.     case QtDebugMsg:
  17.         fprintf(flogfile, "%s\n", qPrintable(msg));
  18.         break ;
  19.     case QtInfoMsg:
  20.         fprintf(flogfile, "%s\n", qPrintable(msg));
  21.         break ;
  22.     case QtWarningMsg:
  23.         fprintf(flogfile, "WARNING: %s\n", qPrintable(msg));
  24.         break ;
  25.     case QtCriticalMsg:
  26.         fprintf(flogfile, "CRITICAL: %s (%s)\n", qPrintable(msg), func);
  27.         break ;
  28.     case QtFatalMsg:
  29.         fprintf(flogfile, "FATAL: %s (%s:%u, %s)\n", qPrintable(msg), file, context.line, func);
  30.         break ;
  31.     }
  32. }
  33.  
  34. int main(int argc, char *argv[])
  35. {
  36.     QCoreApplication a(argc, argv);
  37.  
  38.     if ((flogfile = fopen("/home/user/llog", "a")) == nullptr) {
  39.             flogfile = stderr;
  40.             printf("can't open: %s", strerror(errno));
  41.         }
  42.     qInfo("suka");
  43.  
  44.     return a.exec();
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement