Reputation: 12631
#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)
struct timespec timeval;
print_line(timeval)
getting an error : error: expected ')' before .
Upvotes: 0
Views: 1673
Reputation: 3571
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) ;
Upvotes: 0
Reputation: 55649
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.
Upvotes: 1