log: fix log_message so do only one write before the fsync.

This way the logging from the various child processes does not
get clobbered up. Formerly, the different write portions
(time stamp, message, newline) would get mixed from the
various child processes' log messages.

Michael
This commit is contained in:
Michael Adam 2009-12-23 00:24:36 +01:00
parent adf4640104
commit bc10479452

View File

@ -173,6 +173,8 @@ void log_message (int level, const char *fmt, ...)
syslog (level, "%s", str);
#endif
} else {
char *p;
nowtime = time (NULL);
/* Format is month day hour:minute:second (24 time) */
strftime (time_string, TIME_LENGTH, "%b %d %H:%M:%S",
@ -182,6 +184,17 @@ void log_message (int level, const char *fmt, ...)
syslog_level[level], time_string,
(long int) getpid ());
/*
* Overwrite the '\0' and leave room for a trailing '\n'
* be added next.
*/
p = str + strlen(str);
vsnprintf (p, STRING_LENGTH - strlen(str) - 1, fmt, args);
p = str + strlen(str);
*p = '\n';
*(p+1) = '\0';
assert (log_file_fd >= 0);
ret = write (log_file_fd, str, strlen (str));
@ -190,19 +203,6 @@ void log_message (int level, const char *fmt, ...)
"Could not write to log file");
}
vsnprintf (str, STRING_LENGTH, fmt, args);
ret = write (log_file_fd, str, strlen (str));
if (ret == -1) {
log_message (LOG_WARNING,
"Could not write to log file");
}
ret = write (log_file_fd, "\n", 1);
if (ret == -1) {
log_message (LOG_WARNING,
"Could not write to log file");
}
fsync (log_file_fd);
}