1、inline模式:
inline int log0str(const char* str,char withdate=-1)
{
#ifndef loglevel0
return 0;
#else
return commlogStr(str,withdate);
#endif
}
inline int log0str2(const char* str,const char* str2,char withdate=-1)
{
#ifndef loglevel0
return 0;
#else
return commlogStr2(str,str2,withdate);
#endif
}
inline int log0int(const char* str,const int d,char withdate=-1)
{
#ifndef loglevel0
return 0;
#else
return commlogInt(str,d,withdate);
#endif
}
inline int log0strs(char withdate=-1,...) //对于变参,这样是通不过编译的,实际实现要麻烦一些,而且用inline似乎不太好:代码太多了
{
#ifndef loglevel0
return 0;
#else
return commlogStrs(withdate,...);
#endif
}
//对于8种日志等级,需要重复8遍--这引出另一个问题:这种重复也可以用“超级宏”实现吗?
2、宏模式:
#if defined(loglevel0)
#define log0str(a) commlogStr(a)
#define log0int(a,b) commlogInt(a,b)
#define log0str2(a,b) commlogStr2(a,b)
#define log0strs(a...) commlogStrs(a...)
#define log0str(a,d) commlogStr(a,d) //想支持缺省参数。但是编译通不过
#define log0int(a,b,d) commlogInt(a,b,d)
#define log0str2(a,b,d) commlogStr2(a,b,d)
#else
#define log0str(a)
#define log0int(a)
#define log0str2(a,b)
#define log0strs(d,a...)
#define log0str(a,d)
#define log0int(a,b,d)
#define log0str2(a,b,d)
#endif
//对于8种日志等级,需要重复8遍--这引出另一个问题:这种重复也可以用“超级宏”实现吗?
3、最实在的办法,老老实实增加一个等级参数:
extern int commlogStr(const int level,const char* str,char withdate=-1);
extern int commlogStr2(const int level,const char* str,const char* str2,char withdate=-1);
extern int commlogStrs(const int level,char withdate=-1,...);
extern int commlogInt(const int level,const char* str,const int d,char withdate=-1);
每个函数实现的前部增加2行:
int commlogStr(const int level,const char* str,char withdate)
{
if ((loglevelflag & level)==0)
return 0;
。。。
}