51

This might sound like an interview question but is actually a practical problem.

I am working with an embedded platform, and have available only the equivalents of those functions:

  • printf()
  • snprintf()

Furthermore, the printf() implementation (and signature) is likely to change in the near future, so calls to it have to reside in a separate module in order to be easy to migrate later.

Given that, can I wrap logging calls in some function or macro? The goal is that my source code calls THAT_MACRO("Number of bunnies: %d", numBunnies); in a thousand places, but calls to the above functions are seen only in a single place.

Compiler: arm-gcc -std=c99

Edit: just to mention, but post 2000 best practices and probably a lot earlier, inline functions are far better than macros for numerous reasons.

Vorac
  • 8,726
  • 11
  • 58
  • 101

8 Answers8

90

There are 2 ways to do this:

  1. Variadric macro

    #define my_printf(...) printf(__VA_ARGS__)
    
  2. function that forwards va_args

    #include <stdarg.h>
    #include <stdio.h>
    
    void my_printf(const char *fmt, ...) {
        va_list args;
        va_start(args, fmt);
        vprintf(fmt, args);
        va_end(args);
    }
    

There are also vsnprintf, vfprintf and whatever you can think of in stdio.

Sergey L.
  • 21,822
  • 5
  • 49
  • 75
  • Moreover, you can find some documentation about macros (and variadic macros) [here](http://gcc.gnu.org/onlinedocs/cpp/Macros.html). – jmlemetayer Dec 17 '13 at 16:41
  • @Roddy Yes if you want to forward all arguments. I would discourage it as you can't define a no-op macro in that way. With a function-like macro you can always make it no-op if you have to. – Sergey L. Dec 17 '13 at 16:42
  • I wish someone edited this answer, so that I can remove my upvote. I DO NOT have vprintf or other fancies. Embedded, you know. – Vorac Dec 18 '13 at 12:37
  • Sorry for the tone. I really can't use the standard libraries for this. It's a custom ARM-based platform. – Vorac Dec 18 '13 at 13:33
  • I've been trying various methods like this, but 1. I can't make Macros that work through namespaces; `#define Log::WriteLine(_Format, ...) printf(_Format, __VA_ARGS__)` doesn't work. And 2. It doesn't show the `%s` etc. in lime green, or validate input... and is therefore useless as a substitute. If there any way to get a custom printf that shows the `%s` etc. in lime green, with the validation that is essential for use? – Apache Jan 18 '20 at 16:25
  • @Apache This question was asked with respect to C, not C++. In C++ you should use a variadic template to forward ``template auto WriteLine(const char * _Format, T &&... args) { return printf(_Format, std::forward(args)...); }`` – Sergey L. Feb 10 '20 at 18:33
  • @SergeyL.Will that show the syntax highlighting correctly as well, with the `%s` etc, in lime green? That's the essential thing. Without it, it's useless to mask the `printf` function. – Apache Feb 11 '20 at 17:19
  • why is fmt an argument of va_start? – ThorSummoner May 14 '20 at 01:28
  • oh, I'm guessing but I think va_start's "last" argument means the last stack arguments when examined right-to-left, eg "stop parsing va args when we get to fmt" – ThorSummoner May 14 '20 at 01:48
53

Since you can use C99, I'd wrap it in a variadic macro:

#define TM_PRINTF(f_, ...) printf((f_), __VA_ARGS__)
#define TM_SNPRINTF(s_, sz_, f_, ...) snprintf((s_), (sz_), (f_), __VA_ARGS__)

since you didn't say that you have vprintf or something like it. If you do have something like it, you could wrap it in a function like Sergey L has provided in his answer.


The above TM_PRINTF does not work with an empty VA_ARGS list. At least in GCC it is possible to write:

#define TM_PRINTF(f_, ...) printf((f_), ##__VA_ARGS__)

The two ## signs remove the excess comma in front of them them if __VA_ARGS__ is empty.

hdorio
  • 12,902
  • 5
  • 34
  • 34
ldav1s
  • 15,885
  • 2
  • 53
  • 56
  • 2
    `TM_PRINTF("Starting ei-oh!");` yields `error: expected expression before ')' token`. Without the format string argument it works. Do variadic arguments need to be non-zero in number? – Vorac Dec 18 '13 at 12:50
  • It look that the above error cannot be fixed without gcc extensions: http://gcc.gnu.org/onlinedocs/cpp/Variadic-Macros.html – Vorac Dec 18 '13 at 13:07
  • You can solve the issue removing the _f part and keeping only the varidac thing. Work without issue for me but I don't know if it is out of standard – Lesto Sep 30 '16 at 10:12
  • @lesto, yes it "works", but removing `f_` would make `TM_PRINTF()` allowable. If that string were in the code, the compiler would complain later about about `printf()` which doesn't match it's function signature. It's better to make the compiler complain about `TM_PRINTF` in this case, since that's more visible. – ldav1s Sep 30 '16 at 14:39
  • @ldav1s I didn't get what you meant by a "variadic" macro. How did magically those ellipsis `...` forward them to `__VA_ARGS__` ? – John Strood Oct 29 '16 at 17:04
  • @Djack a variadic macro is one that uses the `...` notation in the `#define` parameters list and usually `__VA_ARGS__` in the replacement text. The C preprocessor (especially before C99) isn't as sophisticated as other languages, barely more than just simple text replacement. In this case it's got to do more than simple text replacement -- it's got to buffer up all the tokens when the macro gets invoked. For example, in the invocation `TM_PRINTF(foo, bar, baz)` the preprocessor sets a replacement variable `f_` to the token `foo` and `__VA_ARGS__` to the tokens `bar`, `,` and `baz`. – ldav1s Oct 29 '16 at 19:31
  • Should it work with cygwin? Because I'm not able to make it works! However, the second way of @Sergey L. worked. – ferdepe Feb 13 '17 at 14:56
  • @ferdepe You need to be using at least C99 to compile using this syntax (.e.g. `gcc -std=c99`) otherwise it won't work. The cygwin _should_ work if it has C99 support. – ldav1s Feb 13 '17 at 15:41
11

If you can live with having to wrap the call in two parentheses, you can do it like this:

#define THAT_MACRO(pargs)    printf pargs

Then use it:

THAT_MACRO(("This is a string: %s\n", "foo"));
           ^
           |
          OMG

This works since from the preprocessor's point of view, the entire list of arguments becomes one macro argument, which is substituted with the parenthesis.

This is better than just plain doing

#define THAT_MACRO printf

Since it allows you to define it out:

#define THAT_MACRO(pargs)  /* nothing */

This will "eat up" the macro arguments, they will never be part of the compiled code.

UPDATE Of course in C99 this technique is obsolete, just use a variadic macro and be happy.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • 1
    OMG thanks for the great answer. I guess you'll be getting upvotes from all the poor C89 programmers out there over the next several years :) – Vorac Dec 18 '13 at 12:51
  • 2
    An important side effect of this technique: all calls to `THAT_MACRO` will need double parentheses, even with single argument calls, e.g. `THAT_MACRO(("Foo Bar"))`. --With love, a poor C89 programmer from several years later. – drmuelr Jul 19 '16 at 16:02
11
#define TM_PRINTF(f_, ...) printf((f_), ##__VA_ARGS__)

The ## token will enable the usage TM_PRINTF("aaa");

Felipe Lavratti
  • 2,887
  • 16
  • 34
  • It's like magic! Saved me a lot of duplicated macros for each case! How does that ## work? – natenho Feb 18 '16 at 17:58
  • Yeah, this worked while the accepted answer did not. Thanks! – sudo Feb 22 '17 at 01:57
  • 5
    Note that this is a compiler extension, not C99 standard. Basically, compiler writers, smart people that they are, recognised this oversight in the standard, and found a workaround. The caveat is that it's not guaranteed to work on every C99 compliant compiler, and some compilers might use different syntax for the same thing. – Jostikas May 11 '17 at 07:32
4
#define PRINTF(...) printf(__VA_ARGS__)

This works like this:

It defines the parameterized macro PRINTF to accept (up to) infinite arguments, then preprocesses it from PRINTF(...) to printf(__VA_ARGS__). __VA_ARGS__ is used in parameterized macro definitions to denote the arguments given ('cause you can't name infinite arguments, can you?).

EKons
  • 887
  • 2
  • 20
  • 27
2

Limited library? Embedded system? Need as much performance as possible? No problem!

As demonstrated in this answer to this question, you can use assembly language to wrap function which do not accept VA_LIST into ones that do, implementing your own vprintf at little cost!

While this will work, and almost certainly result in the performance as well as abstraction you want, I would just recommend you get a more feature filled standard library, perhaps by slicing parts of uClibc. Such a solution is surely to be a more portable and overall more useful answer than using assembly, unless you absolutely need every cycle.

That's why such projects exist, after all.

Community
  • 1
  • 1
Alice
  • 3,958
  • 2
  • 24
  • 28
  • @ΈρικΚωνσταντόπουλος Yes and that's one of the very few systems where ASM would be an acceptable trade off of programmer time. – Alice Apr 17 '16 at 01:16
2

This is a slightly modified version of @ldav1's excellent answer which prints time before the log:

#define TM_PRINTF(f_, ...)                                                                            \
{                                                                                                 \
    struct tm _tm123_;                                                                            \
    struct timeval _xxtv123_;                                                                     \
    gettimeofday(&_xxtv123_, NULL);                                                               \
    localtime_r(&_xxtv123_.tv_sec, &_tm123_);                                                     \
    printf("%2d:%2d:%2d.%d\t", _tm123_.tm_hour, _tm123_.tm_min, _tm123_.tm_sec, _xxtv123_.tv_usec); \
    printf((f_), ##__VA_ARGS__);                                                                  \
};
BHP
  • 443
  • 4
  • 13
-1

Below is an example wrapper for the vsprintf() function, from https://www.cplusplus.com/reference/cstdio/vsprintf/:

#include <stdio.h>
#include <stdarg.h>

void PrintFError ( const char * format, ... )
{
   char buffer[256];
   va_list args;
   va_start (args, format);
   vsprintf (buffer,format, args);
   perror (buffer);
   va_end (args);
}

Following the example above, one can implement wrappers for other desired functions from <stdio.h>.

Vladimir
  • 301
  • 3
  • 12