By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,194 Members | 884 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,194 IT Pros & Developers. It's quick & easy.

Rolling files in C++

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

P: n/a
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

P: n/a
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.