2

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.

nvoigt
  • 75,013
  • 26
  • 93
  • 142
rcplusplus
  • 2,767
  • 5
  • 29
  • 43
  • Possible duplicate of: http://stackoverflow.com/questions/997946/c-get-current-time-and-date – Rhexis May 31 '12 at 05:23
  • I can already get the time as a string, i need to prefix every stderr log with it though... – rcplusplus May 31 '12 at 05:24
  • Have you had a look at fprintf? http://www.cplusplus.com/reference/clibrary/cstdio/fprintf/ – Rhexis May 31 '12 at 05:28
  • stderr can be used as an argument for any function that expects an output stream as one of its parameters. – Rhexis May 31 '12 at 05:30
  • That's quite a surprising idea (from SFML). It basically prevents you to separate their logging from regular error output should you wish to... – Matthieu M. May 31 '12 at 08:09

2 Answers2

2

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:

  • the easy way is that each time you read a \n character you set a flag, and on the first put afterward you insert your timestamp (and unset the flag)
  • the hard way is that you bufferize the output until a \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.

Matthieu M.
  • 287,565
  • 48
  • 449
  • 722
  • +1 This advice matches with their documentation: http://www.sfml-dev.org/documentation/2.0/group__system.php#func-members (though sf::err() is from SFML 2.0, I'm sure it works the same with std::cerr) – cgmb May 31 '12 at 09:15
0

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.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621