我正在为用C编写的自动化系统开发一些模块,我需要使用硬件执行大量工作.我没有看到调试事物而不是跟踪日志的简单方法(如传统方法).所以我正在寻找一个记录函数调用的好习惯.至少调用和返回值的顺序.
它在应用程序中执行的方式非常简单,并且实际上使用不相关的构造来污染代码
int function (int param){
if(trace_level & LOG_FCALLS){
writelog("Entering function()");
}
/* something useful */
if(trace_level & LOG_FCALLS){
writelog("Exit from function()=%d", ret);
}
}
Run Code Online (Sandbox Code Playgroud)
我决定使用一个可以完成所有脏工作的宏.现在看起来像这样
#define LOG_E(fn) const char *__fname=fn; printf("LOG: Entry to %s\n",__fname)
#define return(ret) printf("LOG: Exit from %s()=%d\n",__fname,ret)
int testFunc(){
LOG_E("testFunc");
/*do useful things */
return(ret);
}
Run Code Online (Sandbox Code Playgroud)
我看到了这段代码的问题
我正在覆盖return语句,它需要一直写return(ret)而不是return ret.很容易忘记这个问题.
我在我的宏中定义字符串变量.我知道__func__宏存在于C99中,但不幸的是,我的编译器不支持这个宏或任何其他相关的宏.
如何记录函数参数的值?
我很确定这不是一个新问题,我不是第一个面对它的人.我也知道AOP的事情,但代码检测是我的系统不可接受的解决方案,我没有发现任何可能与我的编译器.
所以我正在寻找一个好主意如何以最优雅的方式实现跟踪.
我的环境:传统代码,C,Watcom 10.x,实时操作系统
我正在将Google Test / Mock评估为C代码单元测试的框架。
如何为我要模拟的函数指定输出字符串参数?
这里有int get_int_param(const char *)要测试的函数,它使用int _get_text_file_content(const char *fn, char *content)了我想模拟的函数。
如何指定char *content将要执行模拟功能的结果?
我在这段代码中苦苦挣扎:
TEST(GetParameterTest,Positiv){
const static int strLen=29;
char *text=(char *)calloc(strLen,1);
strcpy(text, "param1=1\nparam2=42\nparam3=3");
MokedFunctions mokedFunctions;
EXPECT_CALL(mokedFunctions, _get_text_file_content("process.conf",_)).Times(AtLeast(1)).WillOnce(SetArgReferee<1>(text));
EXPECT_EQ(1, get_int_param("param1"));
}
Run Code Online (Sandbox Code Playgroud)
并得到此编译错误:
/usr/include/gmock/gmock-more-actions.h: In instantiation of ‘typename
testing::internal::Function<F>::Result testing::SetArgRefereeActionP<k,
value_type>::gmock_Impl<F>::gmock_PerformImpl(const args_type&,
arg0_type, arg1_type, arg2_type, arg3_type, arg4_type, arg5_type,
arg6_type, arg7_type, arg8_type, arg9_type) const [with arg0_type =
const char*; arg1_type = char*; arg2_type =
testing::internal::ExcessiveArg; arg3_type =
testing::internal::ExcessiveArg; arg4_type =
testing::internal::ExcessiveArg; arg5_type = …Run Code Online (Sandbox Code Playgroud)