如何在c中为日志获取适当的时间戳?

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

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

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

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

 Wed Mar 07 12:27:29 2012 

查看全部时间相关functionhttp://pubs.opengroup.org/onlinepubs/7908799/xsh/time.h.html

请在下面找到Pavan的答案的线程安全版本。

 time_t ltime; struct tm result; char stime[32]; ltime = time(NULL); localtime_r(&ltime, &result); asctime_r(&result, stime); 

有关详细信息,请参阅此处。