-
#include <iostream>
-
#include <cstdarg>
-
#include <cstdio>
-
-
using namespace std;
-
-
-
int ImplLogPrint(int iLogLevel, const char *format, va_list argList);
-
-
#define ERROR 0
-
#define WARNING 1
-
#define INFO 2
-
#define DEBUG 3
-
-
#define _IMPL_LOGPRINT(LogLevel) \
-
void LogPrint##LogLevel(const char *format, ...) \
-
{ \
-
va_list argList; \
-
va_start(argList, format); \
-
ImplLogPrint(LogLevel, format, argList); \
-
va_end(argList);\
-
}
-
-
#define IMPL_LOGPRINT(x) _IMPL_LOGPRINT(x) //跳板宏,用于展开ERROR 等宏
-
-
IMPL_LOGPRINT(ERROR) //展开之后为LogPrint0
-
IMPL_LOGPRINT(WARNING)
-
IMPL_LOGPRINT(INFO)
-
IMPL_LOGPRINT(DEBUG)
-
-
#define Logging(LogLevel) (LogLevel <= GetLogLevel())
-
-
#define _LogEvent(LogLevel, x) \
-
do { \
-
if (Logging(LogLevel)) \
-
{ \
-
LogPrint##LogLevel x ; \
-
} \
-
} while(0)
-
-
#define LogEvent(LogLevel, x) _LogEvent(LogLevel, x) //跳板宏,用于展开ERROR 等宏
-
-
#define LogError(x) LogEvent(ERROR, x)
-
#define LogWarning(x) LogEvent(WARNING, x)
-
#define LogInfo(x) LogEvent(INFO, x)
-
#define LogDebug(x) LogEvent(DEBUG, x)
-
-
int g_LogLevel = 0;
-
-
int GetLogLevel()
-
{
-
return g_LogLevel;
-
}
-
-
int SetLogLevel(int iLevel)
-
{
-
g_LogLevel = iLevel;
-
-
return 0;
-
}
-
-
int ImplLogPrint(int iLogLevel, const char *format, va_list argList)
-
{
-
char buf[1024];
-
-
int ret = vsprintf(buf, format, argList);
-
-
switch(iLogLevel)
-
{
-
case ERROR:
-
cout << "Error:"<< buf << endl;
-
break;
-
case WARNING:
-
cout << "Warning:"<< buf << endl;
-
break;
-
case INFO:
-
cout << "Info:"<< buf << endl;
-
break;
-
case DEBUG:
-
cout << "Debug:"<< buf << endl;
-
break;
-
default:
-
break;
-
}
-
-
return ret;
-
}
-
-
int main()
-
{
-
SetLogLevel(3);
-
LogError(("hello %s", "world"));
-
LogWarning(("hello %s", "world"));
-
LogInfo(("hello %s", "world"));
-
LogDebug(("hello %s", "world"));
-
-
SetLogLevel(1);
-
LogError(("hello %s", "world"));
-
LogWarning(("hello %s", "world"));
-
LogInfo(("hello %s", "world"));
-
LogDebug(("hello %s", "world"));
-
-
return 0;
-
}
预编译展开后的代码
# g++ -E debug_level.cpp -o debug_level.i
阅读(1259) | 评论(0) | 转发(0) |