Rolling files in C++

Discussion in 'C++' started by farhadtarapore, Mar 20, 2007.

  1. 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.
     
    farhadtarapore, Mar 20, 2007
    #1
    1. Advertising

  2. On 20 Mar, 09:06, "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::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.

    --
    Erik Wikström
     
    =?iso-8859-1?q?Erik_Wikstr=F6m?=, Mar 20, 2007
    #2
    1. Advertising

  3. farhadtarapore

    Guest

    On Mar 20, 4:06 am, "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::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.
     
    , Mar 20, 2007
    #3
  4. farhadtarapore

    Larry Smith Guest

    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::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/
     
    Larry Smith, Mar 20, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. PT
    Replies:
    0
    Views:
    4,198
  2. Brett Sheeran

    Log4j Not Rolling Files

    Brett Sheeran, Feb 9, 2004, in forum: Java
    Replies:
    1
    Views:
    7,428
    Oscar kind
    Feb 23, 2004
  3. Dharmveer Jain
    Replies:
    0
    Views:
    497
    Dharmveer Jain
    Sep 14, 2004
  4. Rolling uphill

    , Dec 8, 2005, in forum: Java
    Replies:
    0
    Views:
    485
  5. Replies:
    3
    Views:
    408
    James Kanze
    Nov 15, 2007
Loading...

Share This Page