473,395 Members | 1,689 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

deriving from ostream - Logger

I set out to make a custom logger. Examining some other code laying around,
I came across one that derived from ostream, and had a associated class
derived from streambuf. Is this practice a good idea? I was under the
impression that deriving from std classes that do not have virtual methods
was bad. Is ostream designed to be derived from?

If, so are there any resources on how to do this properly? ostream has alot
if internals that look pretty difficult to..for loss of words...understand
completely.

Thanks.
Nov 30 '07 #1
1 3471
On Nov 30, 5:16 am, "Christopher Pisz" <some...@somewhere.netwrote:
I set out to make a custom logger. Examining some other code
laying around, I came across one that derived from ostream,
and had a associated class derived from streambuf. Is this
practice a good idea?
It depends on how you implement the framework. In my case, the
"logger" is the return value of a function, which means that it
must support copy, and so deriving from ostream is out. Other
architectures are certainly possible, however, and if copy isn't
necessary, deriving from ostream is probably a good solution.
I was under the impression that deriving from std classes that
do not have virtual methods was bad.
So how does that affect ostream. Ostream has a virtual
destructor.
Is ostream designed to be derived from?
Definitly. What do you think ofstream and ostringstream do?
If, so are there any resources on how to do this properly?
ostream has alot if internals that look pretty difficult
to..for loss of words...understand completely.
For the most part, you derive from ostream for convenience: to
automatically create and destroy the desired type of streambuf.
It's also usual to provide rdbuf(), to return a pointer to the
derived type of streambuf.

In the case of a logger based on a temporary, it might make
sense to add some special "end of log record" handling in the
destructor. Plus some special function which returns a
non-const reference, to kick things off (and allow the use of
non-member << operators). Something like:

#define LOG( level ) \
ologstream( level ).start( __FILE__, __LINE__ )

for example: the constructor of ologstream connects to a
streambuf according to the level, the start function outputs the
filename and linenumber, and returns an ostream& so that all <<
operators can be used, and the destructor calls a special
function on the streambuf which flushes (but also ensures that
the output is correctly terminated with a '\n', etc.).

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Nov 30 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Laszlo | last post by:
Hi I need a GL::Logger wich implements Class::Singleton to be a singleton and Log::Log4perl to can log amy message into a file. Here is the module source:...
3
by: Hans De Winter | last post by:
Hi, I am really puzzled why my compiler does not accept the following code:- --- CODE STARTS HERE --- #include <iostream> using namespace std; class Foo {
2
by: Julian | last post by:
I would like to have output from my program to be written to cout as well as a file. (actually, i want several other output options but this should explain my problem in the simplest way). I have...
1
by: hitech_guy | last post by:
Hi, I am creating my own class derving from ostream. I am facing problem in compiling with GNU G++ compiler under linux class IWCoreEXPORT IWAFTraceLine : public ostream { public:...
4
by: fabricemarchant | last post by:
Hi ! I've translated Andru Luvisi small LISP interpreter sl3.c http://www.sonoma.edu/users/l/luvisi/sl3.c in C++ in order to understand how it works and to experiment with small LISP....
5
by: lars | last post by:
Hi, I have C (printf) style debug functions static void debugPrint(Silver::Strategy& s, unsigned level, const char *format, ...) { if (s.verbosityLevel() >= level ) { va_list args ;...
0
by: James Kanze | last post by:
On 11 avr, 17:44, "mc" <mc_r...@yahoo.comwrote: OK. If the actual format is well documented, that's half the battle won already. Note, however, that reading a float as an int is still very...
6
by: Dan Smithers | last post by:
I want to write my own class derived from the ostream class. I have been getting errors with my templates: First, I get an error writing a nested template. If I leave the function definition...
1
by: AJG | last post by:
Hi there. I am using a library called SOCI that has a method to set a stream which it uses to log SQL queries. The signature is as follows: void setLogStream(std::ostream *s); This works great...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.