123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- #pragma once
- // 下列 ifdef 块是创建使从 DLL 导出更简单的
- // 宏的标准方法。此 DLL 中的所有文件都是用命令行上定义的 LOGGER_EXPORTS
- // 符号编译的。在使用此 DLL 的
- // 任何其他项目上不应定义此符号。这样,源文件中包含此文件的任何其他项目都会将
- // LOGGER_API 函数视为是从 DLL 导入的,而此 DLL 则将用此宏定义的
- // 符号视为是被导出的。
- #ifndef SHAREMEMORY_LOGGER_EXPORTS
- #ifdef _WIN64
- #ifdef _DEBUG
- #pragma comment(lib, "ShareMemory_LoggerX64D.lib")
- #else
- #pragma comment(lib, "ShareMemory_LoggerX64.lib")
- #endif
- #else
- #ifdef _DEBUG
- #pragma comment(lib, "ShareMemory_LoggerD.lib")
- #else
- #pragma comment(lib, "ShareMemory_Logger.lib")
- #endif
- #endif
- #endif
- #include <string>
- //#include <sstream>
- #include "ShareMemory_Lock.h"
- using namespace std;
- #ifdef SHAREMEMORY_LOGGER_EXPORTS
- #define SHAREMEMORY_LOGGER_API __declspec(dllexport)
- #define SHAREMEMORY_LOGGER_C_API extern "C" __declspec(dllexport)
- #else
- #define SHAREMEMORY_LOGGER_API __declspec(dllimport)
- #define SHAREMEMORY_LOGGER_C_API extern "C" __declspec(dllimport)
- #endif
- #define lOG_LEVEL 1 //1:TRACE,2:DEBUG,3:INFO,4:WARN,5:ERROR,6:FATAL
- #define MAXFILESIZE 12 //Mbs
- #define MAXBACKUPCOUNT 15 //backup count
- #define MAXTIMEPERIOD 1 //time period:day
- #define FILENAME 0 //print filename. 0:No,1:Yes
- #define FUNCTIONNAME 1 //print functionname. 0:No,1:Yes
- #define LINE 1 //print line number. 0:No,1:Yes
- #define DATE 1 //print date. 0:No,1:Yes
- #define PROC 0 //print process Id. 0:No,1:Yes
- #define THREAD 1 //print Thread Id. 0:No,1:Yes
- #define CACHE 1 //cache. 0:No,1:Yes
- typedef enum _SHAREMEMORY_Log_Level {
- SM_LOG_LEVEL_INIT,
- SM_LOG_LEVEL_TRACE,
- SM_LOG_LEVEL_DEBUG,
- SM_LOG_LEVEL_INFO,
- SM_LOG_LEVEL_WARNING,
- SM_LOG_LEVEL_ERROR,
- SM_LOG_LEVEL_FATAL,
- SM_LOG_LEVEL_MAX
- }SHAREMEMORY_LOG_LEVEL;
- class SHAREMEMORY_LOGGER_API ShareMemory_Logger_Pattern {
- public:
- ShareMemory_Logger_Pattern(void);
- virtual ~ShareMemory_Logger_Pattern(void);
- bool SetLevel(LONG Level);
- SHAREMEMORY_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 SHAREMEMORY_LOGGER_API ShareMemory_Logger : public CShareMemory_Lock
- {
- DWORD m_FileId;
- /*file name using NAME_YYYYMMDDHHMMSS.LOG*/
- string *m_pFullLogFileName;
- ShareMemory_Logger_Pattern *m_pPattern;
- public:
- ShareMemory_Logger(ShareMemory_Logger &obj);
- ShareMemory_Logger(void);
- virtual ~ShareMemory_Logger(void);
- ShareMemory_Logger& operator = (const ShareMemory_Logger &tValue);
- void SetLogPattern(ShareMemory_Logger_Pattern& Pattern);
- void GetLogPattern(ShareMemory_Logger_Pattern& Pattern);
- void SetLogFilepath(const char *pPath);
- bool ChangeLoggerFileTitle(ShareMemory_Logger* p, const char *pTitle);
- bool IsLogFilePathExist();
- DWORD GetLogFileId();
- int GetLogLevel();
- };
- SHAREMEMORY_LOGGER_C_API void ExitLoggerModule();
- SHAREMEMORY_LOGGER_C_API ShareMemory_Logger* ShareMemCreateLogger();
- SHAREMEMORY_LOGGER_C_API void ShareMemReleseLogger(ShareMemory_Logger* p);//非自己创建(CreateLogger获取)的Logger,不要Relase!!!!
- SHAREMEMORY_LOGGER_C_API void __ShareMemory_DebugPrintA(ShareMemory_Logger* &plog,
- int Level,
- const char* file,
- int line,
- const char* function,
- const char* fmt,
- ...);
- #define SM_PRINTA_LOG_INFO(log,format,...) \
- {\
- if (log)\
- {\
- __ShareMemory_DebugPrintA(log, SM_LOG_LEVEL_INFO, __FILE__, __LINE__, __FUNCTION__, format, __VA_ARGS__); \
- }\
- }
- #define SM_PRINTA_LOG_WARN(log,format,...) \
- {\
- if (log)\
- {\
- __ShareMemory_DebugPrintA(log, SM_LOG_LEVEL_WARNING, __FILE__, __LINE__, __FUNCTION__, format, __VA_ARGS__); \
- }\
- }
- #define SM_PRINTA_LOG_ERR(log,format,...) \
- {\
- if (log)\
- {\
- __ShareMemory_DebugPrintA(log, SM_LOG_LEVEL_ERROR, __FILE__, __LINE__, __FUNCTION__, format, __VA_ARGS__); \
- }\
- }
- //#define mLog::FINFO(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__)
|