Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get appropriate timestamp in c for logs?

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

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.

like image 357
user1097772 Avatar asked Mar 07 '12 06:03

user1097772


1 Answers

Please find the thread safe version of Pavan's answer below.

time_t ltime;
struct tm result;
char stime[32];

ltime = time(NULL);
localtime_r(&ltime, &result);
asctime_r(&result, stime);

Please refer to this for more details.

like image 148
Jay Avatar answered Oct 02 '22 15:10

Jay