469,954 Members | 1,722 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,954 developers. It's quick & easy.

Rolling files in C++

I have a very large C++ application that has been converted into a
windows service. This application writes a lot of statements to the
console i.e. cout and cerr.
I have used
std::ofstream out(coutFilePath.c_str (), ofstream::out |
ofstream::app);
if(!out.is_open())
throw std::runtime_error("Failed to open cout file");

//Save the previous target before we redirect
std::streambuf* orig_cout = std::cout.rdbuf( out.rdbuf());

.....
.... code that uses cout, calls lots of libraries, functions, etc...

.....
.....

//Restore the cout to previous target
std::cout.flush();
std::cout.rdbuf(orig_cout);
out.close();

//////////////////////////

My problem is this: The application cout file fills up very quickly. I
need some mechanism like a rolling file appender of Log4Cpp. How can I
implement a rolling file log in C++?

Thanks in advance.

Mar 20 '07 #1
3 8699
On 20 Mar, 09:06, "farhadtarapore" <farhadtarap...@gmail.comwrote:
I have a very large C++ application that has been converted into a
windows service. This application writes a lot of statements to the
console i.e. cout and cerr.
I have used
std::ofstream out(coutFilePath.c_str (), ofstream::out |
ofstream::app);
if(!out.is_open())
throw std::runtime_error("Failed to open cout file");

//Save the previous target before we redirect
std::streambuf* orig_cout = std::cout.rdbuf( out.rdbuf());

....
... code that uses cout, calls lots of libraries, functions, etc...

....
....

//Restore the cout to previous target
std::cout.flush();
std::cout.rdbuf(orig_cout);
out.close();

//////////////////////////

My problem is this: The application cout file fills up very quickly. I
need some mechanism like a rolling file appender of Log4Cpp. How can I
implement a rolling file log in C++?
Don't know Log4Cpp but I assume that you want to rotate the logfiles.
There are several ways to do this, one would be to periodically check
how much data has been written to out (use tellp()) and if this
exceeds some value you close the file, rename it and then opens a new
file. Make sure that none is trying to write to the log when doing
this (if multithreaded). You could also shift files periodically
instead of based on size.

--
Erik Wikström

Mar 20 '07 #2
On Mar 20, 4:06 am, "farhadtarapore" <farhadtarap...@gmail.comwrote:
I have a very large C++ application that has been converted into a
windows service. This application writes a lot of statements to the
console i.e. cout and cerr.
I have used
std::ofstream out(coutFilePath.c_str (), ofstream::out |
ofstream::app);
if(!out.is_open())
throw std::runtime_error("Failed to open cout file");

//Save the previous target before we redirect
std::streambuf* orig_cout = std::cout.rdbuf( out.rdbuf());

....
... code that uses cout, calls lots of libraries, functions, etc...

....
....

//Restore the cout to previous target
std::cout.flush();
std::cout.rdbuf(orig_cout);
out.close();

//////////////////////////

My problem is this: The application cout file fills up very quickly. I
need some mechanism like a rolling file appender of Log4Cpp. How can I
implement a rolling file log in C++?

Thanks in advance.
To roll a log you just close the old fstream and open a new fstream.

Kind of OT but...
Our apps have structured names for their log files. So we just append
a number (e.g. 0, 1, 2) for rolling purposes. The roller keeps the
maximum number of files to keep around (e.g. 10).

There are at least two ways to determine when to roll the file:
1. Check each time you output. Doesn't seem very efficient.

2. Roll it periodically (e.g. once a day).

Hope that helps.

Mar 20 '07 #3
farhadtarapore wrote:
I have a very large C++ application that has been converted into a
windows service. This application writes a lot of statements to the
console i.e. cout and cerr.
I have used
std::ofstream out(coutFilePath.c_str (), ofstream::out |
ofstream::app);
if(!out.is_open())
throw std::runtime_error("Failed to open cout file");

//Save the previous target before we redirect
std::streambuf* orig_cout = std::cout.rdbuf( out.rdbuf());

....
... code that uses cout, calls lots of libraries, functions, etc...

....
....

//Restore the cout to previous target
std::cout.flush();
std::cout.rdbuf(orig_cout);
out.close();

//////////////////////////

My problem is this: The application cout file fills up very quickly. I
need some mechanism like a rolling file appender of Log4Cpp. How can I
implement a rolling file log in C++?

Thanks in advance.
Why not just use 'log4cpp'?

http://log4cpp.sourceforge.net/
Mar 20 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by deko | last post: by
5 posts views Thread by traga_2_whiskys | last post: by
3 posts views Thread by DB2 Convert | last post: by
reply views Thread by PeteCresswell | last post: by
101 posts views Thread by Elijah Cardon | last post: by
17 posts views Thread by Jose Durazo | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.