Home > other >  C will be formatted string passed as a parameter to a function
C will be formatted string passed as a parameter to a function

Time:04-18

Well, this really should not be difficult, but after several attempts, I didn't find any solution.
How to format the string passed to the function of line 1? I want to have a simple way, I can be used to log/debugging purposes, as shown in the following:

Void the debug (string s) {
If (DEBUG) cerr & lt; }

I want to be able to send a formatted string to this function, as shown in the following:

The debug (reading "% s" % fname);

My main goal is to add as much as possible in the debug method of function, so every time I want to put the things written to stderr, I can make life easier.
Printf solution seems to need a lot of code for simple formatting, and boost: : the format seems to return some fuzzy type, cannot be easily used as a parameter and not always contains some. STR ().
Whether I am too lazy to be a C programmer, or have a complete method can achieve the goal of I have not found?

CodePudding user response:

You can use __VA_ARGS__ around fprintf defines a macro:

# define DBGLOG (format,... ) if (DEBUG) {fprintf (stderr, "% s, % d", __FUNCTION__ and __LINE__); Fprintf (stderr, format, # # __VA_ARGS__); }

If the DEBUG to true, then the output function name and line number, then you want to print the contents of the output.
For example:

DBGLOG (" % d \ n ", some_integer);

This will add integer value in front of the function and line number and print it out.

CodePudding user response:

Oh, my god, I can only say that the method is very useful upstairs
  • Related