Re: Providing diagnostic information upon successful completion
Nicola Musatti wrote:
I'm looking for advice on how to provide diagnostic
information related to a function call that executed
successfully.
A little background: I'm writing a library which I don't want to
encumber with logging facilities, but for which I want to provide
diagnostic information about the outcome of calls to specific
functions. When calls fail it's easy, diag info is packaged into the
exceptions that I throw; when calls succeed but still need to provide
some information I'm not sure how to proceed. I see a few alternatives:
1) Throw a "warning" exception, which may contain the function's return
value
when one is expected;
An exception for a normal return value?
2) Return a special object which combines the return value with
optional diag info,
in which case I can
2a) Force inspection of diag info, by using something similar to
Barton & Nackman's
Fallible class, where a conversion operator throws if there's
info available and it
hasn't been inspected;
2b) Leave the user the option whether to act upon diag info or not.
I actually started implementing 1), but I'm beginning to feel that even
2a) may be a bit too strong. What do you people think? Do you see
alternative strategies?
Given that it's diagnostic info, and the return value is valid,
I'd only throw (or assertion failure) if the diagnostic info
hadn't been read before the last copy of the Fallible object had
been destructed, supposing 2a. (You could use something like
the boost::shared_ptr counters to keep track of the number of
copies.)
FWIW, my own implementation of Fallible has been extended to
handle additional status information (rather than simply
true/false). It only asserts if there is an attempt to read the
value and the status is invalid (as determined by a traits
class), so it implements 2b exactly.
It shouldn't be too difficult to extend it to implement 2a, if
that is what you want. (Without knowing the context, it's
difficult to judge, but I'd tend to prefer 2b over the other
solutions. At least, it's what I needed---which explains why my
Fallible implements it, and not one of the others.)
The code is available at:
http://kanze.james.neuf.fr/code-en.html. In subsystem Basic, if
you want to read the documentation first.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient?e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S?mard, 78210 St.-Cyr-l'?cole, France, +33 (0)1 30 23 00 34
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]