Add a nice timestamp to the output of log file messages

This commit is contained in:
Laurence Withers 2008-07-26 17:45:52 +00:00
parent 9c3a85f824
commit 340c72f9e2
2 changed files with 10 additions and 3 deletions

View File

@ -113,6 +113,8 @@ void log_func_file(int level, const char* fmt, ...)
va_list va;
int ret;
const char* level_str;
char timebuf[40];
time_t t;
/* choose appropriate line description */
switch(level) {
@ -127,11 +129,15 @@ void log_func_file(int level, const char* fmt, ...)
default: level_str = "?BUG?"; break;
}
/* construct timestamp */
time(&t);
strftime(timebuf, sizeof(timebuf), "%FT%H:%M:%S", gmtime(&t));
/* print to file descriptor */
va_start(va, fmt);
if(safe_write_fixed(STDERR_FILENO, level_str, 5) != 5) ret = -1;
ret = vdprintf(log_func_fd, fmt, va);
if(safe_write_fixed(STDERR_FILENO, "\n", 1) != 1) ret = -1;
ret = dprintf(log_func_fd, "%s %s ", timebuf, level_str);
if(ret != -1) ret = vdprintf(log_func_fd, fmt, va);
if(ret != -1 && safe_write_fixed(log_func_fd, "\n", 1) != 1) ret = -1;
va_end(va);
/* report errors */

View File

@ -117,6 +117,7 @@ int main(int argc, char* argv[])
LOG_ANYWHERE("Error logging to file `%s' (%m).", log_argument);
return 1;
}
log_destination_set(log_destination_file);
}
/* daemonise? */