Connecting Tech Pros Worldwide Help | Site Map

Class that can log what it's doing...sometimes

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 22nd, 2005, 09:54 PM
Eric Lilja
Guest
 
Posts: n/a
Default Class that can log what it's doing...sometimes

Hello, I have a class that I want to be able to output detailed information
about what it's doing either
to a file or to the screen. What I first tried was adding a
std::ostream-reference member variable to
the class and then adding a constructor that, apart from the "normal"
arguments, also took an std::ostream&.
I then intended to set a boolean member variable called logging to true and
have the other member functions
check that variable to determine whether they should perform logging or not.
Something like:

class foo
{
public:
foo(args); /* logging set to false, m_out set to nothing */
foo(args, std::ostream& out); /* logging set to true, m_out set to out */

void bar(); /* log to m_out if logging == true */

private:
std::ostream& m_out;
bool logging;
};

I thought about what would happen if I in a member function forgot to check
the value of logging and tried
to use m_out without it referencing something. However, I didn't get that
far because the compiler stopped
me saying that I must initialize m_out (which I didn't do in the first
constructor).
I understand why...there is no such thing as a NULL reference, it has to be
a reference to a valid object
and it may never be set to reference something else.
So I was thinking having the second constructor accepting a const
std::string& instead and that string would
hold a file name and then I would open a file with that name and log to it.
But that variant isn't very
satisfying because then the user wont be able to log directly to cout or
cerr...how do I proceed? I could solve
it by using the preprocessor...#ifndef LOGGING, and that would probably
yield faster code for the times
when I dont want logging turned on because I never would have to check any
flags or something (and yield
a smaller program), but it's not very flexible.

/ Eric



  #2  
Old July 22nd, 2005, 09:54 PM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: Class that can log what it's doing...sometimes

Eric Lila wrote:[color=blue]
> Hello, I have a class that I want to be able to output detailed information
> about what it's doing either
> to a file or to the screen. What I first tried was adding a
> std::ostream-reference member variable to
> the class and then adding a constructor that, apart from the "normal"
> arguments, also took an std::ostream&.
> I then intended to set a boolean member variable called logging to true and
> have the other member functions
> check that variable to determine whether they should perform logging or not.
> Something like:
>
> class foo
> {
> public:
> foo(args); /* logging set to false, m_out set to nothing */
> foo(args, std::ostream& out); /* logging set to true, m_out set to out */
>
> void bar(); /* log to m_out if logging == true */
>
> private:
> std::ostream& m_out;
> bool logging;
> };
>
> I thought about what would happen if I in a member function forgot to check
> the value of logging and tried
> to use m_out without it referencing something. However, I didn't get that
> far because the compiler stopped
> me saying that I must initialize m_out (which I didn't do in the first
> constructor).
> I understand why...there is no such thing as a NULL reference, it has to be
> a reference to a valid object
> and it may never be set to reference something else.
> So I was thinking having the second constructor accepting a const
> std::string& instead and that string would
> hold a file name and then I would open a file with that name and log to it.
> But that variant isn't very
> satisfying because then the user wont be able to log directly to cout or
> cerr...how do I proceed? I could solve
> it by using the preprocessor...#ifndef LOGGING, and that would probably
> yield faster code for the times
> when I dont want logging turned on because I never would have to check any
> flags or something (and yield
> a smaller program), but it's not very flexible.[/color]

I would give 'out' a default value, which is, say, a static member of
your 'foo' class, and is essentially a /dev/null kind of stream:

class foo {
foo(args, std::ostream &s = foo::nullstream);
static std::ostream& nullstream;
};

std::ostream& foo::nullstream = ??? ;

You would have to find a decent implementation of the nullstream (I am
not that good with streams to give you a one-liner for it). All you
need is a stream whose buffer would just eat all and do nothing. I bet
there are implementations of it out there.

V
  #3  
Old July 22nd, 2005, 09:54 PM
Eric Lilja
Guest
 
