D
Dilip
I have a tiny design question to ask.
I am stuck with a home-brewed logging library that has different
methods for logging messages based on severity (like warning, error,
information etc.). Its very primitive, very simple and kind of gets
the job done.
The only drawback, I find myself littering my entire code base like
this:
ostringstream msg;
msg.str("");
msg << "some error code=" << errcode;
LoggingObj->ErrorMsg(msg.str().c_str());
I can probably have just one global ostringstream object and just keep
re-using it. Even then, the remaining 3 statements gets repeated
everywhere. Its bloating my codebase and is an eye-sore to look at!
Ideally I would love to be able to do something like:
wrapperObj << "some message to log. error code=" << err << endl;
where wrapperObj is an instance of a LogWrapper class that implements
the << operator and internally just takes the formatted string and
calls LoggingObj's appropriate method. LogWrapper will also probably
manage the lifetime of the actual logging object.
I haven't figured out how to tell wrapperObj what kind of severity the
message has. In this case I will have to assume that there will be
only one instance of wrapperObj which can be manipulated by multiple
threads, so I probably need a way to insert the severity information
inside the extractor operator itself... something like:
wrapperObj << setSeverity(severityID::warning) << "blah....\n";
I am working under the assumption that since every thread has its own
stack, any local variables inside the function will be local to that
thread.. so I basically want to pass the severity information as some
kind of argument to the extractor operator (along with the message to
be logged of course).
I apologize for the 10,000 feet description of the problem -- I would
appreciate being pointed in the right direction to achieve my goal. I
am fairly proficient in C++ but I don't know a whole lot about C++ I/O
Streams. However I can pick up quite fast if I just knew what I should
start looking for.
For ex: should LogWrapper class derive from ostream or something like
that to inherit the functionality available to cout and friends?
I am stuck with a home-brewed logging library that has different
methods for logging messages based on severity (like warning, error,
information etc.). Its very primitive, very simple and kind of gets
the job done.
The only drawback, I find myself littering my entire code base like
this:
ostringstream msg;
msg.str("");
msg << "some error code=" << errcode;
LoggingObj->ErrorMsg(msg.str().c_str());
I can probably have just one global ostringstream object and just keep
re-using it. Even then, the remaining 3 statements gets repeated
everywhere. Its bloating my codebase and is an eye-sore to look at!
Ideally I would love to be able to do something like:
wrapperObj << "some message to log. error code=" << err << endl;
where wrapperObj is an instance of a LogWrapper class that implements
the << operator and internally just takes the formatted string and
calls LoggingObj's appropriate method. LogWrapper will also probably
manage the lifetime of the actual logging object.
I haven't figured out how to tell wrapperObj what kind of severity the
message has. In this case I will have to assume that there will be
only one instance of wrapperObj which can be manipulated by multiple
threads, so I probably need a way to insert the severity information
inside the extractor operator itself... something like:
wrapperObj << setSeverity(severityID::warning) << "blah....\n";
I am working under the assumption that since every thread has its own
stack, any local variables inside the function will be local to that
thread.. so I basically want to pass the severity information as some
kind of argument to the extractor operator (along with the message to
be logged of course).
I apologize for the 10,000 feet description of the problem -- I would
appreciate being pointed in the right direction to achieve my goal. I
am fairly proficient in C++ but I don't know a whole lot about C++ I/O
Streams. However I can pick up quite fast if I just knew what I should
start looking for.
For ex: should LogWrapper class derive from ostream or something like
that to inherit the functionality available to cout and friends?