Boost logo

Boost Users :

From: Richard Howells (richard_at_[hidden])
Date: 2004-12-22 06:30:09


Vladimir is right.

You've seen the problems and crashes that leaving type checking to the
programmer causes. You need the compiler to do the checking for you.

I suggest using the same kind of scheme as the iostreams library. You might
want to do more design than this - but you could start by adding a small
family of global functions that look like... (you'll have to work up any
varargs support stuff - I don't remember it)

MyLogger & operator << (MyLogger & logger, int i)
{
        logger.dump("%d", i) ;
}

MyLogger & operator << (MyLogger & logger, char * str)
{
        logger.dump("%s", str) ;
}

This will allow you to write...

MyLogger Log ....

Int I ;

Log << 10 << "abc" << I ;

...it will be (mostly) type safe - the type dangerous stuff is isolated to
one place per output type - and you can extend it as you go. You'll likely
need extra formatting stuff and that's where boost::format may help you.

HTH - Richard

-----Original Message-----
From: boost-users-bounces_at_[hidden]
[mailto:boost-users-bounces_at_[hidden]] On Behalf Of Vladimir Prus
Sent: 22 December 2004 10:45
To: boost-users_at_[hidden]
Subject: [Boost-users] Re: Q: boost::format : can formatting
variablearguments lists be done ?

Dan Dimerman wrote:

> Currently I have a piece of code that logs messages much in the same way
> as printf() :
>
> void MyLogger::dump( const std::string& format, va_list& list )
> {
> const unsigned short buf_size = 1024;
> char buffer[ buf_size ]; ::memset( buffer, 0, buf_size );
> ::_vsnprintf( buffer, buf_size - 1, format.c_str(), list);
>
> /.../
> }
>
> After many headaches (and crashes) due to mistaken format specification
> fields, I thought to upgrade my class's API to use boost::format, but
> alas, I couldn't find the way to feed the format object from a va_list.
> Is there a way?

Why do you think that boost::format together with va_list will be any safer
than vsnprintf? If boost::format accepted va_list, it would have to guess
types from format string, which will be as dangerous as it is for
vsnprintf.

- Volodya

_______________________________________________
Boost-users mailing list
Boost-users_at_[hidden]
http://lists.boost.org/mailman/listinfo.cgi/boost-users

-- 
No virus found in this incoming message.
Checked by AVG Anti-Virus.
Version: 7.0.296 / Virus Database: 265.6.3 - Release Date: 21/12/2004
 
-- 
No virus found in this outgoing message.
Checked by AVG Anti-Virus.
Version: 7.0.296 / Virus Database: 265.6.3 - Release Date: 21/12/2004
 

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net