How to make Boost.Format fault tolerant?
A common case for using Boost.Format is in the production of error or warning messages. So, these don't get run on a normal use of the program, and problems with the arguments will go unnoticed until there _is_ an error, in which case I get (at best) no error message, or worse, turn a log-only warning statement into an exception. Does Format already have some features to make it not throw exceptions? Idealy, for messages that go into a log or are used to diagnose some program bug, any information is better than none and I'd like to see what it did get as best as it can, and not worry about how pretty it may be. So missing arguments use a default placeholder text instead, and extra arguments without specifiers in the format string just get appended with spaces in between, and any formatting spec error just goes back to default string, and any error during that gives a placeholder but the other arguments are processed. —John
A common case for using Boost.Format is in the production of error or warning messages. So, these don't get run on a normal use of the program, and problems with the arguments will go unnoticed until there _is_ an error, in which case I get (at best) no error message, or worse, turn a log-only warning statement into an exception.
Does Format already have some features to make it not throw exceptions?
http://www.boost.org/doc/libs/1_48_0/libs/format/doc/format.html#exceptions
On 2/10/2012 1:14 AM, Igor R wrote:
A common case for using Boost.Format is in the production of error or warning messages. So, these don't get run on a normal use of the program, and problems with the arguments will go unnoticed until there _is_ an error, in which case I get (at best) no error message, or worse, turn a log-only warning statement into an exception.
Does Format already have some features to make it not throw exceptions?
http://www.boost.org/doc/libs/1_48_0/libs/format/doc/format.html#exceptions
It seems to suppress exceptions for some things, but a format string like: "Funny format string: 25% off now! %z %x" still throws an exception "boost::bad_format_string: format-string is ill-formed" when exceptions(boost::io::no_error_bits) is used. —John
On 2/11/2012 1:43 AM, John M. Dlugosz wrote:
It seems to suppress exceptions for some things, but a format string like: "Funny format string: 25% off now! %z %x" still throws an exception "boost::bad_format_string: format-string is ill-formed" when exceptions(boost::io::no_error_bits) is used.
The example could be more enlightening… boost::format my_fmt(const std::string & f_string) { using namespace boost::io; format fmter(f_string); fmter.exceptions( all_error_bits ^ ( too_many_args_bit | too_few_args_bit )); return fmter; } doesn't work for errors in the format string itself. It would be better illustrated along the lines of: boost::format my_fmt(const std::string & f_string) { using namespace boost::io; format fmter; // don't give it the string until after the exceptions are set fmter.exceptions( whatever ); fmter.parse (f_string); return fmter; }
On 2/11/2012 1:43 AM, John M. Dlugosz wrote:
It seems to suppress exceptions for some things, but a format string like: "Funny format string: 25% off now! %z %x" still throws an exception "boost::bad_format_string: format-string is ill-formed" when exceptions(boost::io::no_error_bits) is used.
The example could be more enlightening…
boost::format my_fmt(const std::string & f_string) { using namespace boost::io; format fmter(f_string); fmter.exceptions( all_error_bits ^ ( too_many_args_bit | too_few_args_bit )); return fmter; }
doesn't work for errors in the format string itself.
It would be better illustrated along the lines of:
boost::format my_fmt(const std::string & f_string) { using namespace boost::io; format fmter; // don't give it the string until after the exceptions are set fmter.exceptions( whatever ); fmter.parse (f_string); return fmter; } You could just wrap the format instance creation in a try/catch which would eat away the exception and return a static instance of format, one
Il 02/11/2012 08:52 AM, John M. Dlugosz ha scritto: that ignores al arguments except the line and the file and prints something like "format error in format string at line L in file F" Instead of the static instance you could also generate a dynamic one including somehow informations about the exception itself, but this could throw again and I advise against it... LC -- Leo Cacciari Aliae nationes servitutem pati possunt populi romani est propria libertas
participants (3)
-
Igor R
-
John M. Dlugosz
-
Leo Cacciari