Rolling files in C++

F

farhadtarapore

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::eek:fstream out(coutFilePath.c_str (), ofstream::eek:ut |
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.
 
?

=?iso-8859-1?q?Erik_Wikstr=F6m?=

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::eek:fstream out(coutFilePath.c_str (), ofstream::eek:ut |
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.
 
A

AnonMail2005

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::eek:fstream out(coutFilePath.c_str (), ofstream::eek:ut |
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.
 
L

Larry Smith

farhadtarapore said:
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::eek:fstream out(coutFilePath.c_str (), ofstream::eek:ut |
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/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top