我想在我的 C++ 应用程序中记录每个函数调用和参数值。下面是我想出的代码:
头文件:
#pragma once
class Deneme1
{
public:
Deneme1(void);
~Deneme1(void);
int Deneme1::foo1(double &a);
int Deneme1::foo2(double &a);
struct Logger
{};
template <typename T>
struct LogReturner
{
T* ptrReturnValue;
Logger& theLog;
LogReturner(Logger& log, T* retVal) : theLog(log), ptrReturnValue(retVal) { }
~LogReturner() { /**/ }
};
#define EXPAND_ARGS(...) __VA_ARGS__;
#define LOG_FUNCTION_CALL(x, logger, varName, ar) x varName; LogReturner<x> LR(logger, &varName); FuncCall(__FUNCTION__, EXPAND_ARGS ar);
Logger globLogger;
};
cp文件:
#include "Deneme1.h"
#include "FuncCall.h"
Deneme1::Deneme1(void)
{
}
Deneme1::~Deneme1(void)
{
}
int Deneme1::foo1(double &a)
{
LOG_FUNCTION_CALL(int, globLogger, returnValue,EXPAND_ARGS());
Deneme1 c;
double d = 5;
c.foo2(d);
return returnValue;
}
int Deneme1::foo2(double &a)
{
LOG_FUNCTION_CALL(int, globLogger, returnValue);
return returnValue;
}
.h 文件负责处理日志功能:
#pragma once
#include <stdio.h>
#include <cstdarg>
#include <iostream>
class FuncCall
{
public:
FuncCall(const char * lpszFuncName, ...) {
printf(lpszFuncName);printf("\n");
va_list arguments;
double sum = 0;
int num = 1;
va_start ( arguments, num );
for ( int x = 0; x < num; x++ )
std::cout << va_arg ( arguments, double ) << std::endl;
va_end ( arguments );
}
~FuncCall() { printf("func return ");printf("\n"); }
};
但是编译器抱怨在“LOG_FUNCTION_CALL”中找不到“EXPAND_ARGS”。其实我不知道如何正确调用“LOG_FUNCTION_CALL”。
谢谢。