Ai
3 Star 18 Fork 6

John Yet/SMSS安全通讯框架

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
loggerfactory.cpp 2.31 KB
一键复制 编辑 原始数据 按行查看 历史
John Yet 提交于 2020-10-06 15:53 +08:00 . 更新框架 添加tcp拆包方案
#include "loggerfactory.h"
#include <QCoreApplication>
#include <QDir>
#include <QDateTime>
#include <QFile>
#include <QDebug>
#include <mutex>
#include <iostream>
using namespace std;
stream::LogRank LoggerFactory::log_rank_ = stream::DEBUG;
string LoggerFactory::log_path_ = "log/";
static void OutputMessage(QtMsgType type, const QMessageLogContext &ctx, const QString &msg)
{
QString txt;
txt
.append(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"))
.append(" [").append(ctx.function).append(":").append(to_string(ctx.line).c_str()).append("]");
stream::LogRank curr_log_rank;
switch (type)
{
case QtDebugMsg:
txt.append(" DEBUG ");
curr_log_rank = stream::LogRank::DEBUG;
break;
case QtInfoMsg:
txt.append(" INFO ");
curr_log_rank = stream::LogRank::INFO;
break;
case QtWarningMsg:
txt.append(" WQRNING ");
curr_log_rank = stream::LogRank::WARNING;
break;
case QtCriticalMsg:
txt.append(" CRITICAL ");
curr_log_rank = stream::LogRank::CRITIAL;
break;
case QtFatalMsg:
txt.append(" FATAL ");
curr_log_rank = stream::LogRank::FATAL;
break;
}
if(curr_log_rank >= LoggerFactory::log_rank_)
{
QString log_path(LoggerFactory::log_path_.c_str());
if(!log_path.startsWith("/"))
{
log_path = QCoreApplication::applicationDirPath() + "/" + log_path;
}
if(!log_path.endsWith("/"))
{
log_path.append("/");
}
QDir dir(log_path);
if(!dir.exists())
{
dir.mkdir(log_path);
}
QString filename = log_path + "log." + QDateTime::currentDateTime().toString("yyyy-MM-dd") + ".txt";
QFile log_file(filename);
static mutex mtx;
mtx.lock();
if(log_file.open(QIODevice::WriteOnly | QIODevice::Append))
{
QTextStream stream(&log_file);
stream << txt << msg << endl;
log_file.close();
}
mtx.unlock();
cout << txt.toStdString() << msg.toStdString() << endl;
}
}
void LoggerFactory::Init(stream::LogRank log_rank)
{
LoggerFactory::log_rank_ = log_rank;
qInstallMessageHandler(OutputMessage);
}
LoggerFactory::LoggerFactory()
{
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/learnhow/encrypted_stream.git
git@gitee.com:learnhow/encrypted_stream.git
learnhow
encrypted_stream
SMSS安全通讯框架
2.0

搜索帮助