0

a.h

#define print_line(fmt,...) do{\
    struct timespec _t; clock_gettime(CLOCK_MONOTONIC, &_t);\
    printf("%ld.%ld %s:%d: " fmt "\n", _t.tv_sec + _t.tv_nsec / 1000000, __func__, __LINE__, ##__VA_ARGS__);\
}while(0)

a.c

struct timespec timeval;

print_line(timeval)

getting an error : error: expected ')' before .

Andremoniy
  • 34,031
  • 20
  • 135
  • 241
Angus
  • 12,133
  • 29
  • 96
  • 151
  • Did you include `time.h`? – alk Jan 24 '13 at 07:54
  • The error appears somewhere else; mentioned code will not cause such problems. Show us full source of `a.h` and `a.c` if it is possible. – Andremoniy Jan 24 '13 at 07:56
  • 1
    It seems that you try to use variadic define without `-std=c99` option. However, if you'll add it, you'll get an error about `timespec ` - http://stackoverflow.com/questions/3875197/std-c99-wtf-on-linux – borisbn Jan 24 '13 at 08:04
  • you need a ; after a while(0) ; or not? – qPCR4vir Jan 24 '13 at 08:08
  • please, show a litter more of a.c – qPCR4vir Jan 24 '13 at 08:26
  • @qPCR4vir No, there shouldn't be a semi-colon. Consider `if (...) print_line(timeval); else ...`. While this looks like valid C++ code, it would give a syntax error. [This discussion](http://stackoverflow.com/questions/154136/do-while-and-if-else-statements-in-c-c-macros) touches on it, but not directly. – Bernhard Barker Jan 24 '13 at 08:32
  • @Dukeling. Rigth, but Angus show he use the macro Without the ; at end – qPCR4vir Jan 24 '13 at 08:42
  • @Angus : And struct timespec _t; clock_gettime(CLOCK_MONOTONIC, &_t); is test data too? – qPCR4vir Jan 24 '13 at 12:07

2 Answers2

1

The "..." fmt "..." that appears in your #define is only valid if fmt is a string literal ("...").

I suspect you want something more along the lines of:

 #define print_line(fmt,...) do{\
    char str[100]; \
    sprintf(str, "%%ld.%%ld %%s:%%d: %s\n", fmt);\
    printf(str, 1.0 / 1000000, __func__, __LINE__, ##__VA_ARGS__);\
}while(0)

Test:

char *i = "hello%s";
print_line(i, "abc");

Another thing - C has no idea how to convert struct timespec to string so you'll need to do something like: (if timespec starts with anything other than a null-terminated char array, it won't work)

struct timespec
{
   char abc[100];
};
struct timespec ts;
sprintf(ts.abc, "hello%%s"); // for testing
print_line(&ts, "abc");

One more thing - "%ld.%ld" appears to print out rubbish and I'm not entirely sure why. Maybe you want "%f" instead.

Bernhard Barker
  • 54,589
  • 14
  • 104
  • 138
0

I assumed you whant a debbuging tool with just print the name of the variable you are about to use, with the time in sec and millisec, source code location and some optional comment.

#define print_line(fmt,...) do{\
    struct timespec _t; clock_gettime(CLOCK_MONOTONIC, &_t);\
    printf("%ld s %ld ms %s:%d: %s" " fmt " "\n", _t.tv_sec , _t.tv_nsec / 1000000, __func__, __LINE__, ##__VA_ARGS__);\
}while(0)

use:

 struct timespec timeval;
 print_line(timeval) ;
qPCR4vir
  • 3,521
  • 1
  • 22
  • 32
  • If you want to print the name of the variable, you'll change `fmt` to `#fmt` in the question (your change will just print the string "fmt"), but I assumed `fmt` stood for `format` (and looking at what the `#define` does supports that notion), thus OP probably wants more than just the variable name. – Bernhard Barker Jan 24 '13 at 10:13
  • Hmm… !! OK, I agree. Thank. But then what he originally wrote is very far from that, in both the macro and how he uses it. I assumed he wanted something else. – qPCR4vir Jan 24 '13 at 10:25