Posted By

deepsoul on 03/18/10


Tagged

printf logfile


Versions (?)

Log all output to file


 / Published in: C
 

With the code fragment below, you can easily add output logging to a text-based program. Add the functions, open the log file and replace calls to (f)printf by (f)printflog.

  1. FILE *log= NULL;
  2.  
  3. int printflog( const char *format, ... )
  4. {
  5. va_list arglist;
  6. int ret;
  7.  
  8. va_start(arglist, format);
  9. ret= vprintf(format, arglist);
  10. if( log )
  11. vfprintf(log, format, arglist);
  12. return ret;
  13. }
  14.  
  15. int fprintflog( FILE *stream, const char *format, ... )
  16. {
  17. va_list arglist;
  18. int ret;
  19.  
  20. va_start(arglist, format);
  21. ret= vfprintf(stream, format, arglist);
  22. if( log )
  23. vfprintf(log, format, arglist);
  24. return ret;
  25. }

Report this snippet  

You need to login to post a comment.