2,新增“Apps”; 3,新增“Common”; 4,新增“FileList”; 5,新增“MediaX”; 6,新增“OpenSource”; 7,新增“Samples”; 8,新增“SoftwareBusinessLines”.
169 lines
4.0 KiB
C++
169 lines
4.0 KiB
C++
#include "qlog.h"
|
|
#include <io.h>
|
|
#include <direct.h>
|
|
#include <iostream>
|
|
//#include <Windows.h>
|
|
#include <stdio.h>
|
|
#include <tchar.h>
|
|
#include <QDir>
|
|
#include <QApplication>
|
|
#define LOG_SIZE 10*1024*1024
|
|
|
|
|
|
QLog::QLog(QString strDirName): m_strDirName(strDirName)
|
|
{
|
|
m_hUDPMutex = CreateMutex(NULL, false, NULL);
|
|
m_isOpen = true;
|
|
fp = nullptr;
|
|
m_thread = nullptr;
|
|
m_currentIndex = 0;
|
|
m_strDir = QApplication::applicationDirPath() + "/Log/" + strDirName;
|
|
QDir tmpDir(m_strDir);
|
|
if (!tmpDir.exists())
|
|
{
|
|
tmpDir.mkpath(m_strDir);
|
|
}
|
|
}
|
|
|
|
void QLog::setLogOpen(bool bl)
|
|
{
|
|
m_isOpen = bl;
|
|
}
|
|
|
|
void QLog::start()
|
|
{
|
|
SYSTEMTIME sys;
|
|
GetLocalTime(&sys);
|
|
sprintf(m_fileName, "%04d_%02d_%02d_%02d_%02d_%03d", sys.wYear, sys.wMonth, sys.wDay, sys.wHour, \
|
|
sys.wMinute, sys.wSecond, sys.wMilliseconds);
|
|
m_isExit = false;
|
|
char filePath[256] = {0};
|
|
sprintf(filePath, "./Log/%s/%s_%d.txt", m_strDirName.toStdString().c_str(), m_fileName, m_currentIndex);
|
|
fp = fopen(filePath, "a+");
|
|
if (m_thread)
|
|
{
|
|
delete m_thread;
|
|
m_thread = nullptr;
|
|
}
|
|
m_thread = new Thread(this, &fun);
|
|
m_thread->start();
|
|
}
|
|
|
|
void QLog::addLog(const char* format, ...)
|
|
{
|
|
if (m_isOpen)
|
|
{
|
|
WaitForSingleObject(m_hUDPMutex, INFINITE);
|
|
char szBuffer[5000];
|
|
memset(szBuffer, 0x0, sizeof(szBuffer));
|
|
va_list list;
|
|
va_start(list, format);
|
|
int nByteWritten = vsnprintf(szBuffer, sizeof(szBuffer), format, list);
|
|
va_end(list);
|
|
SYSTEMTIME sys;
|
|
GetLocalTime(&sys);
|
|
char tmp[256] = {0};
|
|
sprintf(tmp, "%04d_%02d_%02d_%02d_%02d_%02d_%03d:", sys.wYear, sys.wMonth, sys.wDay, sys.wHour, \
|
|
sys.wMinute, sys.wSecond, sys.wMilliseconds);
|
|
string str = string(tmp) + string(szBuffer) + string("\n");
|
|
m_logList.push(str);
|
|
ReleaseMutex(m_hUDPMutex);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
void QLog::stop()
|
|
{
|
|
m_isExit = true;
|
|
if (m_thread)
|
|
{
|
|
m_thread->join();
|
|
delete m_thread;
|
|
m_thread = nullptr;
|
|
}
|
|
if (fp)
|
|
{
|
|
fclose(fp);
|
|
fp = nullptr;
|
|
}
|
|
}
|
|
|
|
bool QLog::isStart()
|
|
{
|
|
if (!m_thread)
|
|
{
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
int QLog::createDirectory(std::string path)
|
|
{
|
|
int len = path.length();
|
|
char tmpDirPath[256] = { 0 };
|
|
for (int i = 0; i < len; i++)
|
|
{
|
|
tmpDirPath[i] = path[i];
|
|
if (tmpDirPath[i] == '\\' || tmpDirPath[i] == '/')
|
|
{
|
|
if (_access(tmpDirPath, 0) == -1)
|
|
{
|
|
int ret = _mkdir(tmpDirPath);
|
|
if (ret == -1)
|
|
return ret;
|
|
}
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
void QLog::run()
|
|
{
|
|
while (!m_isExit)
|
|
{
|
|
if (!m_logList.empty())
|
|
{
|
|
if (fp)
|
|
{
|
|
fseek(fp, 0, SEEK_END);
|
|
long size = ftell(fp);
|
|
fseek(fp, 0, SEEK_CUR);
|
|
if (size < LOG_SIZE)
|
|
{
|
|
WaitForSingleObject(m_hUDPMutex, INFINITE);
|
|
string str = m_logList.front();
|
|
m_logList.pop();
|
|
ReleaseMutex(m_hUDPMutex);
|
|
fwrite(str.c_str(), str.length(), 1, fp);
|
|
}
|
|
else
|
|
{
|
|
m_currentIndex++;
|
|
fclose(fp);
|
|
SYSTEMTIME sys;
|
|
GetLocalTime(&sys);
|
|
sprintf(m_fileName, "%04d_%02d_%02d_%02d_%02d_%03d", sys.wYear, sys.wMonth, sys.wDay, sys.wHour, \
|
|
sys.wMinute, sys.wSecond, sys.wMilliseconds);
|
|
char filePath[256] = {0};
|
|
sprintf(filePath, "./Log/%s/%s_%d.txt", m_strDirName.toStdString().c_str(), m_fileName, m_currentIndex);
|
|
fp = fopen(filePath, "a+");
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
Sleep(1);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void QLog::fun(void* pthis)
|
|
{
|
|
QLog* p = (QLog*)pthis;
|
|
p->run();
|
|
}
|