//调用bool函数假如返回false,我们经常需要打印日志,然后再有可能需要返回:
if(!BoolFunc()){
printf("...");
return false;
}
//本人比较懒,希望一步搞定:
DebugBoolFuncErrorReturn(BoolFunc());
// 以下是源代码
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
bool DebugBoolFlag(const char *log1, const char *log2 = NULL);
bool DebugBoolFlag(const char *log1, const char *log2){
printf("debug: %s make a error\n", log1);
if(log2)
printf("\t%s\n", log2);
return false;
}
#define TOSTRING_HELPER(s) #s
#define TOSTRING(s) TOSTRING_HELPER(s)
# define LOCATION ":" __FILE__ ":" TOSTRING(__LINE__) " "
#define DebugBoolFuncNoReturnWithCustomLog(func, log2) func ? true : DebugBoolFlag(#func LOCATION, log2)
#define DebugBoolFuncNoReturn(func) func ? true : DebugBoolFlag(#func LOCATION)
#define DebugBoolFuncErrorReturnWithCustomLog(func, log2) {\
if(!func){\
DebugBoolFlag(#func LOCATION, log2); \
return false;\
}\
}
#define DebugBoolFuncErrorReturn(func) {\
if(!func){\
DebugBoolFlag(#func LOCATION); \
return false;\
}\
}
bool abc(bool result){
printf("calling abc\n");
return result;
}
int main()
{
// 不return,发生错误的时候打印日志,并附加自定义的log
bool result = DebugBoolFuncNoReturnWithCustomLog( abc(0), "bool custom log!" );
printf("%d\n", result);
// 不return,发生错误的时候打印日志
result = DebugBoolFuncNoReturn( abc(0) );
printf("%d\n", result);
// 当发生错误的时候return,发生错误的时候打印日志,并附加自定义的log
DebugBoolFuncErrorReturnWithCustomLog(abc(0), "bool custom log!");
// 当发生错误的时候return,发生错误的时候打印日志
DebugBoolFuncErrorReturn(abc(0));
printf("END\n");
return 0;
}
调用bool函数的时候如果函数return false。自动打印日志并return false,有兴趣可以拿来玩玩用
最新推荐文章于 2022-04-17 16:02:34 发布