如何在 c 中为日志获取适当的时间戳? [英] How to get appropriate timestamp in c for logs?

查看:22
本文介绍了如何在 c 中为日志获取适当的时间戳?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个客户端-服务器应用程序.我想做一些日志记录.

I'm creating a client-server application. I want to do some logging.

服务器在 C 中.现在我正在向终端打印消息.所以我可能只是将它复制到 sprintf 并添加时间戳.我该怎么做那个时间戳?它可能应该包括日期、小时、分钟、秒.

Server is in C. Now I'm print messages to the terminal. So I'll probably just copy that to sprintf and add timestamp. How can I do that timestamp? It should probably include date, hours, minutes, seconds.

推荐答案

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

在我的电脑上,它只是打印

On my PC, it just prints

Wed Mar 07 12:27:29 2012

在此处查看所有与时间相关的功能http://pubs.opengroup.org/onlinepubs/7908799/xsh/time.h.html

Check out the whole range of time related functions here http://pubs.opengroup.org/onlinepubs/7908799/xsh/time.h.html

这篇关于如何在 c 中为日志获取适当的时间戳?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