Posts: n/a
Default Re: Class that can log what it's doing...sometimes


"Victor Bazarov" <v.Abazarov@comAcast.net> wrote in message
news:Kg9md.10950$Ae.1990@newsread1.dllstx09.us.to. verio.net...[color=blue]
> Eric Lila wrote:[color=green]
>> Hello, I have a class that I want to be able to output detailed
>> information about what it's doing either
>> to a file or to the screen. What I first tried was adding a
>> std::ostream-reference member variable to
>> the class and then adding a constructor that, apart from the "normal"
>> arguments, also took an std::ostream&.
>> I then intended to set a boolean member variable called logging to true
>> and have the other member functions
>> check that variable to determine whether they should perform logging or
>> not. Something like:
>>
>> class foo
>> {
>> public:
>> foo(args); /* logging set to false, m_out set to nothing */
>> foo(args, std::ostream& out); /* logging set to true, m_out set to out
>> */
>>
>> void bar(); /* log to m_out if logging == true */
>>
>> private:
>> std::ostream& m_out;
>> bool logging;
>> };
>>
>> I thought about what would happen if I in a member function forgot to
>> check the value of logging and tried
>> to use m_out without it referencing something. However, I didn't get that
>> far because the compiler stopped
>> me saying that I must initialize m_out (which I didn't do in the first
>> constructor).
>> I understand why...there is no such thing as a NULL reference, it has to
>> be a reference to a valid object
>> and it may never be set to reference something else.
>> So I was thinking having the second constructor accepting a const
>> std::string& instead and that string would
>> hold a file name and then I would open a file with that name and log to
>> it. But that variant isn't very
>> satisfying because then the user wont be able to log directly to cout or
>> cerr...how do I proceed? I could solve
>> it by using the preprocessor...#ifndef LOGGING, and that would probably
>> yield faster code for the times
>> when I dont want logging turned on because I never would have to check
>> any flags or something (and yield
>> a smaller program), but it's not very flexible.[/color]
>
> I would give 'out' a default value, which is, say, a static member of
> your 'foo' class, and is essentially a /dev/null kind of stream:
>
> class foo {
> foo(args, std::ostream &s = foo::nullstream);
> static std::ostream& nullstream;
> };
>
> std::ostream& foo::nullstream = ??? ;
>
> You would have to find a decent implementation of the nullstream (I am
> not that good with streams to give you a one-liner for it). All you
> need is a stream whose buffer would just eat all and do nothing. I bet
> there are implementations of it out there.
>
> V[/color]

That seems to be a nice solution! I am googling right now to see what I
can find, no luck yet though. Lots of hits, just not found what I'm looking
for yet.

Thanks Victor

/ Eric


  #4  
Old July 22nd, 2005, 09:54 PM
Ron Natalie
Guest
 
Posts: n/a
Default Re: Class that can log what it's doing...sometimes

Eric Lilja wrote:
[color=blue]
> private:
> std::ostream& m_out;
> bool logging;
> };
>[/color]
One option to your problem would be to add a null output
device.

class NullStreamBuf : public streambuf {
int_type overflow(int_type c) { return c; }

} ;

class NullStream : public ostream
public:
NullStream() : ostream(new NullStreamBuf) { }
~NullStream() { delete rdbuf(); }

};

You can then output to the stream to your heart's content
and the data is just discarded.
  #5  
Old July 22nd, 2005, 09:55 PM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: Class that can log what it's doing...sometimes

Eric Lilja wrote:[color=blue]
> [...] I am googling right now to see what I
> can find, no luck yet though. Lots of hits, just not found what I'm looking
> for yet.[/color]

See Ron's post.
  #6  
Old July 22nd, 2005, 09:55 PM
Vyacheslav Kononenko
Guest
 
Posts: n/a
Default Re: Class that can log what it's doing...sometimes

