code_junkie

Linux/c++ log rotation scheme

Nov 13th, 2011
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.79 KB | None | 0 0
  1. int main() {
  2. std::ofstream logfile(config::logname, std::ios::app);
  3. std::streambuf *const old_buffer = std::clog.rdbuf(logfile.rdbuf());
  4.  
  5. // .. the guts of the application
  6.  
  7. std::clog.rdbuf(old_buffer);
  8. }
  9.  
  10. #include <sys/types.h>
  11. #include <sys/stat.h>
  12. #include <unistd.h>
  13.  
  14. void logworker()
  15. {
  16. ino_t inode = 0;
  17. FILE *logfile;
  18.  
  19. logfile = fopen(logfilename, "a+");
  20. while(running)
  21. {
  22. struct stat mystat;
  23.  
  24. if (stat(logfilename, &mystat)!=0 || mystat.st_ino != inode)
  25. {
  26. logfile = freopen(logfilename, "a+", logfile);
  27. inode = mystat.st_ino;
  28. }
  29.  
  30. while (stuff_in_buffer)
  31. {
  32. fwrite(); /* etc */
  33. }
  34. fflush(logfile);
  35.  
  36. /* sleep until something interesting happens */
  37. }
  38. }
Add Comment
Please, Sign In to add comment