226 lines
5.5 KiB
C++
226 lines
5.5 KiB
C++
#include "stdafx.h"
|
|
#include "logger.h"
|
|
#include <atltime.h>
|
|
#include <sys/timeb.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <sys/stat.h>
|
|
#include <iostream>
|
|
#include <io.h>
|
|
#include <direct.h>
|
|
|
|
using namespace std;
|
|
|
|
void CLogger::SendAtTime(const TCHAR* buffer)
|
|
{
|
|
EnterCriticalSection(&m_lockLogger);
|
|
if (!m_File)
|
|
{
|
|
m_File = _wfsopen(m_FileName, _T("at"), _SH_DENYWR);
|
|
}
|
|
if (m_File)
|
|
{
|
|
CTime _cTime = CTime::GetCurrentTime();
|
|
CString csTime = _cTime.Format("[%m/%d %H:%M] ");
|
|
_ftprintf(m_File,_T("%s"), csTime);
|
|
_ftprintf(m_File, _T("%s\r\n"), buffer);
|
|
}
|
|
|
|
LeaveCriticalSection(&m_lockLogger);
|
|
}
|
|
|
|
void CLogger::Send(LPCTSTR format, ...)
|
|
{
|
|
EnterCriticalSection(&m_lockLogger);
|
|
if (!m_File)
|
|
{
|
|
m_File = _wfsopen(m_FileName, _T("at"), _SH_DENYWR);
|
|
}
|
|
int length = 0;
|
|
va_list list;
|
|
va_start(list, format);
|
|
length = vswprintf_s(m_Str, 5000, format, list);
|
|
if (m_File)
|
|
{
|
|
_ftprintf(m_File, m_Str);
|
|
}
|
|
va_end(list);
|
|
LeaveCriticalSection(&m_lockLogger);
|
|
}
|
|
|
|
void CLogger::SendAndFlush(LPCTSTR format, ...)
|
|
{
|
|
EnterCriticalSection(&m_lockLogger);
|
|
int length = 0;
|
|
va_list list;
|
|
|
|
va_start(list, format);
|
|
length = vswprintf_s(m_Str2, 5000, format, list);
|
|
Send(m_Str2);
|
|
|
|
if (m_File)
|
|
{
|
|
fclose(m_File);
|
|
m_File = nullptr;
|
|
if (m_FileName.GetLength() > 0)
|
|
m_File = _wfsopen(m_FileName, _T("at"), _SH_DENYWR);
|
|
}
|
|
va_end(list);
|
|
LeaveCriticalSection(&m_lockLogger);
|
|
}
|
|
|
|
//vswprintf_s()
|
|
//参考连接:https://www.educative.io/answers/what-is-vswprintfs-in-c
|
|
void CLogger::SendAndFlushWithTime(LPCTSTR format, ...)
|
|
{
|
|
EnterCriticalSection(&m_lockLogger);
|
|
if (!IsEnabledLog)
|
|
{
|
|
return;
|
|
}
|
|
if (!m_File)
|
|
{
|
|
m_File = _wfsopen(m_FileName, _T("at"), _SH_DENYNO); //追加
|
|
}
|
|
if (m_File)
|
|
{
|
|
int length = 0;
|
|
va_list list;
|
|
//在C中,当无法列出传递函数的所有实参的类型和数目时,可以用省略号指定参数表
|
|
va_start(list, format);
|
|
//vswprintf()函数将宽字符串写入宽字符串缓冲区
|
|
|
|
//ws:指定指向给定宽字符串缓冲区的指针,该缓冲区将存储结果
|
|
//len : 指定写回缓冲区的宽字符的最大长度,包括终止的空字符
|
|
//format : 指定指向空终止的宽字符串的指针
|
|
//arg : 指定标识变量参数列表的值
|
|
length = vswprintf(m_Str2, 5000, format, list);
|
|
CTime _cTime = CTime::GetCurrentTime();
|
|
CString csTime = _cTime.Format("[%Y/%m/%d %H:%M:%S");
|
|
struct _timeb timebuffer;
|
|
_ftime64_s(&timebuffer);
|
|
if (m_File)
|
|
{
|
|
_ftprintf(m_File, _T("%s:%03d] "), csTime, timebuffer.millitm);
|
|
}
|
|
if (m_File)
|
|
{
|
|
_ftprintf(m_File, m_Str2);
|
|
}
|
|
if (m_File)
|
|
{
|
|
fclose(m_File);
|
|
m_File = nullptr;
|
|
}
|
|
|
|
va_end(list);
|
|
}
|
|
//放弃当前线程对锁定部分的所有权。一旦锁定部分的所有权被放弃,那么请求访问临界区的下一个线程,将可以对锁定部分进行操作。每一个调用EnterCriticalSection的线程,都应该调用一次LeaveCriticalSection。
|
|
|
|
//使一个线程可以使用一个临界段对象来进行互斥同步。这个过程需要优先创建一个临界区结构体变量(分配使用内存)。在使用临界区之前, 待操作临界区的进程必须调用InitializeCriticalSection 或者 InitializeCriticalSectionAndSpinCount函数来初始化临界区。
|
|
// 一个线程使用EnterCriticalSection 或TryEnterCriticalSection函数来获得关键部分对象的所有权时,该线程必须在离开临界区时调用LeaveCriticalSection。
|
|
|
|
LeaveCriticalSection(&m_lockLogger);
|
|
}
|
|
|
|
void CLogger::SendAndFlushPerMode(LPCTSTR format, ...)
|
|
{
|
|
EnterCriticalSection(&m_lockLogger);
|
|
int length = 0;
|
|
va_list list;
|
|
|
|
va_start(list, format);
|
|
length = vswprintf_s(m_Str2, 5000, format, list);
|
|
Send(m_Str2);
|
|
|
|
if ((m_lLogMask & LOGFLUSH) && m_File)
|
|
{
|
|
fclose(m_File);
|
|
m_File = nullptr;
|
|
if (m_FileName.GetLength() > 0)
|
|
m_File = _wfsopen(m_FileName, _T("at"), _SH_DENYWR);
|
|
}
|
|
va_end(list);
|
|
LeaveCriticalSection(&m_lockLogger);
|
|
}
|
|
|
|
string ConvertCharToString(char* a, int size)
|
|
{
|
|
int i;
|
|
string s = "";
|
|
for (i = 0; i < size; i++)
|
|
{
|
|
s = s + a[i];
|
|
}
|
|
return s;
|
|
}
|
|
|
|
//删除指定目录以及目录下的所有文件
|
|
void DelFiles(string path)
|
|
{
|
|
//在目录后面加上"\\*.*"进行第一次搜索
|
|
string newDir = path + "\\*.*";
|
|
//用于查找的句柄
|
|
intptr_t handle;
|
|
struct _finddata_t fileinfo;
|
|
//第一次查找
|
|
handle = _findfirst(newDir.c_str(), &fileinfo);
|
|
|
|
if (handle == -1)
|
|
{
|
|
//cout << "无文件" << endl;
|
|
system("pause");
|
|
return;
|
|
}
|
|
|
|
do
|
|
{
|
|
if (fileinfo.attrib & _A_SUBDIR)
|
|
{
|
|
//如果为文件夹,加上文件夹路径,再次遍历
|
|
if (strcmp(fileinfo.name, ".") == 0 || strcmp(fileinfo.name, "..") == 0)
|
|
continue;
|
|
|
|
// 在目录后面加上"\\"和搜索到的目录名进行下一次搜索
|
|
newDir = path + "\\" + fileinfo.name;
|
|
DelFiles(newDir.c_str()); //先遍历删除文件夹下的文件,再删除空的文件夹
|
|
cout << newDir.c_str() << endl;
|
|
if (_rmdir(newDir.c_str()) == 0)
|
|
{
|
|
//删除空文件夹
|
|
cout << "delete empty dir success" << endl;
|
|
}
|
|
else
|
|
{
|
|
cout << "delete empty dir error" << endl;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
string file_path = path + "\\" + fileinfo.name;
|
|
cout << file_path.c_str() << endl;
|
|
if (remove(file_path.c_str()) == 0)
|
|
{
|
|
//删除文件
|
|
cout << "delete file success" << endl;
|
|
}
|
|
else
|
|
{
|
|
cout << "delete file error" << endl;
|
|
}
|
|
}
|
|
}
|
|
while (!_findnext(handle, &fileinfo));
|
|
|
|
_findclose(handle);
|
|
}
|
|
|
|
string ByteArrayToString(byte byteArray[])
|
|
{
|
|
char str[sizeof(byteArray) + 1];
|
|
memcpy(str, byteArray, sizeof(byteArray));
|
|
str[sizeof(byteArray)] = '\0';
|
|
|
|
return str;
|
|
}
|