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.
....
What do you people think? Do you see alternative strategies?
I realise that this is exactly what Aspect Oriented Programming is all
about, but I have a low level library rather than an application or a
framework; the decision to use such a strategic approach is best left
to the users of my library.
Hi Nicola,
For low-level libs a function pointer might be the appropriate
alternative. Ie, the low-level functions have an additional parameter
where the client /might/ pass in a function pointer to a trace or
logging function
// interface
typedef void (*log_cb)(const char*);
void foo(log_cb cb=0);
// implementation
struct Logger {
Logger(const char* name, log_cb cb=0) : name_(name), cb_(cb) {}
~Logger() { if (cb_) cb_(name_); }
const char* name_;
log_cb cb_;
};
void foo(log_cb cb) { Logger logger("foo", cb); }
// usage
void trace(const char* text) { cout << text << endl; }
int main() {
foo();
foo(trace);
return 0;
}
Several issues still need to be considered
- above Logger logs on every, ie even on exceptional, exit
- if logging is lib-wide then it's tedious to pass the fp to every func
- ...
but at least the user has now already an easy way to "register" its
personal logging function.
Ali
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]