You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
165 lines
4.0 KiB
C++
165 lines
4.0 KiB
C++
#include "Logthread.h"
|
|
#include <QDir>
|
|
#include <QMutex>
|
|
#include <QDate>
|
|
|
|
const char PATH_LogPath[] = "D:/log/";
|
|
const char Suffix[] = ".txt";
|
|
bool CLog::isFileReady = false;
|
|
bool CLog::isRecord2File = true;
|
|
QFile localFile;
|
|
QMutex mutexLog;
|
|
|
|
CLog::CLog()
|
|
{
|
|
}
|
|
|
|
void CLog::setLogType(const CLog::CLOG_TYPE& level)
|
|
{
|
|
}
|
|
|
|
bool CLog::init(LogConfig& logConfig)
|
|
{
|
|
isRecord2File = logConfig.isRecord2File;
|
|
QString logDir = QString(PATH_LogPath);
|
|
if (createDir(logDir))
|
|
{
|
|
QString fileName = logDir + QDir::separator() + QDate::currentDate().toString("yyyy-MM-dd") + QString(Suffix);
|
|
QFileInfo fi(fileName);
|
|
if (fi.exists())
|
|
{
|
|
if (!localFile.exists())
|
|
{
|
|
localFile.setFileName(fileName);
|
|
if (localFile.open(QFile::WriteOnly | QFile::Append | QFile::Text))
|
|
{
|
|
isFileReady = true;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
isFileReady = true;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
localFile.setFileName(fileName);
|
|
if (localFile.open(QFile::WriteOnly | QFile::Append | QFile::Text))
|
|
{
|
|
isFileReady = true;
|
|
}
|
|
}
|
|
}
|
|
return isFileReady;
|
|
}
|
|
|
|
bool CLog::createDir(QString dirPath)
|
|
{
|
|
QFileInfo fileInfo(dirPath);
|
|
if (!fileInfo.exists())
|
|
{
|
|
QDir tmpDir;
|
|
return tmpDir.mkpath(dirPath);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
void CLog::log(CLOG_TYPE nType, const char* fileDesc, const char* functionDesc, int lineNum, const char* data, ...)
|
|
{
|
|
QMutexLocker locker(&mutexLog);
|
|
if (isFileReady)
|
|
{
|
|
QString recordInfo = QString("[%1]").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss:zzz"));
|
|
recordInfo.append(getTypeDesc(nType));
|
|
|
|
#ifndef QT_NO_DEBUG
|
|
recordInfo.append(QString("[%1:%2:%3]").arg(fileDesc).arg(functionDesc).arg(lineNum));
|
|
#endif
|
|
va_list vlist;
|
|
va_start(vlist, data);
|
|
|
|
QByteArray byteArray;
|
|
#if defined(Q_OS_WIN)
|
|
int recordLen = _vscprintf(data, vlist);
|
|
byteArray.resize(recordLen);
|
|
#else
|
|
byteArray.resize(1024);
|
|
#endif
|
|
vsprintf(byteArray.data(), data, vlist);
|
|
recordInfo.append(byteArray);
|
|
va_end(vlist);
|
|
|
|
recordInfo.append("\n");
|
|
|
|
if (isRecord2File) {
|
|
localFile.write(recordInfo.toLocal8Bit().data(), recordInfo.toLocal8Bit().length());
|
|
localFile.flush();
|
|
}
|
|
else {
|
|
// qDebug()<<recordInfo;
|
|
}
|
|
}
|
|
}
|
|
|
|
void CLog::log(CLOG_TYPE nType, const char* data, ...)
|
|
{
|
|
QMutexLocker locker(&mutexLog);
|
|
if (isFileReady)
|
|
{
|
|
QString recordInfo = QString("[%1]").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss:zzz"));
|
|
recordInfo.append(getTypeDesc(nType));
|
|
recordInfo.append(" ");
|
|
|
|
va_list vlist;
|
|
va_start(vlist, data);
|
|
|
|
QByteArray byteArray;
|
|
#if defined(Q_OS_WIN)
|
|
int recordLen = _vscprintf(data, vlist);
|
|
byteArray.resize(recordLen);
|
|
#else
|
|
byteArray.resize(1024);
|
|
#endif
|
|
vsprintf(byteArray.data(), data, vlist);
|
|
recordInfo.append(byteArray);
|
|
va_end(vlist);
|
|
|
|
recordInfo.append("\n");
|
|
|
|
if (isRecord2File) {
|
|
localFile.write(recordInfo.toLocal8Bit().data(), recordInfo.toLocal8Bit().length());
|
|
localFile.flush();
|
|
}
|
|
else {
|
|
// qDebug()<<recordInfo;
|
|
}
|
|
}
|
|
}
|
|
|
|
QString CLog::getTypeDesc(CLog::CLOG_TYPE type)
|
|
{
|
|
/*
|
|
switch (type)
|
|
{
|
|
case CLOG_TYPE::STARTAPP:
|
|
return "[INFO]";
|
|
case CLOG_TYPE::STARTWORK:
|
|
return "[WARNING]";
|
|
case CLOG_TYPE::PAUSEWORK:
|
|
return "[ERROR]";
|
|
}
|
|
*/
|
|
return "[INFO]";
|
|
}
|
|
|
|
void CLog::recMegFromCigarette(QString str)
|
|
{
|
|
CLog::LogConfig logConfig;
|
|
logConfig.isRecord2File = true;
|
|
logConfig.level = 0;
|
|
CLog::init(logConfig);
|
|
//qDebug() << GetCurrentThreadId() << "WinAPI";
|
|
CLOG_INFO(str.toStdString().c_str());
|
|
}
|