method to count 'endl' manipulator on insertion operator

J

jack

I have a class which overloads the insertion operator '<<' for every
type that ostream handles. I do this so that I can use my class a
direct replacement for cout and cerr where the insertion syntax is
used. The reason for this is that I have a log file class that needs
to know how many lines have been written. When the line exceeds a
certain number, I need to close the log file, and open a new one.

I would like to be able to track the number of times that the 'endl'
manipulator is passed to the insertion operator.

I realise that I can't simply compare the pf pointer with endl in this
function

ostream_type&
operator<<(ostream_type& (*pf) (ostream_type&))
{
if(pf == endl)
count++;
}

because it won't compile. But I was wondering if there is a way to get
the address of the concrete implementation of the endl manipulator for
the cout and cerr implementation explicitely.

Thanks
 
P

peter steiner

I have a class which overloads the insertion operator '<<' for every
type that ostream handles. I do this so that I can use my class a
direct replacement for cout and cerr where the insertion syntax is
used. The reason for this is that I have a log file class that needs
to know how many lines have been written. When the line exceeds a
certain number, I need to close the log file, and open a new one.

I would like to be able to track the number of times that the 'endl'
manipulator is passed to the insertion operator.

I realise that I can't simply compare the pf pointer with endl in this
function

ostream_type&
operator<<(ostream_type& (*pf) (ostream_type&))
{
if(pf == endl)
count++;
}

because it won't compile. But I was wondering if there is a way to get
the address of the concrete implementation of the endl manipulator for
the cout and cerr implementation explicitely.

try to explicitely cast the manipulator to the function pointer type:

typedef ostream_type& (*ManipT) (ostream_type&);
if (pf == static_cast<ManipT>(endl))
....

either there is some arcane reason in the standard (anyone?) or this is
just a shortcoming of many current compilers, but using template
function addresses in expressions without casting or storing in a
temporary variable and using that instead does not compile.

-- peter
 
D

Dietmar Kuehl

I have a class which overloads the insertion operator '<<' for every
type that ostream handles. I do this so that I can use my class a
direct replacement for cout and cerr where the insertion syntax is
used. The reason for this is that I have a log file class that needs
to know how many lines have been written. When the line exceeds a
certain number, I need to close the log file, and open a new one.

This is the entirely wrong approach to go about the problem! The
correct approach is not to defeat the IOStream classes but to use
them: you shall create a filtering stream buffer and check for
appropriate conditions in its 'overflow()' and 'sync()' methods.
 
D

David Harmon

On 20 Dec 2005 13:52:54 -0800 in comp.lang.c++,
(e-mail address removed) wrote,
I have a class which overloads the insertion operator '<<' for every
type that ostream handles. I do this so that I can use my class a
direct replacement for cout and cerr where the insertion syntax is
used. The reason for this is that I have a log file class that needs
to know how many lines have been written. When the line exceeds a
certain number, I need to close the log file, and open a new one.

Bad idea. Use a custom streambuf derivative and don't redo any of
the formatting operations of the iostream front end. Your class is
not a direct replacement for cout or cerr unless it can be the
object of a std::iostream& reference.

See:
http://www.inf.uni-konstanz.de/~kuehl/c++/iostream/
http://groups.google.com/groups?as_q=streambuf&as_ugroup=comp.lang.c++
I would like to be able to track the number of times that the 'endl'
manipulator is passed to the insertion operator.

Should probably be never. Use endl only when it is important to
flush the stream after every line. For ordinary purposes use '\n'.

Your streambuf can count instances of '\n' sent to it.
 
J

jack

Bad idea. Use a custom streambuf derivative and don't redo any of
the formatting operations of the iostream front end.

I guess you are correct here. I kind of knew my solution was a bit
wrong, but couldn't figure out how.
Your class is
not a direct replacement for cout or cerr unless it can be the
object of a std::iostream& reference.

I am not sure I understand what you are saying here. Could you amplify
this.

That would solve one part of my problem (which I didn't state) in that
I also need to put a timestamp on each new line. That was one of the
reasons for intercepting the endl manip., I cannot see, howvever how I
would use this to open a new file when I reach a certain line count.
What am I missing ?
Should probably be never. Use endl only when it is important to
flush the stream after every line. For ordinary purposes use '\n'.

Your streambuf can count instances of '\n' sent to it.

Understood. I realise that endl caused a flush, but prefer it to the
use of '\n' in my strings. BTW, is there a manipulator which just
inserts a '\n' character and doesn't flush ?
 
D

deane_gavin

Understood. I realise that endl caused a flush, but prefer it to the
use of '\n' in my strings.

Any particular reason?
BTW, is there a manipulator which just
inserts a '\n' character and doesn't flush ?

Errr... no. Because the effect of inserting '\n' is "inserts a '\n'
character and doesn't flush". It does precisely what you want. Why not
use it?

Gavin Deane
 
D

Dietmar Kuehl

That would solve one part of my problem (which I didn't state) in that
I also need to put a timestamp on each new line. That was one of the
reasons for intercepting the endl manip., I cannot see, howvever how I
would use this to open a new file when I reach a certain line count.
What am I missing ?

There is no need to use a generic filtering stream buffer which gets
the stream buffer to used passed, e.g. as constructor argument.
Instead, you can use a 'std::filebuf' internally which you open to
refer to the desired file. For example, prior to writing the
timestamp you could check the current line count and, when necessary,
close the file buffer and reopen it to write to a new file.
Understood. I realise that endl caused a flush, but prefer it to the
use of '\n' in my strings.

Just note that relying on 'endl' is somewhat error prone. In a logging
stream buffer I would probably only use an internal buffer and have
each character be intercepted by 'overflow()' such that I can write
the line as soon as I receive a newline. Whether the users remembers
to flush the stream buffer would be irrelevant in this situation.

Personally, I think that 'endl' is grossly overused. It looks like a
bright idea to realize a writing mode similar to C's line buffered
mode (see the '_IOLBF' parameter to C's 'setvbuf()') but it relies on
the user to do the right thing. One thing is granted: if I'm member of
the project I would mess up things in this context! Dealing with
newlines in the stream buffer is much more reliable although, depending
on your needs, it may have a performance impact. If the latter is
indeed problematic, I would still not rely on 'endl' but rather set up
"unit buffering" (see 'std::ios_base::unitbuf') on my stream...
BTW, is there a manipulator which just
inserts a '\n' character and doesn't flush ?

No but it is easy to write:

std::eek:stream& nl(std::eek:stream& out) { return out << '\n'; }

.... plus some template stuff for a little bit more generic version.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,163
Messages
2,570,897
Members
47,436
Latest member
MaxD

Latest Threads

Top