Eric Lilja wrote:
[color=blue]
> Hello, I have a class that I want to be able to output detailed information
> about what it's doing either
> to a file or to the screen. What I first tried was adding a
> std::ostream-reference member variable to
> the class and then adding a constructor that, apart from the "normal"
> arguments, also took an std::ostream&.
> I then intended to set a boolean member variable called logging to true and
> have the other member functions
> check that variable to determine whether they should perform logging or not.
> Something like:
>
> class foo
> {
> public:
> foo(args); /* logging set to false, m_out set to nothing */
> foo(args, std::ostream& out); /* logging set to true, m_out set to out */
>
> void bar(); /* log to m_out if logging == true */
>
> private:
> std::ostream& m_out;
> bool logging;
> };
>
> I thought about what would happen if I in a member function forgot to check
> the value of logging and tried
> to use m_out without it referencing something. However, I didn't get that
> far because the compiler stopped
> me saying that I must initialize m_out (which I didn't do in the first
> constructor).
> I understand why...there is no such thing as a NULL reference, it has to be
> a reference to a valid object
> and it may never be set to reference something else.
> So I was thinking having the second constructor accepting a const
> std::string& instead and that string would
> hold a file name and then I would open a file with that name and log to it.
> But that variant isn't very
> satisfying because then the user wont be able to log directly to cout or
> cerr...how do I proceed? I could solve
> it by using the preprocessor...#ifndef LOGGING, and that would probably
> yield faster code for the times
> when I dont want logging turned on because I never would have to check any
> flags or something (and yield
> a smaller program), but it's not very flexible.
>
> / Eric
>
>[/color]
You can use something like this:
#include <iostream>

class Logger {
public:
Logger() : stream(0) {}
Logger( std::ostream &out ) { enable( out ); }

template <class T>
Logger &operator<<(T t) { if( stream ) *stream << t; return *this; }

Logger &operator<<( std::ostream&(*manip)(std::ostream&) ) { if(
stream ) *stream << manip; return *this; }

void enable( std::ostream &out ) { stream = &out; }
void disable() { stream = 0; }
private:
std::ostream *stream;
};

int main()
{
Logger log( std::cout );

log << "hello world" << std::endl;
log.disable();
log << "hello world again" << std::endl;

return 0;
}

So just create an instance of Logger as member in your class and use it
for logging in methods.
--
Regards,
Slava

  #7  
Old July 22nd, 2005, 09:55 PM
Vyacheslav Kononenko
Guest
 
Posts: n/a
Default Re: Class that can log what it's doing...sometimes

Eric Lilja wrote:
[color=blue]
> Hello, I have a class that I want to be able to output detailed information
> about what it's doing either
> to a file or to the screen. What I first tried was adding a
> std::ostream-reference member variable to
> the class and then adding a constructor that, apart from the "normal"
> arguments, also took an std::ostream&.
> I then intended to set a boolean member variable called logging to true and
> have the other member functions
> check that variable to determine whether they should perform logging or not.
> Something like:
>
> class foo
> {
> public:
> foo(args); /* logging set to false, m_out set to nothing */
> foo(args, std::ostream& out); /* logging set to true, m_out set to out */
>
> void bar(); /* log to m_out if logging == true */
>
> private:
> std::ostream& m_out;
> bool logging;
> };
>
> I thought about what would happen if I in a member function forgot to check
> the value of logging and tried
> to use m_out without it referencing something. However, I didn't get that
> far because the compiler stopped
> me saying that I must initialize m_out (which I didn't do in the first
> constructor).
> I understand why...there is no such thing as a NULL reference, it has to be
> a reference to a valid object
> and it may never be set to reference something else.
> So I was thinking having the second constructor accepting a const
> std::string& instead and that string would
> hold a file name and then I would open a file with that name and log to it.
> But that variant isn't very
> satisfying because then the user wont be able to log directly to cout or
> cerr...how do I proceed? I could solve
> it by using the preprocessor...#ifndef LOGGING, and that would probably
> yield faster code for the times
> when I dont want logging turned on because I never would have to check any
> flags or something (and yield
> a smaller program), but it's not very flexible.
>
> / Eric
>
>[/color]
You can use something like this:
#include <iostream>

