新增全局热键示例
This commit is contained in:
@@ -12,10 +12,10 @@
|
||||
#define QDATE qPrintable(QDate::currentDate().toString("yyyy-MM-dd"))
|
||||
|
||||
//日志重定向
|
||||
#if (QT_VERSION < QT_VERSION_CHECK(5,0,0))
|
||||
void Log(QtMsgType type, const char *msg)
|
||||
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
|
||||
void Log(QtMsgType type, const QMessageLogContext &context, const QString &msg)
|
||||
#else
|
||||
void Log(QtMsgType type, const QMessageLogContext &, const QString &msg)
|
||||
void Log(QtMsgType type, const char *msg)
|
||||
#endif
|
||||
{
|
||||
//加锁,防止多线程中qdebug太频繁导致崩溃
|
||||
@@ -42,6 +42,19 @@ void Log(QtMsgType type, const QMessageLogContext &, const QString &msg)
|
||||
break;
|
||||
}
|
||||
|
||||
//加上打印代码所在代码文件、行号、函数名
|
||||
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
|
||||
if (SaveLog::Instance()->getUseContext()) {
|
||||
int line = context.line;
|
||||
QString file = context.file;
|
||||
QString function = context.function;
|
||||
if (line > 0) {
|
||||
content = QString("行号: %1 文件: %2 函数: %3\n%4").arg(line).arg(file).arg(function).arg(content);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
//将内容传给函数进行处理
|
||||
SaveLog::Instance()->save(content);
|
||||
}
|
||||
|
||||
@@ -67,6 +80,8 @@ SaveLog::SaveLog(QObject *parent) : QObject(parent)
|
||||
|
||||
file = new QFile(this);
|
||||
toNet = false;
|
||||
useContext = true;
|
||||
|
||||
//默认取应用程序根目录
|
||||
path = qApp->applicationDirPath();
|
||||
//默认取应用程序可执行文件名称
|
||||
@@ -81,23 +96,28 @@ SaveLog::~SaveLog()
|
||||
file->close();
|
||||
}
|
||||
|
||||
bool SaveLog::getUseContext()
|
||||
{
|
||||
return this->useContext;
|
||||
}
|
||||
|
||||
//安装日志钩子,输出调试信息到文件,便于调试
|
||||
void SaveLog::start()
|
||||
{
|
||||
#if (QT_VERSION < QT_VERSION_CHECK(5,0,0))
|
||||
qInstallMsgHandler(Log);
|
||||
#else
|
||||
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
|
||||
qInstallMessageHandler(Log);
|
||||
#else
|
||||
qInstallMsgHandler(Log);
|
||||
#endif
|
||||
}
|
||||
|
||||
//卸载日志钩子
|
||||
void SaveLog::stop()
|
||||
{
|
||||
#if (QT_VERSION < QT_VERSION_CHECK(5,0,0))
|
||||
qInstallMsgHandler(0);
|
||||
#else
|
||||
#if (QT_VERSION >= QT_VERSION_CHECK(5,0,0))
|
||||
qInstallMessageHandler(0);
|
||||
#else
|
||||
qInstallMsgHandler(0);
|
||||
#endif
|
||||
}
|
||||
|
||||
@@ -135,6 +155,11 @@ void SaveLog::setToNet(bool toNet)
|
||||
this->toNet = toNet;
|
||||
}
|
||||
|
||||
void SaveLog::setUseContext(bool useContext)
|
||||
{
|
||||
this->useContext = useContext;
|
||||
}
|
||||
|
||||
void SaveLog::setPath(const QString &path)
|
||||
{
|
||||
this->path = path;
|
||||
|
||||
@@ -6,9 +6,10 @@
|
||||
* 1. 支持动态启动和停止。
|
||||
* 2. 支持日志存储的目录。
|
||||
* 3. 支持网络发出打印日志。
|
||||
* 4. 支持Qt4+Qt5+Qt6,开箱即用。
|
||||
* 5. 支持多线程。
|
||||
* 6. 使用做到最简单,start即可。
|
||||
* 4. 支持输出日志上下文信息比如所在代码文件、行号、函数名等。
|
||||
* 5. 支持Qt4+Qt5+Qt6,开箱即用。
|
||||
* 6. 自动加锁支持多线程。
|
||||
* 7. 使用做到最简单,start即可。
|
||||
*/
|
||||
|
||||
#include <QObject>
|
||||
@@ -37,6 +38,8 @@ private:
|
||||
QFile *file;
|
||||
//是否重定向到网络
|
||||
bool toNet;
|
||||
//是否输出日志上下文
|
||||
bool useContext;
|
||||
//日志文件路径
|
||||
QString path;
|
||||
//日志文件名称
|
||||
@@ -44,6 +47,9 @@ private:
|
||||
//日志文件完整名称
|
||||
QString fileName;
|
||||
|
||||
public:
|
||||
bool getUseContext();
|
||||
|
||||
Q_SIGNALS:
|
||||
//发送内容信号
|
||||
void send(const QString &content);
|
||||
@@ -58,6 +64,8 @@ public Q_SLOTS:
|
||||
|
||||
//设置是否重定向到网络
|
||||
void setToNet(bool toNet);
|
||||
//设置是否输出日志上下文
|
||||
void setUseContext(bool useContext);
|
||||
//设置日志文件存放路径
|
||||
void setPath(const QString &path);
|
||||
//设置日志文件名称
|
||||
|
||||
Reference in New Issue
Block a user