Link to home
Start Free TrialLog in
Avatar of reden
reden

asked on

Deriving from streambuf

Hi,

Iam new to streams and C++ and Im writing a generic logging for my app and I want to have something like this,

it will display activities on the console and have an option to write it on a logfile, each line must have a timestamp before the text.

but in my classes and other source file, i want to use something like this
  int i = 5;
  cout << "this is a sample text";
  cout << "there are " << i << " connections";
or
  mobj << "this is a sample text";
  mobj << "there are " << i << " connections";

cout or mobj must handle the timestamp thing, and the decision to write it on a text file.

I need a sample code that does this thing


ASKER CERTIFIED SOLUTION
Avatar of ambience
ambience
Flag of Pakistan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
for time stamp

void put_timestamp()
{
    char tmpbuf[128];
   _strtime( tmpbuf );
     out << tmpbuf;
}

my_ostream& operator<<(int p)
{
    put_timestamp();
    out << p; return *this;
}
....
Avatar of proskig
proskig

As a comment:
my_ostream& operator<<(int p)
{
   put_timestamp();
   out << p; return *this;
}

It means that
mobj << "there are " << i << " connections";

will put timestamp after "there are" string, which is IMHO not what author wants.
that is right, in that case you can explicitly call mobj.put_timestamp();
Dear reden

I think you forgot this question. I will ask Community Support to close it unless you finalize it within 7 days. You can always request to keep this question open. But remember, experts can only help you if you provide feedback to their questions.
Unless there is objection or further activity,  I will suggest to accept

     "ambience"

comment(s) as an answer.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
Force accepted

** Mindphaser - Community Support Moderator **