class Logger {
public:
Logger() : stream(0) {}
Logger( std::ostream &out ) { enable( out ); }

template <class T>
Logger &operator<<(T t) { if( stream ) *stream << t; return *this; }

Logger &operator<<( std::ostream&(*manip)(std::ostream&) ) { if(
stream ) *stream << manip; return *this; }

void enable( std::ostream &out ) { stream = &out; }
void disable() { stream = 0; }
private:
std::ostream *stream;
};

int main()
{
Logger log( std::cout );

log << "hello world" << std::endl;
log.disable();
log << "hello world again" << std::endl;

return 0;
}

So just create an instance of Logger as member in your class and use it
for logging in methods.
--
Regards,
Slava

  #8  
Old July 22nd, 2005, 09:55 PM
Eric Lilja
Guest
 
Posts: n/a
Default Re: Class that can log what it's doing...sometimes


"Ron Natalie" wrote:[color=blue]
> Eric Lilja wrote:
>[color=green]
>> private:
>> std::ostream& m_out;
>> bool logging;
>> };
>>[/color]
> One option to your problem would be to add a null output
> device.
>
> class NullStreamBuf : public streambuf {
> int_type overflow(int_type c) { return c; }
>
> } ;
>
> class NullStream : public ostream
> public:
> NullStream() : ostream(new NullStreamBuf) { }
> ~NullStream() { delete rdbuf(); }
>
> };
>
> You can then output to the stream to your heart's content
> and the data is just discarded.[/color]

Thanks Ron, your version was a bit simpler than the one I found at
http://www.msobczak.com/prog/downloads.html (nullstream).

My constructor now takes a std::ostream& as its last argument and that
argument
has a default value:
foo(args, std::ostream& out = foo::nullstream);
where nullstream is a static data member of foo defined as:
std::ostream& LinearHashTable::nullstream = *( new NullStream());
The fact that I am using new is a bit troubling but then again, I cannot
reference a temporary. I guess I don't have to worry about deleting it,
because
it will be destroyed when the program exits and then all memory is returned
to OS, right?

Now I wonder if should check if out is actually a NullStream (and how would
I do that?) or not and set
the logging member variable accordingly or just skip that and always write
to
the stream (thinking about performance here).

/ Eric


  #9  
Old July 22nd, 2005, 09:55 PM
Ron Natalie
Guest
 
Posts: n/a
Default Re: Class that can log what it's doing...sometimes

Vyacheslav Kononenko wrote:
[color=blue]
>
> class Logger {
> public:
> Logger() : stream(0) {}[/color]

....[color=blue]
> int main()
> {
> Logger log( std::cout );
>
> log << "hello world" << std::endl;
> log.disable();
> log << "hello world again" << std::endl;
>
> return 0;
> }
>
> So just create an instance of Logger as member in your class and use it
> for logging in methods.[/color]

I don't suppose you actually tried the above. It isn't going
to work. Logger isn't derived from ostream, those endls aren't
going to work and you'll end up with other issues.
  #10  
Old July 22nd, 2005, 09:55 PM
Eric Lilja
Guest
 
Posts: n/a
Default Re: Class that can log what it's doing...sometimes


"Eric Lilja" <ericlilja_remove_this@yahoo.com> wrote in message
news:cnbb3s$3ga$1@news.island.liu.se...[color=blue]
>
> "Ron Natalie" wrote:[color=green]
>> Eric Lilja wrote:
>>[color=darkred]
>>> private:
>>> std::ostream& m_out;
>>> bool logging;
>>> };
>>>[/color]
>> One option to your problem would be to add a null output
>> device.
>>
>> class NullStreamBuf : public streambuf {
>> int_type overflow(int_type c) { return c; }
>>
>> } ;
>>
>> class NullStream : public ostream
>> public:
>> NullStream() : ostream(new NullStreamBuf) { }
>> ~NullStream() { delete rdbuf(); }
>>
>> };
>>
>> You can then output to the stream to your heart's content
>> and the data is just discarded.[/color]
>
> Thanks Ron, your version was a bit simpler than the one I found at
> http://www.msobczak.com/prog/downloads.html (nullstream).
>
> My constructor now takes a std::ostream& as its last argument and that
> argument
> has a default value:
> foo(args, std::ostream& out = foo::nullstream);
> where nullstream is a static data member of foo defined as:
> std::ostream& LinearHashTable::nullstream = *( new NullStream());
> The fact that I am using new is a bit troubling but then again, I cannot
> reference a temporary. I guess I don't have to worry about deleting it,
> because
> it will be destroyed when the program exits and then all memory is
> returned
> to OS, right?
>
> Now I wonder if should check if out is actually a NullStream (and how
> would I do that?) or not and set
> the logging member variable accordingly or just skip that and always write
> to
> the stream (thinking about performance here).
>[/color]

