MBCS 로그 인쇄(1)

10681 단어
개술
로그 파일의 최대 10M이며, 파일 이름은 일련 번호에 따라 수정되고 자동으로 이름이 지정됩니다. 최신 로그는 일련 번호 0의 로그입니다(예: 로그 AgentRest log 0.log).로그가 10개로 누적되면 프로그램에서 가장 오래된 로그를 삭제합니다.
헤더 파일
#pragma once
//          
enum Error_Code
{
	ERROR_OK = 0,
	ERROR_PARAM_ERROR,					//    
	ERROR_INITIALED,					//       
	ERROR_UNINITIAL,					//    
	ERROR_NOT_SIGNIN,					//   
	ERROR_SIGNINED,						//    ,     
	ERROR_HTTP_FAIL,					//HTTP    
	ERROR_SIGNINMEDIASERVER_FAIL,
	ERROR_ANALYZE_FAIL,					//HTTP      
	ERROR_UNKNOWN,
	ERROR_ALLOC_MEMORY,					//      
	ERROR_DEBUG_NORMAL = 400,
	ERROR_DEBUG_ERROR = 401,
	ERROR_SYSTEM = 500,
	ERROR_EVENT = 501,
	ERROR_BOOLEN = 502,
	ERROR_WARN = 999
};

/****************************************************************************/
//       10M
const USHORT LOGINFO_MAX_LEN = 600;					//        
const USHORT LOGINFO_CONTENT_LEN = 450;
const USHORT LOGINFO_LOGFILE_MAX_LEN = 260;			//         
const USHORT LOGINFO_MAX_NUM_LEN = 10;				//      
const ULONG LOGINFO_MAX_SIZE = 10 * 1024 * 1024;	//        10M
//      
const USHORT APP_PATH_LEN  =  260;					//    
const USHORT MAX_PATH_LEN = 200;					//       
const USHORT PATH_LEN = 260;
const USHORT MAX_DRIVE_LEN = 10;					//    
const USHORT MAX_FOLDER_LEN = 200;					//     
const USHORT MAX_FILE_LEN = 100;					//     
const USHORT MAX_EXT_LEN = 10;						//    

#define LOGFILEEXT    ".log"						//      
#define LOGFILE_LOG   "_log"
#define LOGFILENAME "AgentRest"   
#define DEFAULT_LOGFILEAPTH "C:/agentrest/"
/****************************************************************************/

class CLog
{
public:
	CLog(void);
	~CLog(void);

private:
	CString m_strAppPath;							//      
	CString m_strAppFile;							//     

	DWORD m_dwProcessID;
	
	FILE* m_fp;										//      
	CRITICAL_SECTION  m_csLog;						//       

public:
	int WriteLog(const int nRslt, LPCSTR FormatString, ...);
};

원본 파일
#include "StdAfx.h"
#include "Log.h"
#include <Shlwapi.h>


CLog::CLog(void):m_dwProcessID(0), m_fp(NULL) 
{
	m_dwProcessID = GetCurrentProcessId();							//    

	InitializeCriticalSection(&m_csLog);

	/*****************************       ********************************/
	char szFolder[PATH_LEN];										//      
	memset(szFolder, 0, PATH_LEN);
	char szAppPath[APP_PATH_LEN];
	memset(szAppPath, 0 ,APP_PATH_LEN);
	char szAppFolder[APP_PATH_LEN];
	memset(szAppFolder, 0, APP_PATH_LEN);
	char szDrive[PATH_LEN];											//     
	memset(szDrive, 0, PATH_LEN);
	char szFileName[MAX_FILE_LEN];
	memset(szFileName, 0, MAX_FILE_LEN);

	DWORD dwSize = GetModuleFileName(NULL, szAppPath, APP_PATH_LEN);
	if (dwSize <= 0 || dwSize >= MAX_PATH_LEN)						//      ,        
	{
		m_strAppPath = DEFAULT_LOGFILEAPTH;
		return;
	}
	_splitpath(szAppPath, szDrive, szFolder, szFileName, NULL);		//          
	sprintf_s(szAppFolder, "%s%s", szDrive, szFolder);

	//      
	if (strlen(szAppFolder) >= MAX_PATH_LEN || !PathFileExists(szAppFolder))
	{
		m_strAppPath = DEFAULT_LOGFILEAPTH;
		return;
	}

	m_strAppPath = szAppFolder;   
	m_strAppFile = szFileName;
	/****************************************************************************/

}

CLog::~CLog(void)
{
	if (NULL != m_fp)
	{
		fclose(m_fp);
		m_fp = NULL;
	}

	//         
	DeleteCriticalSection(&m_csLog);
}

//======================================================================================================
//      :WriteLog
//     :    
//     :szLog    :        
//           usLevel  :     ,     4     
//                       1:      
//                       2:     
//                       3:       
//           nLogSize :       ,     10M          
//     : 
//     :0:  
//           1:  
//           2:               
//     :
//     :
//         :
//     :      HWIDriver.log,
//                      ,       AgentRest_n.log(n>=1 && n<=10)
//			 n  10,   AgentBarOCX_10.log
//========================================================================================================
int CLog::WriteLog(const int nRslt, LPCSTR FormatString, ...)
{
	//Log content
	char szLogInfo[LOGINFO_MAX_LEN];
	memset(szLogInfo, 0, LOGINFO_MAX_LEN);

	//      
	char szLogPath[APP_PATH_LEN];
	memset(szLogPath, 0, APP_PATH_LEN);

	//get current time
	SYSTEMTIME st;

	char szTmp[LOGINFO_MAX_NUM_LEN];
	memset(szTmp, 0, LOGINFO_MAX_NUM_LEN);

	//log file whole path
	char szLogFile[LOGINFO_LOGFILE_MAX_LEN];
	memset(szLogFile, 0, LOGINFO_LOGFILE_MAX_LEN);

	//       ,        
	char szLogFileRename[LOGINFO_LOGFILE_MAX_LEN];
	memset(szLogFileRename, 0, LOGINFO_LOGFILE_MAX_LEN);

	FILE* fp = NULL;

	int iIndex = 0;
	int nReadSize = 0;
	int ind = 0;

	CString strContent = "";

	char szContent[LOGINFO_CONTENT_LEN + 1];
	memset(szContent, 0, LOGINFO_CONTENT_LEN + 1);

	va_list args;
	va_start(args, FormatString);

	strContent.FormatV(FormatString, args);
	va_end(args);

	//   450   
	if (strContent.GetLength() >= LOGINFO_CONTENT_LEN)
	{
		strncpy_s(szContent, strContent.GetBuffer(), LOGINFO_CONTENT_LEN - 6);
		//   ,     ,    
		strcat_s(szContent, "...\r
"); } else { strcpy_s(szContent, strContent.GetBuffer()); } strContent.ReleaseBuffer(); //[yy-mm-dd hh:mm:ss.zzz] //[2012-11-19 11:30:27.762][4704] GetLocalTime(&st); sprintf_s(szLogInfo, "[%04d-%02d-%02d %02d:%02d:%02d.%03d][%d]", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds, m_dwProcessID); //[yy-mm-dd hh:mm:ss.zzz][LogLevel] // memset(szTmp, 0, 256); if (ERROR_OK == nRslt) { strcat_s(szLogInfo, "[Normal]"); } else if (ERROR_WARN == nRslt) { strcat_s(szLogInfo, "[Warn]"); } else if (ERROR_SYSTEM == nRslt) { // } else if (ERROR_EVENT == nRslt) { // } else if (ERROR_DEBUG_NORMAL == nRslt) { strcat_s(szLogInfo, "[Debug][Normal]"); } else if (ERROR_DEBUG_ERROR == nRslt) { strcat_s(szLogInfo, "[Debug][Error]"); } else { if (nRslt-100 >= 0) { if (0 == (nRslt - 100) || 1 == (nRslt - 100)) { strcat_s(szLogInfo, "[Normal]"); } else { strcat_s(szLogInfo, "[Fail]"); } } else { strcat_s(szLogInfo, "[Fail]"); } } strcat_s(szLogInfo, szContent); sprintf_s(szLogPath, "%s%s_%s%s", m_strAppPath.GetBuffer(), m_strAppFile.GetBuffer() , LOGFILENAME, LOGFILE_LOG); m_strAppFile.ReleaseBuffer(); m_strAppPath.ReleaseBuffer(); sprintf_s(szLogFile, "%s%s%s", szLogPath, "_0", LOGFILEEXT); if (NULL == m_fp) { m_fp = fopen(szLogFile, "a+"); } if (NULL == m_fp) { return 1; } fseek(m_fp, SEEK_CUR, SEEK_END); nReadSize = ftell(m_fp); // , if (nReadSize < LOGINFO_MAX_SIZE) { EnterCriticalSection(&m_csLog); fwrite(szLogInfo, sizeof(char), strlen(szLogInfo), m_fp); fflush(m_fp); LeaveCriticalSection(&m_csLog); } // 10M, HW_iDriver_Log_0.log HW_iDriver_Log_1.log else { fclose(m_fp); m_fp = NULL; for (iIndex = 9; iIndex >= 1; iIndex--) { memset(szTmp, 0, LOGINFO_MAX_NUM_LEN); memset(szLogFile, 0, LOGINFO_LOGFILE_MAX_LEN); strcat_s(szLogFile, szLogPath); sprintf_s(szTmp, "_%d", iIndex); strcat_s(szLogFile, szTmp); strcat_s(szLogFile, ".log"); fp = fopen(szLogFile,"r"); if(fp != NULL) { fclose(fp); fp = NULL; break; } } //if all 10 log files are full if (9 == iIndex) { memset(szLogFile, 0, LOGINFO_MAX_NUM_LEN); strcat_s(szLogFile, szLogPath); strcat_s(szLogFile, "_9.log"); DeleteFile(szLogFile); iIndex--; } for (ind = iIndex; ind >= 0; ind--) { memset(szTmp, 0, LOGINFO_MAX_NUM_LEN); memset(szLogFile, 0, LOGINFO_MAX_NUM_LEN); memset(szLogFileRename, 0, LOGINFO_MAX_NUM_LEN); strcat_s(szLogFile, szLogPath); strcat_s(szLogFileRename, szLogPath); sprintf_s(szTmp, "_%d", ind); strcat_s(szLogFile, szTmp); memset(szTmp, 0, LOGINFO_MAX_NUM_LEN); sprintf_s(szTmp, "_%d", ind + 1); strcat_s(szLogFileRename, szTmp); strcat_s(szLogFile, ".log"); strcat_s(szLogFileRename, ".log"); rename(szLogFile, szLogFileRename); } memset(szLogFile, 0, LOGINFO_LOGFILE_MAX_LEN); strcat_s(szLogFile, szLogPath); strcat_s(szLogFile, "_0.log"); m_fp = fopen(szLogFile, "a+"); if (m_fp != NULL) { EnterCriticalSection(&m_csLog); fwrite(szLogInfo, sizeof(char), strlen(szLogInfo), m_fp); fflush(m_fp); LeaveCriticalSection(&m_csLog); } } return 0; }

사용:
 
//[2012-11-19 11:38:16.297][5820][Normal][Method return] QueryWaitCallInfo: lSkillID: 0 Rslt: 0
	CLog log;
	log.WriteLog(0, LOGINFO_METHOD_RETURN_QUERYWAITCALLINFO, 0, 0 );

좋은 웹페이지 즐겨찾기