472,811 Members | 1,611 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,811 software developers and data experts.

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 9383
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: deko | last post by:
I use this script to keep count of visits to my site - it's on every page in the site. <?php $file = '/home/post/public_html/viscount'; if ($counter = @file ($file)) { $line = each($counter);...
5
by: traga_2_whiskys | last post by:
Weel i want do make a box with text rolling down to up, i've make-it wiht <marquee> in html, byt the text isn't always rolling. can anibody help me?
3
by: DB2 Convert | last post by:
Hi, Correct me if I am wrong? Why should I specify at the restore statement such as Restore Database ABC ... "WITHOUT ROLLING FOWARD"? My understand is when I am using circular logging,...
3
by: db2group88 | last post by:
we are using db2 udb v8.2 on windows. All our tables are created with "not logged initially" parameter. Our application with auto commit on. I would like to do an online backup and rolling forward...
0
by: PeteCresswell | last post by:
The one I would up was a pivot table presentation" Fund Year ReturnForYear ReturnForYearPlusPreceedingYear, ReturnForYearPlusTwoPreceedingYears.....and so-forth. But that design compromise...
101
by: Elijah Cardon | last post by:
Let's say I have m dice having n sides, such that n^m is not going to bust int as a datatype. With m=4 and n=6, an outcome might be {2, 5, 1, 2}. What is a good way to represent this in c so that...
17
by: Jose Durazo | last post by:
Hello, I'm doing an exercise to simulate rolling a pair of dice 36,000 times, then count and display how many times the simulation rolls each possible sum. For some reason each time I run my...
2
by: BurtonBach | last post by:
I have a small database that mostly keeps track of material quantities going into jobs we do. I have been able to make this work well for me. I now want to add the tracking of a rolling balance of...
4
by: brendan.wong | last post by:
hello. i'm trying to incorporate error handling into my application, but i've run into a dilemma. i've already performed 10 successful INSERTS, but on the 11th INSERT, the application fails for...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
linyimin
by: linyimin | last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.