I have one daemon written in C. I am logging the events in a log file, but now I want to add date and time while writing event to log file. How can I achieve that?
Current log file:-
Event one occurred: result:
Event two occurred: result:
I want the log file to look like:-
Sep 14 11:35:55 Event one occurred: result:
Sep 14 11:35:55 Event two occurred: result:
My environment is C and Linux.
To enable date and timestamp in the log:Navigate to the Program Options dialog (Tools > Options...). In the Options tree, select Logging. The Logging options will appear. Select Display timestamps in the log.
To get the current time you can use time. h for example... You can also use strftime as suggested by paxdiablo for more time/date formatting possibilities. Of course, for your case, the result of ctime(&now) will go into your log entry char array/string instead of printf .
How can this be done in the shell script? you can use following "logfile_$(date +%F). log" this will let you create file with current date.
A log file is a computer-generated data file that contains information about usage patterns, activities, and operations within an operating system, application, server or another device, and is the primary data source for network observability.
You need to look into using date
and either gmtime
or localtime
to get the actual date and time.
Then strftime
can format it for you.
Sample program follows:
#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;
}
This outputs:
2011-09-14 04:52:11 Event occurred now
I prefer the use of UTC rather than local time since it allows you to tie together events from geographically separated machine without worrying about timezone differences. In other words, use gmtime
rather than localtime
unless you're very certain you won't be crossing timezones.
I also tend to prefer the YYYY-MM-DD HH:MM:SS
format since it's easier to sort than month names, vital for extraction and manipulation tools.
If you have an implementation that provides the optional bounds-checking functions (as per Appendix K of C11), you can probably use gmtime_s
in preference. It allows you to specify your own buffer for receiving the result and is thus safer in re-entrant and/or threaded code.
To use that, you need to change your code to something like:
#include <stdio.h>
#define __STDC_WANT_LIB_EXT1__ 1
#include <time.h>
int main (void) {
char buff[20];
struct tm sTm;
time_t now = time (0);
gmtime_s (&now, &sTm);
strftime (buff, sizeof(buff), "%Y-%m-%d %H:%M:%S", &sTm);
printf ("%s %s\n", buff, "Event occurred now");
return 0;
}
Although you should be aware that the folks at Microsoft have somehow managed to get the arguments for gmtime_s
around the wrong way. You'll need to take that into account.
POSIX (and Linux) also provides a gmtime_r
function which performs in the same way as the standard gmtime_s
function (with the arguments in the correct order).
Adding my log functions, based on the answer by @paxdiablo. Using local time, but could use gmt just by modifying 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 like this:
LOGDEBUG("This is a log");
LOGDEBUG("This is a log with params %d", 42);
Which produces the output:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With