QT (纯C++)项目 ‘Qxxx‘ file not found 和 编译报错问题(已解决)_qt头文件file not found-CSDN博客
qInstallMessageHandler(指针函数参数) 需要静态指针,这个函数
#include <iostream>
#include "singleton.h"
#include "log4qt.h"
#include<QDebug>
using namespace std;
SingleTon* const SingleTon::p = new SingleTon;
int main()
{
// SingleTon* p1 = SingleTon::getSingleTon();
// p1 -> printData("hello singleton");
// SingleTon* p2 = SingleTon::getSingleTon();
// p2->printData(" hi singleton");
//2024.07.10 log
qInstallMessageHandler(Log4qt::myMessageHandler);
qDebug()<<"This is a debug message.";
return 0;
}
#ifndef LOG4QT_H
#define LOG4QT_H
#include <QFile>
#include <QTextStream>
class Log4qt
{
public:
Log4qt();
static void myMessageHandler(QtMsgType type, const QMessageLogContext& context,
const QString& msg);
};
#endif // LOG4QT_H
#include "log4qt.h"
Log4qt::Log4qt()
{
}
void Log4qt::myMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
static QFile logFile("log.txt");
if(!logFile.isOpen()){
logFile.open(QIODevice::WriteOnly | QIODevice::Append);
}
QTextStream ts(&logFile);
switch(type){
case QtDebugMsg:
ts<<"Debug: ";
break;
case QtInfoMsg:
ts<<"Info: ";
break;
case QtWarningMsg:
ts<<"Warning: ";
break;
case QtCriticalMsg:
ts<<"Critical: ";
break;
case QtFatalMsg:
ts<<"Fatal: ";
abort();
}
ts<<context.file<<"("<<context.line<<"): "<<msg<<endl;
}