Thiago Alves' Blog

A C/C++ feature that helped me to log better

Thiago Alves

I read an article some time ago where it claims that you take at least ten years to learn C/C++ entirely. I don't remember if was Bjarne Stroustrup or someone else that said that but I completely agree!

I code professionally for about eight years, and most of my career was developing in C/C++; still, I don't consider I know everything about this terrific language. One proof is that I found an interesting feature available regarding #define macros!

Let's LOG

One important thing of every software is its logs. It's used to trace program's flow or even debug on some cases where you cannot use GDB/DBX.

One important thing to put on your logs is the file name and line number where it occurs. On my early days, I thought to have found a smart way to do it!

Yes, but if you start working with a bunch of people, you'll realize that use these standard macros might confuse some folks, and others tend to use plain printf just because they don't know how to use it.

Now, how to solve THIS problem?

#define Macros to save us

It turns out there is a feature on standard C called Variadic Macros that I didn't know before this week that solves this issue and makes log functions elegant and efficient. I'll point you again to the The GNU C Preprocessor on the Variadic Macros page so you can read the details, but in a glimpse here is its syntax:

#define MACRO_NAME(arg, ...) somefunc(arg, __VA_ARGS__)

One thing to notice though is with this syntax you must pass at least one argument as variable arguments due to the comma on your macro definition. But don't look to this post with this face! There is a second syntax of Variadic Macros that will calm you down:

#define MACRO_NAME(arg, ...) somefunc(arg, ##__VA_ARGS__)

With those double pounds before the __VA_ARGS__ keyword, the C preprocessor will know if you don't add variable arguments and will take the comma out.