V
VirGin
HiHiHi,
I have a logging class. I want to update it to perform the following
by overloading the insertion operator:
- I want to prepend the time to the stream. Whether I'm writing to a
stringstream, cout, cerr, whatever.
- I want to be able to drop certain lines that might otherwise be
written to the stream. This would allow me to set a logging level when
I start writing to the stream.
- In certain situations, I want to be able to insert the result of
some other operation into the stream. I can do this already.
- I want to put this in a library file that I can link to from
different projects when I need to.
Illustration of the different goals
oLog << "Some Event" << endl;
- Use the default log message level and if the message should be
logged, prepend the time to the message and process it.
oLog(a_message_level) << "Some Other Event" << endl;
oLog(different_level) << "Details regarding Some Other Event" <<
endl;
- Process the two lines based on the logging level. Drop one or both
if they don't meet whatever criteria is set.
class LogIt : public ofstream
{
public:
...
various member definitions
...
template <class T>
friend LogIt & operator<<(LogIt &oObj, const T &xVal);
friend LogIt & operator<<(LogIt &oObj, std:stream &(*el)
(std:stream &));
private:
...
various private member definitions
...
};
template <class T>
LogIt & operator<<(LogIt &oObj, const T &xVal)
{
cout << "Prefix_Rez\t" << xVal;// << endl;
return oObj;
}
LogIt & operator<<(LogIt &oObj, std:stream &(*el)(std:stream &))
{
(*el)(oObj);
return oObj;
}
I'm testing the above with:
oLog01 << "Line One" << 5 << 5.55 << endl;
oLog01 << "Line Two" << endl;
oLog01 << 333 << endl;
As you can probably see, I end up with the "Prefix_Rez" string every
time the insertion operator is called. the second problem is that the
endl isn't ending the line.
I've played with different samples, methods and ideas.
So.
- Design-wise, what is a "good" way to specify different logging
levels for different messages?
- Also design-wise, what is a good way to only process the insertion
operator one time for each message, regardless of the number of times
that the insertion operator might be used while building a given
message?
- What is the correct method of passing endl and ending that line?
Thanx
-V-
I have a logging class. I want to update it to perform the following
by overloading the insertion operator:
- I want to prepend the time to the stream. Whether I'm writing to a
stringstream, cout, cerr, whatever.
- I want to be able to drop certain lines that might otherwise be
written to the stream. This would allow me to set a logging level when
I start writing to the stream.
- In certain situations, I want to be able to insert the result of
some other operation into the stream. I can do this already.
- I want to put this in a library file that I can link to from
different projects when I need to.
Illustration of the different goals
oLog << "Some Event" << endl;
- Use the default log message level and if the message should be
logged, prepend the time to the message and process it.
oLog(a_message_level) << "Some Other Event" << endl;
oLog(different_level) << "Details regarding Some Other Event" <<
endl;
- Process the two lines based on the logging level. Drop one or both
if they don't meet whatever criteria is set.
class LogIt : public ofstream
{
public:
...
various member definitions
...
template <class T>
friend LogIt & operator<<(LogIt &oObj, const T &xVal);
friend LogIt & operator<<(LogIt &oObj, std:stream &(*el)
(std:stream &));
private:
...
various private member definitions
...
};
template <class T>
LogIt & operator<<(LogIt &oObj, const T &xVal)
{
cout << "Prefix_Rez\t" << xVal;// << endl;
return oObj;
}
LogIt & operator<<(LogIt &oObj, std:stream &(*el)(std:stream &))
{
(*el)(oObj);
return oObj;
}
I'm testing the above with:
oLog01 << "Line One" << 5 << 5.55 << endl;
oLog01 << "Line Two" << endl;
oLog01 << 333 << endl;
As you can probably see, I end up with the "Prefix_Rez" string every
time the insertion operator is called. the second problem is that the
endl isn't ending the line.
I've played with different samples, methods and ideas.
So.
- Design-wise, what is a "good" way to specify different logging
levels for different messages?
- Also design-wise, what is a good way to only process the insertion
operator one time for each message, regardless of the number of times
that the insertion operator might be used while building a given
message?
- What is the correct method of passing endl and ending that line?
Thanx
-V-