Lfs Common --> Log

#ifndef LOG_H_
#define  LOG_H_
#define MODULE	   0
#define	LOG_FATAL(fmt,...) 		lfs::common::Log::Out(MODULE, 0, __FILE__, __FUNCTION__, __LINE__, fmt, ##__VA_ARGS__)
#define	LOG_ERROR(fmt,...) 		lfs::common::Log::Out(MODULE, 1, __FILE__, __FUNCTION__, __LINE__, fmt, ##__VA_ARGS__)
#define	LOG_WARNING(fmt,...)	lfs::common::Log::Out(MODULE, 2, __FILE__, __FUNCTION__, __LINE__, fmt, ##__VA_ARGS__)
#define	LOG_DEBUG(fmt,...) 		lfs::common::Log::Out(MODULE, 3, __FILE__, __FUNCTION__, __LINE__, fmt, ##__VA_ARGS__)
#define	LOG_INFO(fmt,...) 		lfs::common::Log::Out(MODULE, 4, __FILE__, __FUNCTION__, __LINE__, fmt, ##__VA_ARGS__)
namespace lfs{
namespace common{
class Log
{
public:
	static void Out(const int moduleIndex, const int lvl, const char * fileName, const char * funcName, int line, const char * fmt, ...);
protected:
private:
}; 
}/*  Namespace common */
}/*  Namespace lfs */
#endif /* LOG_H */




#include "Log.h"

#include <stdarg.h>
#include <stdio.h>
#include <errno.h>
#include <time.h>
namespace lfs{
namespace common{
void Log::Out(const int moduleIndex, const int lvl, const char * fileName, const char * funcName, int line, const char * fmt, ...)
{
	static const char * moduleName[]={
		"LFS",
	};
	static const char * levelName[] = {
		"FATAL",
		"ERROR",
		"WARNING",
		"Debug",
		"INFO"
	};
	time_t now;                                                      
	char dbgtime[26] ;                                                  
	time(&now);                                                         
	ctime_r(&now, dbgtime);                                             
	dbgtime[24] = '\0';       
	// with time
	// printf("[%s] [%s][%s][%s:%d]", moduleName[moduleIndex], levelName[lvl], dbgtime, fileName, line);
	// no time - no funcName
	// printf("[%s] [%s][%s:%d]", moduleName[moduleIndex], levelName[lvl], fileName, line);
	// no time with funcName 
	printf("[%s] [%s][%s:%s:%d]", moduleName[moduleIndex], levelName[lvl], fileName, funcName, line);
	va_list pArg;
	va_start(pArg, fmt);
	vprintf(fmt, pArg);
	va_end(pArg);
	printf("\n");
}
}/*  Namespace common */
}/*  Namespace lfs */


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章