RE: [Boost-users] Re: Q: boost::format : can formatting variablea rguments lists be done ?
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 /.../ ...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
Could you do Logger<DBG> MyLogger; MyLogger.print(); ? -----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Dan Dimerman Sent: 22 December 2004 13:03 To: boost-users@lists.boost.org Subject: [Boost-users] Re: Q: boost::format : can formatting variablearguments lists be done ? Richard Howells wrote: likely
need extra formatting stuff and that's where boost::format may help you.
I was thinking about something like: MyLogger::print( const boost::format& f ) and since all format's operator %() return a boost::format, I could with this single function reap all the types handled by it at once. I use a function rather than the operator << because they are actually template functions, with the parameter being the severity. So for _DEBUG versions, the MyLogger::print< DBG >() function instantiates to a proper call to print, while in _RELEASE versions it instantiates to an empty function. I just didn't see a clean way to do this with operators. Dan _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Foster, Gareth wrote:
Could you do Logger<DBG> MyLogger; MyLogger.print(); ?
It wont have the same effect, for if I parametrize the class itself, I will end up having as many actual instances as severity levels. Parametrizing the print function allows me to use the same logger (with its corresponding devices such as files, console, etc.) for different levels. Since the actual function to be called is resolved at compile time, I get to avoid costly constructions (mostly string concatenation and such) for debug purposes to be carried in the release version (assuming the compiler notices...). Dan
le Wednesday 22 December 2004 17:25, dimerman@netvision.net.il écrivit :
Foster, Gareth wrote:
Could you do Logger<DBG> MyLogger; MyLogger.print(); ?
It wont have the same effect, for if I parametrize the class itself, I will end up having as many actual instances as severity levels. Parametrizing the print function allows me to use the same logger (with its corresponding devices such as files, console, etc.) for different levels. Since the actual function to be called is resolved at compile time, I get to avoid costly constructions (mostly string concatenation and such) for debug purposes to be carried in the release version (assuming the compiler notices...).
what about you put the dbg-level dependant code in the formatter object, like : // a no-op formatter object : // (formatting, and outputting, is a no-op at compile-time) struct dummy_format { template<class T> dummy_format& operator%(const T& x) { return *this; } } inline ostream& operator<<(ostrem& os, dummy_format const& x) { return os; } // a level-based selecter : template<int> struct DbgFormatType { typedef boost::format type; } template<> struct DbgFormat<0> { typedef dummy_format type; } // a level-dependant formatter : template<int n> struct DbgFormat { typedef typename DbgFormatType<n>::type type; type fmter; DbgFormat(const char* s) : fmter(s) {} template<class T> type operator%(const T & x) const { return fmter % x; } } And in the code you'd do : (assuming the function dest() returns an ostream. any other semantics for output would work..) myLogger.dest() << DbgFormat<level>(fmt_str) % x1 % x2 % "truc"; if you want to put all log-related stuff (destination AND formatting policy) into a signle logger object, you could have : template<int n> class MyLogger { .. DbgFormat<n> fmter(const char* s) { return DbgFormat<n>(s); } ... } and then do : myLogger.dest() << myLogger.fmter(fmt_str) % x1 % x2 % "truc"; Would that be fitting your needs ? -- Samuel
participants (3)
-
Dan Dimerman
-
Foster, Gareth
-
Samuel Krempp