繁体   English   中英

C ++包装带有可变参数的包装器宏?

[英]C++ Wrapping a wrapper macro with variable arguments?

我有一个方法来通过用宏替换它们来包装函数,以便我可以记录调用和返回代码。 这是一个有效的例子:

int rc;
int foo(int a, int b);
int bar(int a, char *b, int *c);

void    LogRet(char *fn, char *file, char *from, int ln, int ret)
{
    printf("%s.%s.%d: %s()  ret:%08x\n", file, from, ln, fn, ret);
}   

#define foo(args, ...)  (rc = (foo)(args, ##__VA_ARGS__), LogRet("foo", __FILE__, __FUNCTION__, __LINE__, rc), rc)

#define bar(args, ...)  (rc = (bar)(args, ##__VA_ARGS__), LogRet("bar", __FILE__, __FUNCTION__, __LINE__, rc), rc)

它替换的函数的宏调用函数并记录函数名称及其调用位置以及返回码。 包装任何函数使用相同的语法,只需要在宏中替换3次的函数名称。 我想要做的是创建包装器宏,其中重新定义foo的宏将类似于:

#define foo(args, ...) WRAPPPER(foo)

我理解stringify和双字符串化的基础知识,但我甚至无法让WRAPPER宏进行真正的函数调用。 理想情况下,我想将其归结为单个WRAP(foo)语句。 原因是,我有大约100个或更多我想要包装的函数,它希望从一个简单的强制包含文件中做到这一点。 我得出的结论是,这是不可能的,但我想在放弃这个想法之前我会问这里。 我正在使用clang和vc ++,如果这有任何区别,但是当我调试很多不同的系统时,在任何编译器上都有这个很好。


适应Jonathan Leffler的回答

由于我是新来的,我不确定这应该是一个单独的答案还是编辑更新。 这基本上是Jonathan Leffler的回答。 这是一个功能性的例子。 虽然它调用的2个函数是没有意义的,但目标是有一个宏可以用任何arg列表包装任何函数。 我的主要用途是在有问题的大型代码库中记录使用流程。 此外,我原来的样本有一个明显的弱点。 通过将返回代码存储在全局中,如果没有在TLS中进行繁琐的准备,则它不是线程安全的。 现在删除了全局,并且宏不再使用序列运算符来返回值,它由日志记录功能保留并返回。 此外,正如Augurar在Jonathan的例子中指出和展示的那样。 如果宏与函数声明在同一文件中声明,则需要括号。

#include <stdio.h>
#include <string.h>

int foo(int a, int b);
int bar(int a, char *b, int *c);


#if defined (_DEBUG)  || defined (DEBUG)
// Short version of __FILE__ without path requires runtime parsing
#define __SFILE__ (strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__)
#ifdef WIN32
  #define WRAPPER(func, ...) LogRet(#func, __SFILE__, __FUNCTION__, __LINE__, (func)(__VA_ARGS__))
#else
  #define WRAPPER(func, ...) LogRet(#func, __SFILE__, __func__, __LINE__, (func)(__VA_ARGS__))
#endif

inline  int LogRet(const char *fn, const char *file, const char *from, int ln, int ret)
{
    printf("%s.%s.%d: %s()  ret:%08x\n", file, from, ln, fn, ret);
    return ret;
}   

#define foo(...) WRAPPER(foo, __VA_ARGS__)
#define bar(...) WRAPPER(bar, __VA_ARGS__)

#endif


int main(void)
{
    int x = foo(1, 2);
    bar(2, "doubled", &x);

    return 0;
}   

#ifdef foo
  #undef foo
  #undef bar
#endif

// If and only if the function definition is in the same file with the macros, you must either undefine the macros or
// parenthesize the function  e.g.   int (foo)(int a, int b) { ... }

int foo(int a, int b)   
{
    printf("%d + %d = %d\n", a, b, a + b);
    return a + b;
}   

int (bar)(int a, char *b, int *c)
{
    printf("%d  %s = %d\n", *c, b, a * *c);
    return *c * a;
}

发布构建输出:

1 + 2 = 3
3  doubled = 6

调试构建输出:

