How to enter date and time in a log file

I have one daemon written in C. I am logging events in a log file, but now I want to add the date and time when the event is written to the log file. How can i achieve this?

Current log file: -

Event one occurred: result: Event two occurred: result: 

I want the log file to look like this: -

 Sep 14 11:35:55 Event one occurred: result: Sep 14 11:35:55 Event two occurred: result: 

My environment is C and Linux.

+7
source share
4 answers

You need to examine date and gmtime or localtime to get the actual date and time.

Then strftime can format it for you.

Program Example:

 #include <stdio.h> #include <time.h> int main (void) { char buff[20]; struct tm *sTm; time_t now = time (0); sTm = gmtime (&now); strftime (buff, sizeof(buff), "%Y-%m-%d %H:%M:%S", sTm); printf ("%s %s\n", buff, "Event occurred now"); return 0; } 

It is output:

 2011-09-14 04:52:11 Event occurred now 

I prefer to use UTC rather than local time, as it allows you to associate events with a geographically separated machine, without worrying about differences in the time zone. In other words, use gmtime rather than localtime if you are not sure that you will not go through time zones.

I also prefer the format YYYY-MM-DD HH:MM:SS , as it is easier to sort than the names of the months, vital for extraction and manipulation tools.

+12
source

Adding my log features based on @paxdiablo's answer. Using local time, but you can use gmt just by changing getFormattedTime ()

common.h

 // Returns the local date/time formatted as 2014-03-19 11:11:52 char* getFormattedTime(void); // Remove path from filename #define __SHORT_FILE__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__) // Main log macro #define __LOG__(format, loglevel, ...) printf("%s %-5s [%s] [%s:%d] " format "\n", getFormattedTime(), loglevel, __func__, __SHORT_FILE__, __LINE__, ## __VA_ARGS__) // Specific log macros with #define LOGDEBUG(format, ...) __LOG__(format, "DEBUG", ## __VA_ARGS__) #define LOGWARN(format, ...) __LOG__(format, "WARN", ## __VA_ARGS__) #define LOGERROR(format, ...) __LOG__(format, "ERROR", ## __VA_ARGS__) #define LOGINFO(format, ...) __LOG__(format, "INFO", ## __VA_ARGS__) 

common.c

 #include <time.h> // time_t, tm, time, localtime, strftime // Returns the local date/time formatted as 2014-03-19 11:11:52 char* getFormattedTime(void) { time_t rawtime; struct tm* timeinfo; time(&rawtime); timeinfo = localtime(&rawtime); // Must be static, otherwise won't work static char _retval[20]; strftime(_retval, sizeof(_retval), "%Y-%m-%d %H:%M:%S", timeinfo); return _retval; } 

You can use them as follows:

  LOGDEBUG("This is a log"); LOGDEBUG("This is a log with params %d", 42); 

What makes the conclusion:

 2014-03-19 13:22:14 DEBUG [main] [main.c:54] This is a log 2014-03-19 13:22:14 DEBUG [main] [main.c:55] This is a log with params 42 
+6
source

To get the current time, you can use time.h , for example ...

 #include <stdio.h> #include <stdlib.h> #include <time.h> int main(void) { time_t now; time(&now); printf("%s", ctime(&now)); // use ctime to format time to a string. return EXIT_SUCCESS; } 

You can also use strftime , as suggested by paxdiablo, for more time and date formatting options.

Of course, for your case, the result of ctime(&now) will go into your log char array / string instead of printf .

+4
source

based on @inolasco answer, a static variable is not thread safe. a local variable is used instead.

 void getFormattedTime(char * const p, int sz) { time_t rawtime; struct tm* timeinfo; time(&rawtime); timeinfo = localtime(&rawtime); strftime(p, sz, "%Y-%m-%d %H:%M:%S", timeinfo); } int mylog(const char* fmt, ...) { // TODO: log to file also. // TODO: create a new log file daily va_list argptr; va_start(argptr, fmt); vfprintf(stderr, fmt, argptr);//log to stderr va_end(argptr); } #ifdef _WIN32 #define __SHORT_FILE__ (strrchr(__FILE__, '\\') ? strrchr(__FILE__, '\\') + 1 : __FILE__) #else #define __SHORT_FILE__ (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__) #endif #define ___LOG___(fmt,level,path, ...) do{\ /* using local var and using a long name to avoid conflict*/ \ char LAgGV3nzJsTholGvGL2eTNXmhsqYe___xxooxxoo___[24];\ getFormattedTime(LAgGV3nzJsTholGvGL2eTNXmhsqYe___xxooxxoo___,\ sizeof(LAgGV3nzJsTholGvGL2eTNXmhsqYe___xxooxxoo___));\ mylog("%s [%s] [%s:%d] [%s] " fmt "\n", \ LAgGV3nzJsTholGvGL2eTNXmhsqYe___xxooxxoo___, \ level,\ path,\ __LINE__, \ __func__, \ ## __VA_ARGS__);\ }while(0) #define trace(fmt, ...) ___LOG___(fmt, "TRACE",__SHORT_FILE__, ## __VA_ARGS__) #define debug(fmt, ...) ___LOG___(fmt, "DEBUG",__SHORT_FILE__, ## __VA_ARGS__) #define info(fmt, ...) ___LOG___(fmt, "INFO",__SHORT_FILE__, ## __VA_ARGS__) #define warn(fmt, ...) ___LOG___(fmt, "WARN",__SHORT_FILE__, ## __VA_ARGS__) #define error(fmt, ...) ___LOG___(fmt, "ERROR",__SHORT_FILE__, ## __VA_ARGS__) #define tracel(fmt, ...) ___LOG___(fmt, "TRACE", __FILE__, ## __VA_ARGS__) #define debugl(fmt, ...) ___LOG___(fmt, "DEBUG", __FILE__, ## __VA_ARGS__) #define infol(fmt, ...) ___LOG___(fmt, "INFO", __FILE__, ## __VA_ARGS__) #define warnl(fmt, ...) ___LOG___(fmt, "WARN", __FILE__, ## __VA_ARGS__) #define errorl(fmt, ...) ___LOG___(fmt, "ERROR", __FILE__, ## __VA_ARGS__) 

name them as follows:

 info("%s", "a log"); infol("%s", "a log"); 

production:

 2017-09-06 15:55:42 [INFO] [main.c:25] [main] a log 2017-09-06 15:58:08 [INFO] [d:\main.c:25] [main] a log 
+1
source

All Articles