QT日志

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ClamReason/article/details/90039983

先实现一个日志函数


// 日志
std::fstream g_logFile;

void outputMessage(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
	static QMutex mutex;
	mutex.lock();

	QString currentDateTime = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss zzz ddd");
	QString infor = QString("[%1] Line:[%2] Func:[%3]").arg(QString(context.file)).arg(context.line).arg(context.function);

	QString message;
	switch (type)
	{
	case QtDebugMsg:
		message = QString("%1 %2 %3 %4").arg(currentDateTime).arg("Debug:").arg(msg).arg(infor);
		g_logFile << message.toStdString() << "\r\n";
		g_logFile.flush();
		break;

	case QtWarningMsg:
		message = QString("%1 %2 %3 %4").arg(currentDateTime).arg("Warning:").arg(msg).arg(infor);
		g_logFile << message.toStdString() << "\r\n";
		g_logFile.flush();
		break;

	case QtCriticalMsg:
		message = QString("%1 %2 %3 %4").arg(currentDateTime).arg("Critical:").arg(msg).arg(infor);
		g_logFile << message.toStdString() << "\r\n";
		g_logFile.flush();
		break;

	case QtFatalMsg:
		message = QString("%1 %2 %3 %4").arg(currentDateTime).arg("Fatal:").arg(msg).arg(infor);
		g_logFile << message.toStdString() << "\r\n";
		g_logFile.flush();
	}

	mutex.unlock();
}

日志初始化

	//日志初始化
	qInstallMessageHandler(outputMessage);
	QString logPath = Path::Instance()->GetTempDir() + "/Runnig.log";
	g_logFile.clear();
	g_logFile.open(logPath.toStdString(), ios::app | ios::out);
	if (!g_logFile)
	{
		QMessageBox::information(nullptr, QObject::tr("Warning!"), "can not create log file:" + logPath);
	}

 

展开阅读全文

没有更多推荐了,返回首页