自己写的一个日志记录类,记录一下
Logout.h
#pragma once
class CLogout
{
public:
CLogout(void);
~CLogout(void);
void Logout(char* msg, char* msg2=NULL, char*msg3=NULL);
private:
bool bLogout;
char szLogFilePath[MAX_PATH];
char szMsg[MAX_PATH*2];
};
Logout.cpp
#include "StdAfx.h"
#include "Logout.h"
#include <time.h>
#include <stdarg.h>
#pragma comment (lib, "Shell32.lib")
CLogout::CLogout(char* logName)
{
GetModuleFileNameA(NULL, this->szLogFilePath, MAX_PATH);
char * pos = strrchr(szLogFilePath, '\\');
*(pos+1) = 0;
strcat_s(szLogFilePath, logName);
// if (SHGetSpecialFolderPathA(NULL, szLogFilePath, CSIDL_APPDATA, TRUE))
// {
// strcat_s(szLogFilePath, "\\adbmgr\\");
// BOOL b = CreateDirectoryA(szLogFilePath, NULL);
//
// if (b || GetLastError() == ERROR_ALREADY_EXISTS)
// {
// strcat_s(szLogFilePath, "adbmgr.log");
// bLogout = true;
// }
// }
bLogout = true;
}
CLogout::~CLogout(void)
{
}
void CLogout::Logout(char* pszFormat, ...)
{
if (bLogout)
{
FILE* fp;
fopen_s(&fp, szLogFilePath, "a+");
if (fp)
{
va_list args;
va_start(args, pszFormat); //一定要“...”之前的那个参数
time_t t = time(0);
char tmp[64];
strftime(tmp, sizeof(tmp), "%Y/%m/%d %X",localtime(&t));
sprintf_s(szMsg, "%s ", tmp); //先打印时间
vsnprintf(szMsg+strlen(szMsg), MAX_PATH*2-strlen(szMsg), pszFormat, args); //后再打印消息
va_end(args);
fwrite(szMsg, 1, strlen(szMsg), fp);
fclose(fp);
}
}
}