中国开发网: 论坛: 程序员情感CBD: 贴子 68421
haitao
本例的3种实现方法,哪个最好?
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;
。。。
}
我的blog:http://szhaitao.blog.hexun.com & http://www.hoolee.com/user/haitao
--以上均为泛泛之谈--
不尽牛人滚滚来,无边硬伤纷纷现 人在江湖(出来的),哪能不挨刀(总归是要的)
网络对话,歧义纷生;你以为明白了对方的话,其实呢?

您所在的IP暂时不能使用低版本的QQ,请到:http://im.qq.com/下载安装最新版的QQ,感谢您对QQ的支持和使用

相关信息:


欢迎光临本社区,您还没有登录,不能发贴子。请在 这里登录