gpt4 book ai didi

带有 _TIME_、_FILE_、_FUNCTION_、_LINE_ 的自定义打印功能

转载 作者:太空宇宙 更新时间:2023-11-04 02:50:59 25 4
gpt4 key购买 nike

我在 debug.h 中有以下代码:

#ifndef __DEBUG_H__
#define __DEBUG_H__

#ifdef DEBUG

int al_debug(const char *format,
const char * time,
const char * file,
const char * function,
int line,
...);
#define debug(fmt, args...) al_debug(fmt, __TIME__, __FILE__, __FUNCTION__, __LINE__, args...)
#else /* IF DEBUG NOT DEFINED*/
#define debug(fmt, ...) /* DO NOT PRINT ANYTHING IF DEBUG IS NOT PRESENT */
#endif /* END OF DEBUG */

#endif /* END OF __DEBUG_H__ */

debug.c中:

#include <stdarg.h>                                                     
#include <string.h>

#define __WRAP_FUNCTION__
#include "debug.h"

#ifdef DEBUG

int debug(const char *format,
const char * time,
const char * file,
const char * function,
int line,
...)
{
int done=0;
va_list arg;
va_start(arg, format);
done = vfprintf(stdout, "%s :%s:%s:%d", time, file, function, line);
done += vfprintf(stdout, format, arg);
va_end(arg);

return done;
}
#endif

编译后出现以下错误:

gcc -g -Wall -DDEBUG -c debug.c -o d_debug.o
debug.c:16:1: error: expected declaration specifiers or ‘...’ before string constant
debug.c:16:1: error: expected declaration specifiers or ‘...’ before string constant
debug.c:11:5: error: expected declaration specifiers or ‘...’ before ‘__FUNCTION__’
debug.c:16:1: error: expected declaration specifiers or ‘...’ before numeric constant

我该如何解决这个问题?这里有什么问题?提前谢谢你。

最佳答案

1) 更改函数名称

// int debug(
int al_debug(

2) 使用常规 fprintf()

// done = vfprintf(stdout, "%s :%s:%s:%d", time, file, function, line);
done = fprintf(stdout, "%s :%s:%s:%d", time, file, function, line);

3) 从开始。在 ...

之前使用参数
// va_start(arg, format);
va_start(arg, line);

4) 迂腐的注释:在每次打印后添加 done 检查 < 0。

done = fprintf(stdout, "%s :%s:%s:%d", time, file, function, line);
if (done >= 0) {
int done_former = done
done = vfprintf(stdout, format, arg);
if (done >= 0) done += done_former;
}
va_end(arg);
return done;

5) 将 args...) 更改为 args) @leeduhem

[编辑] 在格式化后不使用参数。

int al_debug(const char * time, const char * file, const char * function,
int line, const char *format, ...);
#define debug(...) al_debug(__TIME__, __FILE__, __func__, __LINE__, __VA_ARGS__)
// @leeduhem for __VA_ARGS__
#else /* IF DEBUG NOT DEFINED*/
#define debug(...) /* DO NOT PRINT ANYTHING IF DEBUG IS NOT PRESENT */
#endif /* END OF DEBUG */

int al_debug(const char * time, const char * file, const char * function,
int line, const char *format, ...) {
int done = 0;
va_list arg;
va_start(arg, format); // change to `format`
...

关于带有 _TIME_、_FILE_、_FUNCTION_、_LINE_ 的自定义打印功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22213141/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com