⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 log.h

📁 概述:数据的纵向收集
💻 H
字号:
//sunwangme@hotmail.com

#ifndef _Log_h_
#define _Log_h_

//把<>都加入到stdafx.h或者precomp.h里面
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>

enum LogFileType{
	LOG_FILE_SYSTEM,
	LOG_FILE_INNER,
	LOG_FILE_OUTER,
	LOG_FILE_DB,
	LOG_FILE_AUDIT,
	LOG_FILE_ERROR,
	LOG_FILE_ITEMLOG,
	MAX_LOG_FILES
};

enum LogType { 
	LOG_NORMAL, //system
	LOG_WARN,	//system
	LOG_DEBUG,	//system
	LOG_INNER,	//inner
	LOG_OUTER,	//outer
	LOG_DB,		//db
	LOG_AUDIT,	//audit
	LOG_ERROR,	//error
	LOG_ITEMLOG	//itemlog
};

static WCHAR *LogCategory[] = { L"system", L"inner", L"outer", L"db", L"audit", L"error" , L"itemlog"};

//sunwangme@hotmail.com,最大的长度,Add AddV内部要处理错误,做到强壮
#define MAX_LOG_STRING				4096

struct LogLine 
{
	LogLine() { str[0] = NULL; }
	LogType type;
	WCHAR str[MAX_LOG_STRING];
	int len;
};

class CLog 
{
public:
	static CLog* Instance();
	virtual ~CLog();
	static void Release();

protected:
	CLog();
	CLog(const CLog&);
	CLog& operator=(const CLog&);
	static CLog* m_pInstance;

public:
	void Add(LogType type, char* format, ...);
	void Add(LogType type, WCHAR* format, ...);
	void AddV(LogType type, char* format, va_list va);
	void AddV(LogType type, WCHAR* format, va_list va);
	inline void SetLogFileInfo(WCHAR* pServerName) { wcscpy(m_pServerName,pServerName); };
	inline void SetInstance(HANDLE hInstance) { m_inst = hInstance; };

private:
	// for log file
	int					m_nYear;
	int					m_nMonth;
	int					m_nDay;
	int					m_nHour;
	int					m_nHalf;
	HANDLE				m_hLogFile[MAX_LOG_FILES];
	CRITICAL_SECTION	m_csFileSection[MAX_LOG_FILES];

	// for reopen log file... dirty field
	bool m_bDirty;

	// for critical section
	CRITICAL_SECTION	m_csSection;

	// for log file subdir
	WCHAR m_pServerName[MAX_PATH];
	// instance
	HANDLE m_inst;

private:
	void	Enter() { EnterCriticalSection(&m_csSection); };
	void	Leave() { LeaveCriticalSection(&m_csSection); };

	// for log file
	//2007.1.24,对于客户端的log,采用双log限size制;对于服务器的log,采用定时间多文件制
	inline bool ValidLogFile(int nYear, int nMonth, int nDay, int nHour, int nHalf) { if ( m_bDirty == true) return false; return ( (nHalf!=m_nHalf) || (m_nHour!=nHour) || (m_nYear!=nYear) || (m_nMonth!=nMonth) || (m_nDay!=nDay) ) ? false : true ; };
	
	inline void SetLogFileDate(int nYear, int nMonth, int nDay, int nHour, int nHalf) { m_nYear = nYear; m_nMonth = nMonth; m_nDay = nDay; m_nHour = nHour; m_nHalf = nHalf; };
	void	CreateNewLogFile(int nYear, int nMonth, int nDay, int nHour, int nMin);
	inline void	LogFileLock(int nId) { EnterCriticalSection(&m_csFileSection[nId]); } ;
	inline void	LogFileUnLock(int nId) { LeaveCriticalSection(&m_csFileSection[nId]); } ;
};

//extern CLog Log;

inline void LOG(char* format, ...)
{	
	va_list va;
	va_start(va, format);
	CLog::Instance()->AddV(LOG_NORMAL, format, va);
	va_end(va);
}

inline void LOGE(char* format, ...)
{	
	va_list va;
	va_start(va, format);
	CLog::Instance()->AddV(LOG_ERROR, format, va);
	va_end(va);
}

inline void LOGW(char* format, ...)
{	
	va_list va;
	va_start(va, format);
	CLog::Instance()->AddV(LOG_WARN, format, va);
	va_end(va);
}
#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -