I am trying to write a thread-safe C logging function and am having serious problems with the IO file. So basically, I start with an interesting call fopenthat allows me to open the log in binary update mode:
FILE *log, *start;
int timeout = 0, error;
log = fopen(LOG_FILE, "wx");
if(log)
fclose(log);
log = fopen(LOG_FILE, "rb+");
Then I lock the file, including the timeout, if another thread blocks it for too long:
start = log;
rewind(start);
error = lockf(fileno(start), F_TLOCK, 0);
while( error == EACCES || error == EAGAIN)
{
usleep(LOCKED_FILE_RETRY_TIME);
timeout += LOCKED_FILE_RETRY_TIME;
if(timeout > LOCKED_FILE_TIMEOUT)
{
return;
}
error = lockf(fileno(start), F_TLOCK, 0);
}
And finally, I will add the necessary message to the end of the file, unlock it and close the file:
fseek(log, 0, SEEK_END);
fwrite((const void*) log_msg, 1, strlen(log_msg), log);
rewind(start);
lockf(fileno(start), F_ULOCK, 0);
fclose(log);
However, it seems that most messages are overwritten in the log, and are not added in much the same way as if they fopentook a “snapshot” of the file, waited for it to unlock, and wrote where the end of the file would have been if another process had not been added to it. Does anyone know how I can solve this problem?
, , , , , fseek R/W.
. !