src/log.h
#ifndef   _MACRO_LogModule
#define   _MACRO_LogModule
#include <stdio.h>
#include <pthread.h>
#include "macro_define.h"

/* ÿ̵߳buffer size*/
#define   _LOG_BUFFSIZE  1024*1024*4
/* ǰ̵ Stream IO buffer size*/
#define   _SYS_BUFFSIZE  1024*1024*8
/* log ļַ·󳤶*/
#define	  _LOG_PATH_LEN  250
/* ־Ӧģ*/
#define   _LOG_MODULE_LEN 32

typedef  enum LogLevel {  
	LL_DEBUG = 1,
	LL_TRACE = 2,
	LL_NOTICE = 3, 
	LL_WARNING = 4, 
	LL_ERROR = 5,
}LogLevel;

/**
*	Log_Writer  ־
*/
class Log_Writer
{
	public:
		Log_Writer()
		{
			m_system_level = LL_NOTICE;
			//fp = stderr;
			fp = NULL;
			m_issync = false;
			m_isappend = true;
			m_filelocation[0] ='\0'; 
			pthread_mutex_init(&m_mutex, NULL);
		}
		~Log_Writer(){
			logclose();
		}
		bool loginit(LogLevel l, const  char *filelocation, bool append = true, bool issync = false);
		bool log(LogLevel l,char *logformat,...);
		LogLevel get_level();
		bool logclose();
	private:
		const char* logLevelToString(LogLevel l);
		bool checklevel(LogLevel l);
		int premakestr(char* m_buffer, LogLevel l);
		bool _write(char *_pbuffer, int len);
	private:
		enum LogLevel m_system_level;
		FILE* fp;
		bool m_issync;
		bool m_isappend;
		char m_filelocation[_LOG_PATH_LEN];
		pthread_mutex_t m_mutex;
		static __thread char m_buffer[_LOG_BUFFSIZE];
		//The __thread specifier may be applied to any global, file-scoped static, function-scoped static, 
		//or static data member of a class. It may not be applied to block-scoped automatic or non-static data member
		//in the log  scence,It's safe!!!!
		//һԱ֮˳õ__threadԴleak,ͬʱҲõĶLog_Writerţ
		//Ϊһ߳ͬһʱֻһLog_Writerڸɻ֮m_bufferreset
		//Լһ߳ûLog_Write(Ϊһ߳ڵֻ̬д) Ҳ̰߳ȫģ
};

extern Log_Writer WARN_W;
extern Log_Writer INFO_W;

/**
 * LogLevel ־
 * p_modulename ģ mysql
 * p_logdir  ־Ŀ¼
 * */
bool log_init(LogLevel l, const char* p_modulename, const char* p_logdir);
#endif
