Reputation: 115
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:
Is there a better way than wrapping #if DebugMode #endif
s 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?
How can i emulate what printf does with its "..." operator enabling me to pass something like Log("Variable x is {0}", x);
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!
Upvotes: 6
Views: 3235
Reputation: 3555
I recommend using Pantheios, and then you never need to care about DEBUG/!DEBUG. The library uses C++ templates to make sure that no performance cost is paid when logging is not enabled. It's also 100% type-safe and extensible to user defined types.
bar_t bar;
pantheios::log_DEBUG("foo ", bar, " was doing something you should remember");
Check out their performance page for further information.
Upvotes: 2
Reputation: 490178
If the condition is a compile-time constant, so your code (after preprocessing) works out to something like:
if (0)
do the logging
Then the compiler will generally be smart enough to strip out the dead code, including the strings that you passed to it (unless you also used the strings in other code that wasn't stripped out, of course).
Code that acts like printf is pretty simple:
#include <stdarg.h>
void log(char const &fmt, ...) {
if (do_logging) {
va_list args;
va_start(args, fmt);
vfprintf(output_file, fmt, args);
}
}
In a macro (it's important that it be in the macro, not the called function) you can use __FILE__
and __LINE__
for the current line number and source file name to put in the log. With the code above, you'd (probably) want to pass those before the format string.
Upvotes: 2
Reputation: 54600
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.
Upvotes: 5
Reputation: 43321
What about debugging C++ library? In the C++ library Project Properties, Debugging, select C# client in the Command field, and start debugging.
About logging, do you ask about C++ or C# logging? Bot have preprocessor constants defined only in Debug configuration, you can use it.
Upvotes: 0