Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <QCoreApplication>
- #include <QDateTime>
- FILE *flogfile;
- void msg_handler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
- {
- // https://doc.qt.io/qt-5/qtglobal.html#qInstallMessageHandler
- const char *func = context.function ? context.function : "unknown function";
- const char *file = context.file ? context.file : "";
- QString dt = QDateTime::currentDateTime().toString();
- fprintf(flogfile, "[%s] ", qPrintable(dt));
- switch (type) {
- case QtDebugMsg:
- fprintf(flogfile, "%s\n", qPrintable(msg));
- break ;
- case QtInfoMsg:
- fprintf(flogfile, "%s\n", qPrintable(msg));
- break ;
- case QtWarningMsg:
- fprintf(flogfile, "WARNING: %s\n", qPrintable(msg));
- break ;
- case QtCriticalMsg:
- fprintf(flogfile, "CRITICAL: %s (%s)\n", qPrintable(msg), func);
- break ;
- case QtFatalMsg:
- fprintf(flogfile, "FATAL: %s (%s:%u, %s)\n", qPrintable(msg), file, context.line, func);
- break ;
- }
- }
- int main(int argc, char *argv[])
- {
- QCoreApplication a(argc, argv);
- if ((flogfile = fopen("/home/user/llog", "a")) == nullptr) {
- flogfile = stderr;
- printf("can't open: %s", strerror(errno));
- }
- qInfo("suka");
- return a.exec();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement