You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
2.3 KiB
C++

#ifndef __sys_logger_h_
#define __sys_logger_h_
#include <stdio.h>
#include <iostream>
#include <cstring>
#include <stdarg.h>
#include <time.h>
#include <mutex>
//#include <unistd.h>
//#include <pthread.h>
//#include <sys/time.h>
//#include <sys/syscall.h>
//#define gettid() GetCurrentThreadId()
#define gettid() 0
#define LEVEL_SOCKET_DEBUG 0
#define LEVEL_DEBUG 1
#define LEVEL_INFO 2
#define LEVEL_WARNING 3
#define LEVEL_ERROR 4
#define LOG_BUF_SIZE 2048
#define MAX_FILENAME_LEN 256
#define LOG_INFO_NOLOCK(format, ...) \
SysLogger::GetInstance()->WriteLogNoLock(LEVEL_INFO, \
__FILE__,__FUNCTION__, __LINE__, gettid(), \
format, ##__VA_ARGS__)
#define SOCKET_DEBUG(format, ...) \
SysLogger::GetInstance()->WriteLog(LEVEL_SOCKET_DEBUG, \
__FILE__,__FUNCTION__, __LINE__, gettid(), \
format, ##__VA_ARGS__)
#define LOG_DEBUG(format, ...) \
SysLogger::GetInstance()->WriteLog(LEVEL_DEBUG, \
__FILE__,__FUNCTION__, __LINE__, gettid(), \
format, ##__VA_ARGS__)
#define LOG_INFO(format, ...) \
SysLogger::GetInstance()->WriteLog(LEVEL_INFO, \
__FILE__,__FUNCTION__, __LINE__, gettid(), \
format, ##__VA_ARGS__)
#define LOG_WARNING(format, ...) \
SysLogger::GetInstance()->WriteLog(LEVEL_WARNING, \
__FILE__,__FUNCTION__, __LINE__, gettid(), \
format, ##__VA_ARGS__)
#define LOG_ERROR(format, ...) \
SysLogger::GetInstance()->WriteLog(LEVEL_ERROR, \
__FILE__,__FUNCTION__, __LINE__, gettid(), \
format, ##__VA_ARGS__)
class SysLogger
{
public:
~SysLogger();
static SysLogger* GetInstance();
static void DestroyInstance();
bool InitLogger(const char* file_name, int min_level);
void WriteLog(int level, const char* exec_file, const char*func, int exec_line, int tid, const char* format, ...);
void WriteLogNoLock(int level, const char* exec_file, const char*func, int exec_line, int tid, const char* format, ...);
private:
void set_log(int level, const char* exec_file, const char*func, int exec_line, int tid, const char* format, va_list valst);
public:
static SysLogger* instance_;
private:
SysLogger();
SysLogger(const SysLogger&){};//No copy
int m_minLevel;
char* m_logFileName;
char* m_logBuf;
FILE* m_fp;
bool m_isStarted;
std::mutex m_mutex;
};
#endif