static int error(char *fmt, ...) __attribute__ ((format(printf, 1, 2)));
static int error(char *fmt, ...)
{
int res;
static int shown=0;
va_list ap;
if (!shown) {
fprintf(stderr, "Notice: Configuration file is %s\n", filename);
shown++;
}
res = fprintf(stderr, "line %d: ", lineno);
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
errcnt++;
return res;
}
static int error(char *fmt, ...)
{
int res;
static int shown=0;
va_list ap;
if (!shown) {
fprintf(stderr, "Notice: Configuration file is %s\n", filename);
shown++;
}
res = fprintf(stderr, "line %d: ", lineno);
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
errcnt++;
return res;
}
本文介绍了一个自定义的错误处理函数的实现细节。该函数通过使用可变参数列表实现了灵活的日志输出,并确保了配置文件的相关信息仅被显示一次。此外,还展示了如何利用fprintf和vfprintf来格式化错误信息。

被折叠的 条评论
为什么被折叠?



