#pragma once // 下列 ifdef 块是创建使从 DLL 导出更简单的 // 宏的标准方法。此 DLL 中的所有文件都是用命令行上定义的 LOGGER_EXPORTS // 符号编译的。在使用此 DLL 的 // 任何其他项目上不应定义此符号。这样,源文件中包含此文件的任何其他项目都会将 // LOGGER_API 函数视为是从 DLL 导入的,而此 DLL 则将用此宏定义的 // 符号视为是被导出的。 #ifndef LOGGER_EXPORTS #ifdef _WIN64 #ifdef _DEBUG #pragma comment(lib, "CcosLoggerX64D.lib") #else #pragma comment(lib, "CcosLoggerX64.lib") #endif #else #ifdef _DEBUG #pragma comment(lib, "CcosLoggerD.lib") #else #pragma comment(lib, "CcosLogger.lib") #endif #endif #endif #include #include #include "CcosLock.h" #include "ResDataObject.h" using namespace std; #ifdef LOGGER_EXPORTS #define LOGGER_API __declspec(dllexport) #define LOGGER_C_API extern "C" __declspec(dllexport) #else #define LOGGER_API __declspec(dllimport) #define LOGGER_C_API extern "C" __declspec(dllimport) #endif typedef enum _Log_Level { LOG_LEVEL_INIT, LOG_LEVEL_TRACE, LOG_LEVEL_DEBUG, LOG_LEVEL_INFO, LOG_LEVEL_WARNING, LOG_LEVEL_ERROR, LOG_LEVEL_FATAL, LOG_LEVEL_MAX }LOG_LEVEL; class LOGGER_API Logger_Pattern { public: Logger_Pattern(void); virtual ~Logger_Pattern(void); bool SetLevel(LONG Level); LOG_LEVEL m_Level;//log level,def:WARNING size_t MaxFileSize;//log file size limit.def:10M size_t MaxBackupCount;//log file count limit.def: size_t MaxTimePeriod;//by day //debug info bool PrintFileName; bool PrintFunctionName; bool PrintLine; //date bool PrintDate; //time //bool PrintTime; //Proc bool PrintProcId; //Thread bool PrintThreadId; //cachable bool Cachable; //the whole pattern like below line /* //full log line [ date ] [ time ] [LEVEL] [ PID ][ TID ] [ FileName Line Func ] [2016:11:07] [14:03:35:0231] [DEBUG] [PID:0023][TID:00137] [FileName (Line) (Function)] : context........... //minimum log line [14:03:35:0231] : context...... */ }; class LOGGER_API Logger : public CcosLock { DWORD m_FileId; /*file name using NAME_YYYYMMDDHHMMSS.LOG*/ string *m_pFullLogFileName; string *m_pErrFullLogFileName; Logger_Pattern *m_pPattern; public: Logger(Logger &obj); Logger(void); virtual ~Logger(void); Logger& operator = (const Logger &tValue); void SetLogPattern(Logger_Pattern& Pattern); void GetLogPattern(Logger_Pattern& Pattern); void SetLogFilepath(const char *pPath); bool ChangeLoggerFileTitle(Logger* p, const char *pTitle); bool IsLogFilePathExist(); const char* GetErrLogFilePath(); DWORD GetLogFileId(); int GetLogLevel(); }; LOGGER_C_API Logger* GetGlobalLogger(); LOGGER_C_API Logger* GetBusLogger(); LOGGER_C_API Logger* GetThreadLogger(); LOGGER_C_API void ExitLoggerModule(); LOGGER_C_API Logger* CreateLogger(); LOGGER_C_API void ReleseLogger(Logger* p);//非自己创建(CreateLogger获取)的Logger,不要Relase!!!! LOGGER_C_API void PrintA_IOLOG(Logger*& plog, int Level, const char* pszContext, size_t ContextSize); LOGGER_C_API void __DebugPrintA(Logger* &plog, int Level, const char* file, int line, const char* function, const char* fmt, ...); LOGGER_C_API void __G_DebugPrintA( int Level, const char* file, int line, const char* function, const char* fmt, ...); LOGGER_C_API void __T_DebugPrintA( int Level, const char* file, int line, const char* function, const char* fmt, ...); LOGGER_C_API void __Res_DebugPrintA( Logger* &plog, ResDataObject &obj, int Level, const char* file, int line, const char* function, const char* fmt, ...); LOGGER_C_API void __Res_T_DebugPrintA( ResDataObject &obj, int Level, const char* file, int line, const char* function, const char* fmt, ...); #define PRINTA_LOG(log,Level,format,...) \ {\ if (log)\ {\ int level = log->GetLogLevel(); \ if (level <= Level)\ {\ __DebugPrintA(log, LOG_LEVEL_INFO, __FILE__, __LINE__, __FUNCTION__, format, __VA_ARGS__); \ }\ }\ } //#define PRINTA_TRACE(log,format,...) PRINTA_LOG(log,LOG_LEVEL_TRACE,format,__VA_ARGS__) //#define PRINTA_DEBUG(log,format,...) PRINTA_LOG(log,LOG_LEVEL_DEBUG,format,__VA_ARGS__) //#define PRINTA_INFO(log,format,...) PRINTA_LOG(log,LOG_LEVEL_INFO,format,__VA_ARGS__) //#define PRINTA_WARN(log,format,...) PRINTA_LOG(log,LOG_LEVEL_WARNING,format,__VA_ARGS__) //#define PRINTA_ERROR(log,format,...) PRINTA_LOG(log,LOG_LEVEL_ERROR,format,__VA_ARGS__) //#define PRINTA_FATAL(log,format,...) PRINTA_LOG(log,LOG_LEVEL_FATAL,format,__VA_ARGS__) #define PRINTA_TRACE(log,format,...) __DebugPrintA(log,LOG_LEVEL_TRACE,__FILE__,__LINE__,__FUNCTION__,format,__VA_ARGS__) #define PRINTA_DEBUG(log,format,...) __DebugPrintA(log,LOG_LEVEL_DEBUG,__FILE__,__LINE__,__FUNCTION__,format,__VA_ARGS__) #define PRINTA_INFO(log,format,...) __DebugPrintA(log,LOG_LEVEL_INFO,__FILE__,__LINE__,__FUNCTION__,format,__VA_ARGS__) #define PRINTA_WARN(log,format,...) __DebugPrintA(log,LOG_LEVEL_WARNING,__FILE__,__LINE__,__FUNCTION__,format,__VA_ARGS__) #define PRINTA_ERROR(log,format,...) __DebugPrintA(log,LOG_LEVEL_ERROR,__FILE__,__LINE__,__FUNCTION__,format,__VA_ARGS__) #define PRINTA_FATAL(log,format,...) __DebugPrintA(log,LOG_LEVEL_FATAL,__FILE__,__LINE__,__FUNCTION__,format,__VA_ARGS__) //#define GPRINTA_TRACE(format,...) PRINTA_LOG(GetGlobalLogger(),LOG_LEVEL_TRACE,format,__VA_ARGS__) //#define GPRINTA_DEBUG(format,...) PRINTA_LOG(GetGlobalLogger(),LOG_LEVEL_DEBUG,format,__VA_ARGS__) //#define GPRINTA_INFO(format,...) PRINTA_LOG(GetGlobalLogger(),LOG_LEVEL_INFO,format,__VA_ARGS__) //#define GPRINTA_WARN(format,...) PRINTA_LOG(GetGlobalLogger(),LOG_LEVEL_WARNING,format,__VA_ARGS__) //#define GPRINTA_ERROR(format,...) PRINTA_LOG(GetGlobalLogger(),LOG_LEVEL_ERROR,format,__VA_ARGS__) //#define GPRINTA_FATAL(format,...) PRINTA_LOG(GetGlobalLogger(),LOG_LEVEL_FATAL,format,__VA_ARGS__) #define GPRINTA_TRACE(format,...) __G_DebugPrintA(LOG_LEVEL_TRACE,__FILE__,__LINE__,__FUNCTION__,format,__VA_ARGS__) #define GPRINTA_DEBUG(format,...) __G_DebugPrintA(LOG_LEVEL_DEBUG,__FILE__,__LINE__,__FUNCTION__,format,__VA_ARGS__) #define GPRINTA_INFO(format,...) __G_DebugPrintA(LOG_LEVEL_INFO,__FILE__,__LINE__,__FUNCTION__,format,__VA_ARGS__) #define GPRINTA_WARN(format,...) __G_DebugPrintA(LOG_LEVEL_WARNING,__FILE__,__LINE__,__FUNCTION__,format,__VA_ARGS__) #define GPRINTA_ERROR(format,...) __G_DebugPrintA(LOG_LEVEL_ERROR,__FILE__,__LINE__,__FUNCTION__,format,__VA_ARGS__) #define GPRINTA_FATAL(format,...) __G_DebugPrintA(LOG_LEVEL_FATAL,__FILE__,__LINE__,__FUNCTION__,format,__VA_ARGS__) //#define TPRINTA_TRACE(format,...) PRINTA_LOG(GetThreadLogger(),LOG_LEVEL_TRACE,format,__VA_ARGS__) //#define TPRINTA_DEBUG(format,...) PRINTA_LOG(GetThreadLogger(),LOG_LEVEL_DEBUG,format,__VA_ARGS__) //#define TPRINTA_INFO(format,...) PRINTA_LOG(GetThreadLogger(),LOG_LEVEL_INFO,format,__VA_ARGS__) //#define TPRINTA_WARN(format,...) PRINTA_LOG(GetThreadLogger(),LOG_LEVEL_WARNING,format,__VA_ARGS__) //#define TPRINTA_ERROR(format,...) PRINTA_LOG(GetThreadLogger(),LOG_LEVEL_ERROR,format,__VA_ARGS__) //#define TPRINTA_FATAL(format,...) PRINTA_LOG(GetThreadLogger(),LOG_LEVEL_FATAL,format,__VA_ARGS__) #define TPRINTA_TRACE(format,...) __T_DebugPrintA(LOG_LEVEL_TRACE,__FILE__,__LINE__,__FUNCTION__,format,__VA_ARGS__) #define TPRINTA_DEBUG(format,...) __T_DebugPrintA(LOG_LEVEL_DEBUG,__FILE__,__LINE__,__FUNCTION__,format,__VA_ARGS__) #define TPRINTA_INFO(format,...) __T_DebugPrintA(LOG_LEVEL_INFO,__FILE__,__LINE__,__FUNCTION__,format,__VA_ARGS__) #define TPRINTA_WARN(format,...) __T_DebugPrintA(LOG_LEVEL_WARNING,__FILE__,__LINE__,__FUNCTION__,format,__VA_ARGS__) #define TPRINTA_ERROR(format,...) __T_DebugPrintA(LOG_LEVEL_ERROR,__FILE__,__LINE__,__FUNCTION__,format,__VA_ARGS__) #define TPRINTA_FATAL(format,...) __T_DebugPrintA(LOG_LEVEL_FATAL,__FILE__,__LINE__,__FUNCTION__,format,__VA_ARGS__) #define RES_TPRINTA_TRACE(Res,format,...) __Res_T_DebugPrintA(Res,LOG_LEVEL_TRACE,__FILE__,__LINE__,__FUNCTION__,format,__VA_ARGS__) #define RES_TPRINTA_DEBUG(Res,format,...) __Res_T_DebugPrintA(Res,LOG_LEVEL_DEBUG,__FILE__,__LINE__,__FUNCTION__,format,__VA_ARGS__) #define RES_TPRINTA_INFO(Res,format,...) __Res_T_DebugPrintA(Res,LOG_LEVEL_INFO,__FILE__,__LINE__,__FUNCTION__,format,__VA_ARGS__) #define RES_TPRINTA_WARN(Res,format,...) __Res_T_DebugPrintA(Res,LOG_LEVEL_WARNING,__FILE__,__LINE__,__FUNCTION__,format,__VA_ARGS__) #define RES_TPRINTA_ERROR(Res,format,...) __Res_T_DebugPrintA(Res,LOG_LEVEL_ERROR,__FILE__,__LINE__,__FUNCTION__,format,__VA_ARGS__) #define RES_TPRINTA_FATAL(Res,format,...) __Res_T_DebugPrintA(Res,LOG_LEVEL_FATAL,__FILE__,__LINE__,__FUNCTION__,format,__VA_ARGS__) #define RES_PRINTA_TRACE(Log,Res,format,...) __Res_DebugPrintA(Log,Res,LOG_LEVEL_TRACE,__FILE__,__LINE__,__FUNCTION__,format,__VA_ARGS__) #define RES_PRINTA_DEBUG(Log,Res,format,...) __Res_DebugPrintA(Log,Res,LOG_LEVEL_DEBUG,__FILE__,__LINE__,__FUNCTION__,format,__VA_ARGS__) #define RES_PRINTA_INFO(Log,Res,format,...) __Res_DebugPrintA(Log,Res,LOG_LEVEL_INFO,__FILE__,__LINE__,__FUNCTION__,format,__VA_ARGS__) #define RES_PRINTA_WARN(Log,Res,format,...) __Res_DebugPrintA(Log,Res,LOG_LEVEL_WARNING,__FILE__,__LINE__,__FUNCTION__,format,__VA_ARGS__) #define RES_PRINTA_ERROR(Log,Res,format,...) __Res_DebugPrintA(Log,Res,LOG_LEVEL_ERROR,__FILE__,__LINE__,__FUNCTION__,format,__VA_ARGS__) #define RES_PRINTA_FATAL(Log,Res,format,...) __Res_DebugPrintA(Log,Res,LOG_LEVEL_FATAL,__FILE__,__LINE__,__FUNCTION__,format,__VA_ARGS__)