Skip to content

Commit bc10479

Browse files
committed
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
1 parent adf4640 commit bc10479

1 file changed

Lines changed: 13 additions & 13 deletions

File tree

src/log.c

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,8 @@ void log_message (int level, const char *fmt, ...)
173173
syslog (level, "%s", str);
174174
#endif
175175
} else {
176+
char *p;
177+
176178
nowtime = time (NULL);
177179
/* Format is month day hour:minute:second (24 time) */
178180
strftime (time_string, TIME_LENGTH, "%b %d %H:%M:%S",
@@ -182,22 +184,20 @@ void log_message (int level, const char *fmt, ...)
182184
syslog_level[level], time_string,
183185
(long int) getpid ());
184186

185-
assert (log_file_fd >= 0);
187+
/*
188+
* Overwrite the '\0' and leave room for a trailing '\n'
189+
* be added next.
190+
*/
191+
p = str + strlen(str);
192+
vsnprintf (p, STRING_LENGTH - strlen(str) - 1, fmt, args);
186193

187-
ret = write (log_file_fd, str, strlen (str));
188-
if (ret == -1) {
189-
log_message (LOG_WARNING,
190-
"Could not write to log file");
191-
}
194+
p = str + strlen(str);
195+
*p = '\n';
196+
*(p+1) = '\0';
192197

193-
vsnprintf (str, STRING_LENGTH, fmt, args);
194-
ret = write (log_file_fd, str, strlen (str));
195-
if (ret == -1) {
196-
log_message (LOG_WARNING,
197-
"Could not write to log file");
198-
}
198+
assert (log_file_fd >= 0);
199199

200-
ret = write (log_file_fd, "\n", 1);
200+
ret = write (log_file_fd, str, strlen (str));
201201
if (ret == -1) {
202202
log_message (LOG_WARNING,
203203
"Could not write to log file");

0 commit comments

Comments
 (0)