2023-12-02 11:27:25 +08:00
|
|
|
|
|
|
|
#include <QFile>
|
|
|
|
#include <QMessageLogger>
|
|
|
|
#include <qlogging.h>
|
|
|
|
#include <QFile>
|
|
|
|
#include <QTextStream>
|
|
|
|
#include <QDateTime>
|
|
|
|
#include <QString>
|
|
|
|
#include "base/mycfg.h"
|
|
|
|
#include <QUdpSocket>
|
|
|
|
#include <QDir>
|
|
|
|
#include "QApplication"
|
|
|
|
#include "QMutex"
|
|
|
|
|
|
|
|
#define FILE_PATH "/home/root/log/"
|
|
|
|
#define FILE_NAME "_log.txt"
|
|
|
|
|
|
|
|
char *msgHead[] =
|
|
|
|
{
|
|
|
|
(char *)"Debug ",
|
|
|
|
(char *)"Warning ",
|
|
|
|
(char *)"Critical",
|
|
|
|
(char *)"Fatal ",
|
|
|
|
(char *)"Info "};
|
|
|
|
|
|
|
|
struct mydebug
|
|
|
|
{
|
|
|
|
QFile *file;
|
|
|
|
QUdpSocket *socket_;
|
|
|
|
QByteArray send_data;
|
|
|
|
QHostAddress host_addr;
|
|
|
|
quint16 port;
|
|
|
|
bool active;
|
|
|
|
void (*send_data_fun)(QByteArray data);
|
|
|
|
QMutex mutex;
|
|
|
|
// QList<QString> type_filter;
|
|
|
|
// QList<QString> file_filter;
|
|
|
|
// QList<filter_line> line_filter;
|
|
|
|
};
|
|
|
|
|
|
|
|
static mydebug g_debug;
|
|
|
|
|
|
|
|
|
|
|
|
void print_to_console(QtMsgType type, const QMessageLogContext &context, const QString &msg);
|
|
|
|
void print_to_file(QtMsgType type, const QMessageLogContext &context, const QString &msg);
|
|
|
|
|
|
|
|
|
|
|
|
// 创建日志文件,名称用于创建日志文件
|
|
|
|
void mydebug_init(QString name)
|
|
|
|
{
|
|
|
|
qSetMessagePattern("[%{type}] %{time yyyy-MM-dd hh:mm:ss.zzz} %{function}:%{line} %{message}");
|
|
|
|
QDir tempDir;
|
|
|
|
if (!tempDir.exists(FILE_PATH))
|
|
|
|
{
|
|
|
|
tempDir.mkpath(FILE_PATH);
|
|
|
|
}
|
|
|
|
if (g_debug.file != nullptr)
|
|
|
|
{
|
|
|
|
delete g_debug.file;
|
|
|
|
}
|
|
|
|
|
|
|
|
g_debug.file = new QFile(FILE_PATH + name + FILE_NAME);
|
|
|
|
qInfo() << "set log file as " << g_debug.file->fileName();
|
|
|
|
if (g_debug.file->exists())
|
|
|
|
{
|
|
|
|
if (g_debug.file->remove() != true)
|
|
|
|
qWarning() << "remove file " << g_debug.file->fileName() << "err";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!g_debug.file->open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
qInstallMessageHandler(print_to_file);
|
|
|
|
}
|
|
|
|
|
|
|
|
void mydebug_init()
|
|
|
|
{
|
|
|
|
qSetMessagePattern("[%{type}] %{time yyyy-MM-dd hh:mm:ss.zzz} %{function}:%{line} %{message}");
|
|
|
|
qInfo() << "set log as console.";
|
|
|
|
qInstallMessageHandler(print_to_console);
|
|
|
|
}
|
|
|
|
|
|
|
|
void print_to_console(QtMsgType type, const QMessageLogContext &context, const QString &msg)
|
|
|
|
{
|
|
|
|
g_debug.mutex.lock();
|
|
|
|
QByteArray localMsg = msg.toLocal8Bit();
|
|
|
|
QString current_date_time = QDateTime::currentDateTime().toString("hh:mm:ss.zzz");
|
|
|
|
Q_UNUSED(current_date_time);
|
|
|
|
if(context.file!=0){
|
|
|
|
fprintf(stderr, "%s | %s | %s:%u | %s\n", msgHead[type], current_date_time.toLocal8Bit().constData(), &context.file[20], context.line, localMsg.constData());
|
|
|
|
}else{
|
2023-12-04 18:15:37 +08:00
|
|
|
fprintf(stderr,"%s | %s | %s\n", msgHead[type], current_date_time.toLocal8Bit().constData(), localMsg.constData());
|
2023-12-02 11:27:25 +08:00
|
|
|
}
|
|
|
|
g_debug.mutex.unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
void print_to_file(QtMsgType type, const QMessageLogContext &context, const QString &msg)
|
|
|
|
{
|
|
|
|
g_debug.mutex.lock();
|
|
|
|
QByteArray localMsg = msg.toLocal8Bit();
|
|
|
|
QString current_date_time = QDateTime::currentDateTime().toString("hh:mm:ss.zzz");
|
|
|
|
if (g_debug.file)
|
|
|
|
{
|
|
|
|
QTextStream tWrite(g_debug.file);
|
|
|
|
if(context.file!=0){
|
|
|
|
tWrite << QString::asprintf("%s | %s | %s:%u | %s\n", msgHead[type], current_date_time.toLocal8Bit().constData(), &context.file[20], context.line, localMsg.constData());
|
|
|
|
}else{
|
2023-12-04 18:15:37 +08:00
|
|
|
tWrite << QString::asprintf("%s | %s | %s\n", msgHead[type], current_date_time.toLocal8Bit().constData(), localMsg.constData());
|
2023-12-02 11:27:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
g_debug.mutex.unlock();
|
|
|
|
}
|