I am testing if the std::ostream reference passed to the constructor is in
fact
a nullstream by performing a dynamic_cast. If the cast returns NULL, I set
logging to true.
[color=blue]
> / Eric
>[/color]

/ Eric


  #11  
Old July 22nd, 2005, 09:55 PM
Rob Williscroft
Guest
 
Posts: n/a
Default Re: Class that can log what it's doing...sometimes

Ron Natalie wrote in news:41993955$0$31230$9a6e19ea@news.newshosting.co m in
comp.lang.c++:
[color=blue][color=green]
>> So just create an instance of Logger as member in your class and use it
>> for logging in methods.[/color]
>
> I don't suppose you actually tried the above.[/color]

Worked for me.
[color=blue]
> It isn't going
> to work. Logger isn't derived from ostream, those endls aren't
> going to work and you'll end up with other issues.
>[/color]

Such as?

Rob.
--
http://www.victim-prime.dsl.pipex.com/
  #12  
Old July 22nd, 2005, 09:55 PM
Victor Bazarov
Guest
 
Posts: n/a
Default Re: Class that can log what it's doing...sometimes

"Eric Lilja" <ericlilja_remove_this@yahoo.com> wrote...[color=blue]
> [...]
> I am testing if the std::ostream reference passed to the constructor is in
> fact
> a nullstream by performing a dynamic_cast. If the cast returns NULL, I set
> logging to true.[/color]

Actually comparing addresses would be simpler, I believe:

foo(blah, std::ostream& s)
: m_out(s), logging(&s != &foo::nullstream) {}

V


  #13  
Old July 22nd, 2005, 09:55 PM
Buster
Guest
 
Posts: n/a
Default Re: Class that can log what it's doing...sometimes

Eric Lilja wrote:
[color=blue][color=green]
>>Now I wonder if should check if out is actually a NullStream (and how
>>would I do that?) or not and set
>>the logging member variable accordingly or just skip that and always write
>>to
>>the stream (thinking about performance here).[/color][/color]
[color=blue]
> I am testing if the std::ostream reference passed to the constructor is in
> fact
> a nullstream by performing a dynamic_cast. If the cast returns NULL, I set
> logging to true.[/color]

If you're willing to put an if statement around every logging statement,
there's no point using a null_stream at all. You can just change the
reference to a pointer in your original class:

#include <ostream>

struct args { };

class foo
{
public:
foo (args) : m_out (0) { }
foo (args, std::ostream & out) : m_out (& out) { }

void bar ()
{
if (m_out)
{
(* m_out) << "Barring." << std::endl;
}
}
private:
std::ostream * m_out;
};

The gain in readability is small. How about something a little dirtier
for maximum performance:

