I have a process opening a file in append mode. In this case it is a log file. Sample code:
int main(int argc, char **argv) {
FILE *f;
f = fopen("log.txt", "a");
fprintf(f, "log entry line");
fclose(f);
}
Two questions:
- If I have multiple processes appending to the same file, will each log line appear distinctly or can they be interlaced as the processes context switch?
- Will this write block if lots of processes require access to the file, therefore causing concurrency problems?
I am considering either doing this in its simplest incarnation or using zeromq to pump log entries over pipes to a log collector.
I did consider syslog but I don’t really want any platform dependencies on the software.
The default platform is Linux for this btw.
You’ll certainly have platform dependencies since Windows can’t handle multiple processes appending to the same file.
Regarding synchronization problems, I think that line-buffered output /should/ save you most of the time, i.e. more than 99.99% of short log lines should be intact according to my short shell-based test, but not every time. Explicit semantics are definitely preferable, and since you won’t be able to write this hack system-independently anyway, I’d recommend a syslog approach.