Logging printfs to txt file

I’d like to log anything printed to the console to a file and save it in realtime.

My current main.cpp file looks like this:

  
  
#include "ofMain.h"  
#include "testApp.h"  
  
#include <stdio.h>  
#include <stdlib.h>  
  
  
int main( )  
{  
   /*create a new text file for logging with data and time as the name*/  
  
    /* alright first we need to get time and date so our logs can be ordered*/  
    char dateStr [9];  
    char timeStr [9];  
  
    /*time stuff */  
    time_t rawtime;  
    struct tm * timeinfo;  
    char fileName [80];  
  
    time ( &rawtime );  
    timeinfo = localtime ( &rawtime );  
  
    strftime (fileName,80,"logs/log_%B_%d_%y_%H_%M_%S.txt",timeinfo);  
  
    FILE *stream ;  
    if((stream = freopen(fileName, "w", stdout)) == NULL)  
       exit(-1);  
  
	ofSetupOpenGL(900,900, OF_WINDOW);  
	ofRunApp(new testApp());  
  
    stream = freopen("CON", "w", stdout);  
}  
  

This works great for logging anything that OF prints to console into a text file. The problem is, it doesn’t save the file in realtime and only saves on exit of the program. So if the application crashes, the log file prints don’t save their contents.

I think I need a better method, so that it’ll save every time the text is updated so if there’s a crash I can still get a record of what happened.

Has anyone done anything like this? Is there a simple solution similar to what I did in the main.cpp file? I think this would be a good option for OF possibly, to have the ability to turn on logging instead of printing to console/terminal.

Ok, I kind of figured a decent way for doing this. Not sure why I didn’t think of it before.

Instead of putting the code in main, I create the log file in testApp setup and in update I append/save the file. This way it always saves the file everytime update is called.

The problem is if the program crashes in between calling update though. For example, if the program crashes loading the camera which is 80% of the time (because it can’t find a camera or some other reason) then it won’t call update and therefore I can’t save the prints the camera crashed on.

Is there another way perhaps so I can make sure it’ll save the file after every printf is called?

Artem says maybe there’s an ‘inline’ way of doing this? I’m not really sure what that means though lol

http://www.cplusplus.com/reference/clib-…-texit.html

It’s not pretty or perhaps not even what you’re looking for… actually, maybe it’s not even called depending on the crash… but atexit tends to be a goto for me depending on the severity of bug I’m chasing.

Also, SDL has a crash parachute. Perhaps there is a bit you could glean from them?

Cheers.

a super simple solution can be running the app from the command line or a script, on mac and linux should be something like:

  
./oFApp > log`date +%Y%m%d%H%m%S`.txt  

or even:

  
./oFApp >tee log`date +%Y%m%d%H%m%S`.txt  

to also see the output in the console

photex, I wasn’t sure I understood that 100% Does atexit call a function if a program crashes? So for example, I could save the log file in that function? From reading the page, it looks like atexit is only called if the program closes normally (doesn’t crash) which is the current problem; I need it to save on crash.

arturo, yeah that might work in a last case scenario. Ideally we’d like to avoid using a script and have it integrated into the application.

The main reason for logging isn’t for myself. This is something that’ll go in ‘tbeta’ which probably has 20k+ user downloads. In order to help people, it’ll be best if they can just post a logfile of various crashes or what the print was in the console/terminal at the time. For this reason I think it’s more necessary to have the code in the app.

Is it possible to define printf as something else? So whenever prtinf is called it logs and saves? I’ll look into what photex said and as a last resort can try arturo’s method.

To handle crashes/exceptions, there are signal handlers you can install. It’s a bit tricky but feasible. Here’s an example tutorial, but there are many others available:

http://users.evtek.fi/~tk/rtp/signals-programming.html

also you can try the logger classes in poco, they are pretty powerful and allow to change logging from console to file, and even xml. you can configure different levels of logging per class and some other cool features.