#ifdef NDEBUG
#define IFDEBUG(a) 0
#else
#define IFDEBUG(a) a
#endif

#endif

#include <iostream>
#include <ostream>

int main ()
{
IFDEBUG ((std::cout << "Logging." << std::endl));
}
  #14  
Old July 22nd, 2005, 09:55 PM
Eric Fournier
Guest
 
Posts: n/a
Default Re: Class that can log what it's doing...sometimes

While using a nullstream sure provide a more elegant solution(Since it
cleans up the logging code), a simpler approach would simply have been to
switch from references to pointers, since pointers can indeed be null.

-Eric Fournier

"Eric Lilja" <ericlilja_remove_this@yahoo.com> a écrit dans le message de
news:cnb68a$2bu$1@news.island.liu.se...[color=blue]
> Hello, I have a class that I want to be able to output detailed[/color]
information[color=blue]
> about what it's doing either
> to a file or to the screen. What I first tried was adding a
> std::ostream-reference member variable to
> the class and then adding a constructor that, apart from the "normal"
> arguments, also took an std::ostream&.
> I then intended to set a boolean member variable called logging to true[/color]
and[color=blue]
> have the other member functions
> check that variable to determine whether they should perform logging or[/color]
not.[color=blue]
> Something like:
>
> class foo
> {
> public:
> foo(args); /* logging set to false, m_out set to nothing */
> foo(args, std::ostream& out); /* logging set to true, m_out set to out[/color]
*/[color=blue]
>
> void bar(); /* log to m_out if logging == true */
>
> private:
> std::ostream& m_out;
> bool logging;
> };
>
> I thought about what would happen if I in a member function forgot to[/color]
check[color=blue]
> the value of logging and tried
> to use m_out without it referencing something. However, I didn't get that
> far because the compiler stopped
> me saying that I must initialize m_out (which I didn't do in the first
> constructor).
> I understand why...there is no such thing as a NULL reference, it has to[/color]
be[color=blue]
> a reference to a valid object
> and it may never be set to reference something else.
> So I was thinking having the second constructor accepting a const
> std::string& instead and that string would
> hold a file name and then I would open a file with that name and log to[/color]
it.[color=blue]
> But that variant isn't very
> satisfying because then the user wont be able to log directly to cout or
> cerr...how do I proceed? I could solve
> it by using the preprocessor...#ifndef LOGGING, and that would probably
> yield faster code for the times
> when I dont want logging turned on because I never would have to check any
> flags or something (and yield
> a smaller program), but it's not very flexible.
>
> / Eric
>
>[/color]


  #15  
Old July 22nd, 2005, 09:55 PM
John Harrison
Guest
 
Posts: n/a
Default Re: Class that can log what it's doing...sometimes

> My constructor now takes a std::ostream& as its last argument and that[color=blue]
> argument
> has a default value:
> foo(args, std::ostream& out = foo::nullstream);
> where nullstream is a static data member of foo defined as:
> std::ostream& LinearHashTable::nullstream = *( new NullStream());
> The fact that I am using new is a bit troubling but then again, I cannot
> reference a temporary. I guess I don't have to worry about deleting it,
> because
> it will be destroyed when the program exits and then all memory is
> returned
> to OS, right?
>[/color]

No need for new. Just make nullstream an object not a reference

NullStream LinearHashTable::nullstream;

john


  #16  
Old July 22nd, 2005, 09:56 PM
Eric Lilja
Guest
 
Posts: n/a
Default Re: Class that can log what it's doing...sometimes


"John Harrison" <john_andronicus@hotmail.com> wrote in message
news:2vtr2sF2pk9djU1@uni-berlin.de...[color=blue][color=green]
>> My constructor now takes a std::ostream& as its last argument and that
>> argument
>> has a default value:
>> foo(args, std::ostream& out = foo::nullstream);
>> where nullstream is a static data member of foo defined as:
>> std::ostream& LinearHashTable::nullstream = *( new NullStream());
>> The fact that I am using new is a bit troubling but then again, I cannot
>> reference a temporary. I guess I don't have to worry about deleting it,
>> because
>> it will be destroyed when the program exits and then all memory is
>> returned
>> to OS, right?
>>[/color]
>
> No need for new. Just make nullstream an object not a reference
>
> NullStream LinearHashTable::nullstream;
>
> john
>
>[/color]

