Reputation: 2877
I'm using SFML 1.6 to make a game, and sometimes, there will be errors, and when users run the game, I don't want the console showing up, so I redirected stderr
(which SFML uses to log errors apparently) to a file named after the current date. So if there's an error, the users can just check the logs. Now I also want to put the times before each log, so the log file will look like this:
12:34:17 CDT
failed to load image "sprite.png"
12:35:01 CDT
failed to load file "font.ttf"
Is there a way to "tack on" the current time to any output to get the results shown above?
EDIT
I forgot to mention, the SFML library is the one that logs most of the errors, I want to tack on the time as it leaves the application, so to speak. Kind of like a tollbooth, except anything leaving gets a time prepended. I know how to get the time as a string, and I'm not the one calling printf or any other function like it.
Upvotes: 2
Views: 659
Reputation: 300209
You can do better than a simple redirection: you can actually interpose code of your own between the incoming stream of characters and the writing to the file.
The basic idea is to derive a class from filebuf
, to control the characters that are put.
You have two strategies:
\n
character you set a flag, and on the first put
afterward you insert your timestamp (and unset the flag)\n
character is met, at which point you format your timestamp and then flush the buffer (up until \n
)If we consider that there is not much time between the start and end of formatting of each line, the easy way should work reliably enough.
I think that overriding xsputn
should be enough to get the work done.
Upvotes: 2
Reputation: 409422
If you can modify the function doing the actual printing, then yes (I recommend you look up the functions time
, ctime
, localtime
and strftime
). If you can not modify the function, then no.
Upvotes: 0