c - ubuntu 日志文件中的时间戳

标签 c linux ubuntu

<分区>

如何在 ubuntu 的日志文件中添加时间戳? 我的 c 文件中有以下代码:

#define LOG_MSG(args, ...)  fprintf(log_file, "%-*d" args " \n", 5,line_count++, ##__VA_ARGS__);

#define LOG_ERR(args, ...) do {fprintf(log_file, "%-*d%s" args " \n",5, line_count++, "Error: ", ##__VA_ARGS__); increment_error_count();} while(0)

我尝试在上面的代码中包含`date +%H:%M:%S,但它抛出一个错误 - undefined 感谢您提供任何帮助 :) 提前致谢。

最佳答案

这个答案是从这个 link 引用和检查的.

#include <time.h>
void timestamp()
{
    time_t ltime; /* calendar time */
    ltime=time(NULL); /* get current cal time */
    printf("%s",asctime( localtime(&ltime) ) );
}

希望对你有所帮助。

关于c - ubuntu 日志文件中的时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47505086/

相关文章:

C程序打印奇怪的字符

linux - Unix bash shell 脚本 - 在 'for' 循环中迭代数组

ubuntu - ubuntu 16 服务器上微软软件包的哈希和不匹配

ubuntu - 地形 Azurerm : Select Values for a Block (storage_image_reference) based on a Variable Value

c - 如何在 C 中创建 ".log"文件?

c - C 中的三元运算符

c - 从 libpcap 捕获中提取数据包长度

linux - 将 postgresql 主文件夹移出/var/lib/postgresql/9.4

linux - 删除不包含特定文本的行

javascript - Node.js 安装 : openssl not installed