Thanks John, I've changed the type of the static data member nullstream from
std::ostream& to just NullStream.
When I first tried to define it I wrote NullStream
LinearHashTable::nullstream = NullStream();
I didn't realise that that code tried to use the copy constructor (which, as
you know, is private for ostreams), I changed to
your variant which invokes the default constructor.

/ Eric


  #17  
Old July 22nd, 2005, 09:56 PM
Vyacheslav Kononenko
Guest
 
Posts: n/a
Default Re: Class that can log what it's doing...sometimes

Eric Lilja wrote:
[color=blue]
>
> Now I wonder if should check if out is actually a NullStream (and how would
> I do that?) or not and set
> the logging member variable accordingly or just skip that and always write
> to
> the stream (thinking about performance here).[/color]

Look at my solution there is no such problem there.[color=blue]
>
> / Eric
>
>[/color]


--
Regards,
Slava

  #18  
Old July 22nd, 2005, 09:56 PM
Vyacheslav Kononenko
Guest
 
Posts: n/a
Default Re: Class that can log what it's doing...sometimes

Ron Natalie wrote:
[color=blue]
> Vyacheslav Kononenko wrote:
>[color=green]
>>
>> class Logger {
>> public:
>> Logger() : stream(0) {}[/color]
>
>
> ...
>[color=green]
>> int main()
>> {
>> Logger log( std::cout );
>>
>> log << "hello world" << std::endl;
>> log.disable();
>> log << "hello world again" << std::endl;
>>
>> return 0;
>> }
>>
>> So just create an instance of Logger as member in your class and use
>> it for logging in methods.[/color]
>
>
> I don't suppose you actually tried the above. It isn't going
> to work. Logger isn't derived from ostream, those endls aren't
> going to work and you'll end up with other issues.[/color]
You are wrong. You just did not pay attention to this:
Logger &operator<<( std::ostream&(*manip)(std::ostream&) ) { if( stream
) *stream << manip; return *this; }

What are other issues?
--
Regards,
Slava

  #19  
Old July 22nd, 2005, 09:56 PM
Eric Lilja
Guest
 
Posts: n/a
Default Re: Class that can log what it's doing...sometimes


"Vyacheslav Kononenko" <vyacheslav@NOkononenkoSPAM.net> wrote in message
news:%iomd.1942$Ny6.3251@mencken.net.nih.gov...[color=blue]
> Eric Lilja wrote:
>[color=green]
>>
>> Now I wonder if should check if out is actually a NullStream (and how
>> would I do that?) or not and set
>> the logging member variable accordingly or just skip that and always
>> write to
>> the stream (thinking about performance here).[/color]
>
> Look at my solution there is no such problem there.[color=green]
>>
>> / Eric[/color]
>
>
> --
> Regards,
> Slava
>[/color]

I will, thanks for your help, Slava. I'm always interested in different
approaches to problems. Even if I choose one
over the other this time it might be the other way around next time.

/ Eric


  #20  
Old July 22nd, 2005, 09:57 PM
Ron Natalie
Guest
 
Posts: n/a
Default Re: Class that can log what it's doing...sometimes

[color=blue]
> You are wrong. You just did not pay attention to this:
> Logger &operator<<( std::ostream&(*manip)(std::ostream&) ) { if( stream
> ) *stream << manip; return *this; }
>[/color]

Sorry, you are right.[color=blue]
> What are other issues[/color]

It means you can't use this stream with things expecting
ostream. If your entire world is limitted to operator<<
that's fine.
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.