in reply to Writing to a log file without colliding

You need locks if your writing to one file.. You said that you were worried that exclusive locks might degrade performance.

There are other options

Maybe you can write 2 files with time stamps and merge them at the end of a run based on time.
Or have a separate process perform the logging (ie create shared memory or some other ipc like sockets). The logging process just gets data from other processes and stores in a file. Other processes could just place data they want logged in shared memory (or send via socket however its set up) without blocking or worrying about locks. This is significantly more complicated but may work better in the long run.

  • Comment on Re: Writing to a log file without colliding

Replies are listed 'Best First'.
Re^2: Writing to a log file without colliding
by Grygonos (Chaplain) on Aug 17, 2004 at 14:13 UTC

    I very much agree with the socket idea. It decouples the two processes further. As long as the shared memory only exists between each process and the log process/daemon then its just as decoupled as sockets. Prepend a timestamp to your log. Then you could come up with some cool sorting algorithm to make sure your log file is truly inorder if you make your timestamp look like

    [yyyy-mm-dd_hh:mm:ss]|$log_entry
    You can split the logfile based on the pipe and then substitute the square brackets,dashes,colons, and undescores with nulls. You then have an int that you can sort a hash (by key) with, and then have a truly ordered logfile. You would obviously re-sub the special characters in before writing back to the final log (if you wanted to keep them)