这是一个跨平台解决方案,用于登录控制台,如果应用程序是从Qt Creator运行到QString("%1 %2: %3 (%4)").arg...文件,当它被编译并作为独立应用程序运行时。
main.cpp中:
#include
#include
#include
#include
#include
#include
#include
#include
const QString logFilePath = "debug.log";
bool logToFile = false;
void customMessageOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
QHash msgLevelHash({ {QtDebugMsg, "Debug"}, {QtInfoMsg, "Info"}, {QtWarningMsg, "Warning"}, {QtCriticalMsg, "Critical"}, {QtFatalMsg, "Fatal"}});
QByteArray localMsg = msg.toLocal8Bit();
QTime time = QTime::currentTime();
QString formattedTime = time.toString("hh:mm:ss.zzz");
QByteArray formattedTimeMsg = formatte