Re: Deriving my own stream class
Hello,
Dan Smithers wrote:
Thank you very much Bernd,
You're welcome.
I thought that I had tried using this->getloc() during my trials. I
guess that either I had dismissed it as being implicit, or tried it at
the same time as some other "fix" and taken them all out when it
didn't all work.
It is pretty hard to learn that template stuff just by trying and
without working through some textbook. Even worse, you might be faced
with code from different times, when even the textbooks told different
things, or when textbooks on some matter did not exist.
I think what you have tried about iostreams has been tried often enough
and you can find a lot to read about it, but sometimes that stuff does
not hold anymore to the word.
I see that I had made m_fmt public. If I wanted it private, then
operator<< would need to be a friend, or use an access method. Which
is better?
IMO it is better to provide some public methods doing just the work you
need on those data members. Then you can avoid most of that friend
business. Those extra public methods could do fine-grained checks, so
it could be public without posing threats.
Can you suggest where I should look for good log libraries?
Use keywords "C++ logging library" at a search engine, there are loads
of such libraries. What is good depends on your actual requirements.
Maybe you can find some projects similar to yours and see how they do.
With the danger to become OT here, some thoughts on logging:
If logging is an inherent part of your project, then why not designing
it in an application specific manner, i.e. you design a logging
interface not as generic as the iostreams library, but to your needs.
If you have something to log, then the actual formatting of the message
is arbitrary at that place, while using that operator<< interface to
iostreams concerns you with the actual formatting all over. The best
you can get is a method taking the context info through parameters
creating the log message and routing it to the right place.
And sometimes logging has to be done different depending on the platform
and the language. If you do logging through an application specific
interface, then porting and translating the logging requires work only
behind that interface.
Bernd