Hit another snag in my server program. Just to remind you of the basic setup, once the user tells the server to start, it is an infinite (sort of) loop of listening to a socket and dispatching each request to a new thread to be handled. Obviously servers need to keep logs, and I had planned to have the log consist of all the user settings (root directory, CGI programs, etc...) followed by a series of entires. Something like:
I thought this would all work out okay, but the danger of multithreading just hit me. If I have two requests come in very close to eachother, the records might overlap and it would be something like:Code:Timestamp1 Request1 Headers1 Headers1 Status1 Timestamp2 Request2 Headers2 Headers2 Status2
Which would obviously get confusing with the actual data in there too.Code:Timestamp1 Request1 Headers1 Timestamp2 Headers1 Request2 Status1 Headers2 Headers2 Status2
A couple of ideas, which require your feedback:
1) Writing the whole record to an array of strings (or hashtable), and sending that to the file in one go. Would there still be the possibility of overlap?
2) Assigning a unique number to each request and precede each piece of the log with that number.
3) Maybe set up a queue of requests that need to be logged and having a completely separate thread that just reads from the queue and wrties to the file.
I know 2 would work, but it's a bit messy. If possible, I'd rather have the output of 1/3.
As far as 1 goes, I like this idea best, but I don't know how well it would work. I'm still getting everything set up on the computer I plan to test the server on, so it's the not the most convenient time...
And 3 - I like this idea too, I'm just not sure how to make this 'queue'.
Thanks a lot for any ideas/advice.