#define PATH_DELIMITER '\\'
bool createDirectory(const std::string folder) {
std::string folder_builder;
std::string sub;
sub.reserve(folder.size());
for (auto it = folder.begin(); it != folder.end(); ++it) {
//cout << *(folder.end()-1) << endl;
const char c = *it;
sub.push_back(c);
if (c == PATH_DELIMITER || it == folder.end() - 1) {
folder_builder.append(sub);
if (0 != ::_access(folder_builder.c_str(), 0)) {
// this folder not exist
if (0 != ::_mkdir(folder_builder.c_str())) {
return false;
}
}
sub.clear();
}
}
return true;
}
//获取当前日志文件名称
static void GetCurLogFileName(char *pName, Uint32 nSize)
{
char tmp[64];
memset(tmp, 0, 64);
memset(pName, 0, nSize);
time_t timep;
time(&timep);
strftime(tmp, sizeof(tmp), "%Y%m%d", localtime(&timep));//%H%M%S
createDirectory("seecar_log");
sprintf(pName, "seecar_log\\%s.txt", tmp);
}
#include <stdarg.h>
//写日志文件 ...可变参数, va_start va_end获取可变参数
static void WriteLogInfo(const Char *szFormat, ...)
{
Char msgBuf[1024];
Uint32 msgLen = 0;
va_list pvList;
memset(msgBuf, 0, sizeof(msgBuf));
SYSTEMTIME st;
// GetLocalTime(&st);
// msgLen = _snprintf(msgBuf, 1024, "%04d-%02d-%02d %02d:%02d:%02d.%03d: ",
// st.wYear, st.wMonth, st.wDay,
// st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
va_start(pvList, szFormat);
vsnprintf(msgBuf + msgLen, MAX_PRINT_MSG_LEN - msgLen, szFormat, pvList);
va_end(pvList);
char fileName[256];
GetCurLogFileName(fileName, 256);
FILE *fp;
if((fp=fopen(fileName,"wb+")) == NULL){
return;
}
fwrite(msgBuf,strlen(msgBuf),1,fp);
fclose(fp);
}
//删除文件夹下及文件夹下的所有文件
static int removeDir(std::string dirPath)
{
struct _finddata_t fb;
std::string path;
long handle;
int resultone;
int noFile;
noFile = 0;
handle = 0;
path = dirPath + "\\*";
handle = _findfirst(path.c_str(), &fb);
if (handle != -1)
{
while (0 == _findnext(handle, &fb))
{
noFile = strcmp(fb.name, "..");
if (0 != noFile)
{
path.clear();
path = dirPath + "\\" + fb.name;
if (fb.attrib == 16)
{
removeDir(path);
if (fb.size == 0)
{
rmdir(path.c_str());
}
}
else
{
remove(path.c_str());
}
}
}
_findclose(handle);
return 0;
}
}