我正在 Linux 用户空间中用 C 编写嵌入式应用程序。 我需要将文件名和函数名添加到系统日志字符串中。 我该怎么做?
注意:为了更好地解释我自己,我需要定义一个宏来填充文件名和函数名.. 我所做的是以下内容:
#define LOG(prio, ...) my_log(__FILE__, __LINE__, __func__, prio, __VA_ARGS__)
问题出在 my_log 的实现上:
void my_log(const char *file, int line, const char *func, int prio, const char *fmt, ...)
{
//how to add the "filename:line, funcname: " string to the fmt string
va_start(args, full_fmt);
vsyslog(prio, full_fmt, args);
va_end(args);
}
最佳答案
syslog
接受类似 printf
的格式字符串。您可能指的是以下内容:
syslog(1, "%s:%d: something bad happened", __FILE__, __LINE__);
__FILE__
自动扩展为包含当前源文件的字符串。 __LINE__
类似地扩展到当前文件中的当前行。
关于c - 如何将文件名添加到系统日志字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37385561/