Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9108727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T02:52:27+00:00 2026-06-17T02:52:27+00:00

I have to extend the ofstream class to write a logging stream service. The

  • 0

I have to extend the ofstream class to write a logging stream service.

The goal is to intercept each line, adding at the head of each one a customized text (date/time, severity, and so on).

Now, it’s clear that I’m not a C++ guru, so I read a lot of documentation about this task, e.g.

http://www.angelikalanger.com/Articles/Topics.html#CPP
http://asmodehn.wordpress.com/2010/06/20/busy-c-coding-and-testing
http://gabisoft.free.fr/articles-en.html

The above articles suggest to write a custom stream buffer, but during porting of this concept on file streams I encountered a lot of difficulties.

Is there a simpler method to achieve this goal?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T02:52:28+00:00Added an answer on June 17, 2026 at 2:52 am

    You don’t need to write a custom stream buffer; the simplest and most straightforward way is to make a class you can send output to by giving it a templatized single argument operator<<():

    template <typename T>
    std::ostream& operator<<(const T& data_)
    {
        m_outstream << m_severity << "\t" << getTimeStamp() << "\t" << data_;
    
        return m_outstream;
    }
    

    Where m_outstream is some type of std::ostream (std::ofstream, etc). m_severity and getTimeStamp() are examples of what you can insert (and you can create a bog standard two argument operator<<() to format and output the severity type to an ostream).

    This then allows you to do things like:

    myLogger << "failingFunction(" << argument <<
                "): Could not do something." << std::endl;
    

    and you will get output on m_outstream that looks like:

    WARNING 2012-01-03 19:32 failingFunction(“argument value”): Could not do something.

    Beyond this, you will want a way to set the severity, which can be as simple as a method called on the logger class you added the templatized operator<<() to. If you want to get really fancy, you can write your own manipulator that acts much like setw() would on a std::ostream. See http://www.math.hkbu.edu.hk/parallel/pgi/doc/pgC++_lib/stdlibug/man_6665.htm for a primer on writing manipulators.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have to extend an existing applet by adding it the ability to write
I have class that extend FragmentActivity in it I add fragment to layout as
I have the capability to extend a class at compile time, but I need
I have a class PlaylistTrack that I want to extend class Song. When I
I have a class for logging, which MUST NOT inherit std::ostream, and has operator<<
I have one class called Person that basically looks like: public class Person {
When I have to extend the behaviour of a class without modifying it, I
For example, I have a class that has two strings, one of which must
I have two classes, one Circle() and the other GeometricObject(). I have to extend
I have AsyncTask extend class I want to display progress dialoge so I use

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.