1 + 2 = 3
test.cpp.main.35: foo()  ret:00000003
3  doubled = 6
test.cpp.main.36: bar()  ret:00000006

主要的好处是不必在代码中找到foo()或bar()的每一个出现,以插入调试打印来记录调用和结果或您要插入的任何调试代码。

此代码看起来好像符合您的要求:

#include <stdio.h>

int rc;
int foo(int a, int b);
int bar(int a, char *b, int *c);

extern void LogRet(const char *fn, const char *file, const char *from, int ln, int ret);

void LogRet(const char *fn, const char *file, const char *from, int ln, int ret)
{
    printf("%s.%s.%d: %s()  ret:%08x\n", file, from, ln, fn, ret);
}

#define foo(args, ...)  (rc = (foo)(args, ##__VA_ARGS__), LogRet("foo", __FILE__, __FUNCTION__, __LINE__, rc), rc)

#define bar(args, ...)  (rc = (bar)(args, ##__VA_ARGS__), LogRet("bar", __FILE__, __FUNCTION__, __LINE__, rc), rc)

extern void caller1(void);

void caller1(void)
{
    int d;
    int e = foo(1, 2);
    int f = bar(3, "abc", &d);
    printf("%s(): %d + %d + %d = %d\n", __func__, d, e, f, d + e + f);
}

#undef foo
#undef bar

#define WRAPPER(func, ...) ((rc = (func)(__VA_ARGS__)), LogRet(#func, __FILE__, __func__, __LINE__, rc), rc)

#define foo(...) WRAPPER(foo, __VA_ARGS__)
#define bar(...) WRAPPER(bar, __VA_ARGS__)

extern void caller2(void);

void caller2(void)
{
    int d;
    int e = foo(2, 3);
    int f = bar(3, "abc", &d);
    printf("%s(): %d + %d + %d = %d\n", __func__, d, e, f, d + e + f);
}

int (foo)(int a, int b)
{
    return (a + b) % 3;
}

int (bar)(int a, char *b, int *c)
{
    int d = b[a];
    *c = d;
    return a + d;
}

int main(void)
{
    caller1();
    caller2();
    return 0;
}

示例输出:

wrapper.c.caller1.23: foo()  ret:00000000
wrapper.c.caller1.24: bar()  ret:00000003
caller1(): 0 + 0 + 3 = 3
wrapper.c.caller2.41: foo()  ret:00000002
wrapper.c.caller2.42: bar()  ret:00000003
caller2(): 0 + 2 + 3 = 5

预处理源代码(不包括#include <stdio.h>的输出):

# 2 "wrapper.c" 2

int rc;
int foo(int a, int b);
int bar(int a, char *b, int *c);

extern void LogRet(const char *fn, const char *file, const char *from, int ln, int ret);

void LogRet(const char *fn, const char *file, const char *from, int ln, int ret)
{
    printf("%s.%s.%d: %s()  ret:%08x\n", file, from, ln, fn, ret);
}





extern void caller1(void);

void caller1(void)
{
    int d;
    int e = (rc = (foo)(1, 2), LogRet("foo", "wrapper.c", __FUNCTION__, 23, rc), rc);
    int f = (rc = (bar)(3, "abc", &d), LogRet("bar", "wrapper.c", __FUNCTION__, 24, rc), rc);
    printf("%s(): %d + %d + %d = %d\n", __func__, d, e, f, d + e + f);
}
# 36 "wrapper.c"
extern void caller2(void);

void caller2(void)
{
    int d;
    int e = ((rc = (foo)(2, 3)), LogRet("foo", "wrapper.c", __func__, 41, rc), rc);
    int f = ((rc = (bar)(3, "abc", &d)), LogRet("bar", "wrapper.c", __func__, 42, rc), rc);
    printf("%s(): %d + %d + %d = %d\n", __func__, d, e, f, d + e + f);
}

int (foo)(int a, int b)
{
    return (a + b) % 3;
}

int (bar)(int a, char *b, int *c)
{
    int d = b[a];
    *c = d;
    return a + d;
}

int main(void)
{
    caller1();
    caller2();
    return 0;
}

在Mac OS X 10.9.2上使用GCC 4.8.2进行测试。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM