2012-08-16 43 views
0

有没有一种方法可以有条件地静音所有我的printf语句,但是,不使用任何宏?我想在不触及已有代码的情况下完成静音,但在模块中添加一行可以禁用源代码中已有的所有printf。静音printf无宏

谢谢!

int printf(const char * restrict format,...) { return 0; } 
+5

你只是想静音'printf's或每个输出到'stdout'吗? – rwos 2012-08-16 15:20:24

+0

您是否需要恢复正常行为,所以只需静音一段时间,然后再次执行正常的printf输出? – 2012-08-16 15:30:27

+0

@rwos:最好只是printf。 – Scranton 2012-08-16 17:37:42

回答

3

如果您想通过printfputsputchar等,以抵消所有的输出到stdout C库的加载它,你可以使用freopen(3)将其重定向到一个位斗,例如:

// On Unix and Unix-like systems: 
freopen("/dev/null", "w", stdout); 
// On Windows: 
freopen("NUL", "w", stdout); 
0

加给你的源代码替换库的printf用自己的实现printf

,那么你会提前使用LD_PRELOAD=mylib.so ./myprogram

2

在Linux,BSD或其他Unix,你可以创建一个共享库,它提供了自己的printf和包装的C库: