0

I'm running an OpenGL program, and have setup a basic error reporting system which I'm using to call a function which uses fprintf and prints to stderr. On top of this, I have several fprintf's inline with the rest of my code.

I also have a fstream setup which allows me to print directly to the log file of my choice. However I don't want to duplicate every line in which I use fprintf to include printing to the log.

Is there a way I can set my stderr and stdout to also point to the log, or can I create my own?

I'm seeing other questions like this and this, and the answers seem to use a tee and wtee function. The second one states that tee only works on UNIX. Since I'm doing this on Visual Studio 2015 on Windows, so this isn't going to be an option. However I am trying to cross-platform my application, so I'm looking for a cross-platform solution. I'm not too worried about Linux at this stage, as long as it works on MacOS and Windows.

Community
  • 1
  • 1
  • There's no such facility - one file descriptor writing to two files - on Linux; as such, the short answer is: nope. – Sam Varshavchik Sep 17 '16 at 00:16
  • 1
    You could use `sprintf` to create a string, then output that to both/all file handles. – 1201ProgramAlarm Sep 17 '16 at 00:37
  • 2
    just make your `my_pretty_log_function(const char *fmt, ...)` that wraps `vsprintf` and forwards the formatted output to as many streams/facilities as you wish. – Serge Sep 17 '16 at 01:19
  • @Serge I was going to do that, but I also have them lying around everywhere else in the file which I can't afford to use the function for. –  Sep 17 '16 at 09:58
  • @1201ProgramAlarm Can you please provide an example? I don't understand. –  Sep 17 '16 at 09:58
  • char buf[BIG]; sprintf_s(buf, "format", parameters); fprintf(stderr, "%s", buf); fprintf(log_file, "%s", buf); Alternatively you could use fwrite instead of fprintf, or construct `std::string` objects with your messages. – 1201ProgramAlarm Sep 17 '16 at 18:44
  • @1201ProgramAlarm This is the issue I'm having. I don't want to need to use two different lines. One for the console and one for the log. I want to be able to use a **single** command to print to both. –  Sep 17 '16 at 22:31

0 Answers0