Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a better way than #if DebugMode for logging

I'm making a c++ library thats going to be P/Invoked from c#, so i am unable to breakpoint/debug the c++ side of things. So i decided to add logging so i can see if anything goes wrong and where it happens. I add a #define DebugMode 1 in order to determine if i am to log or not. First of all i'm not very good at c++ but i know enough to get around. So my questions are:

  1. Is there a better way than wrapping #if DebugMode #endifs around every Log call? I could simply do that inside the Log method and just return if logging isn't enabled but won't that mean then all those logging strings will be in the assembly?

  2. How can i emulate what printf does with its "..." operator enabling me to pass something like Log("Variable x is {0}", x);

  3. Is there any tricks such as getting the line number or stack trace information of some sort that i can use in the log?

Thanks!

like image 971
Daniel Avatar asked Apr 15 '10 04:04

Daniel


1 Answers

One simple way is to just define a macro that does nothing if you're not in debug mode. That way you don't have to wrap every call in an #ifdef.

A simple implementation might be:

#if DebugMode
#define MY_LOG(string, ...) printf(string, __VA_ARGS__)
#else
#define MY_LOG(string, ...)
#endif

There are other ways and libraries (such as boost) but this will get you something quickly.

like image 131
i_am_jorf Avatar answered Sep 18 '22 13:09

i_am_jorf