This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
selective function call
- From: Hayim Shaul <hayim at iportent dot com>
- To: gcc-help at gcc dot gnu dot org
- Date: Tue, 29 Nov 2005 12:13:50 +0200
- Subject: selective function call
Hi all,
I am trying to write a smart logger that can be activated/deactivated at
run-time. I have a print function for the logger that gets va_list:
void tLogger::print(const char *format, ...) {
if (!is_activated())
return;
va_list marker;
va_start(marker, format);
etc. etc.
Trouble is with performance. When I call the print function while the
logger is not activated arguments are still passed to the function. This
is very time-consuming.
Is there a way to avoid passing unnecessarily extra arguments?
Obviously I can write my code as:
if (logger->is_activated())
logger->print("%d\n", ackerman(5,5));
But this is very tiring.
I created a MACRO
#define LOG if (logger->is_activated()) logger->print
and use it:
LOG("%d\n", ackerman(5,5));
but this seems to me a very fragile solution.
I tried an inline function (hoping for the optimizer would kick in) but
it was no good.
Is there an more elegant solution for this?
Hayim.