Fix variadic forwarding in sway_assert

_sway_assert is a variadic function which tries
to delegate to another variadic function. This
requires a vprintf-style variant of the delegate.

https://stackoverflow.com/a/150616
This commit is contained in:
Jerzi Kaminsky 2017-04-16 10:30:17 +03:00
parent 4b3e533a59
commit eb3b1ec5f1
2 changed files with 13 additions and 5 deletions

View file

@ -63,7 +63,8 @@ void sway_abort(const char *format, ...) {
sway_terminate(EXIT_FAILURE); sway_terminate(EXIT_FAILURE);
} }
void _sway_log(const char *filename, int line, log_importance_t verbosity, const char* format, ...) { void _sway_vlog(const char *filename, int line, log_importance_t verbosity,
const char *format, va_list args) {
if (verbosity <= v) { if (verbosity <= v) {
// prefix the time to the log message // prefix the time to the log message
static struct tm result; static struct tm result;
@ -99,10 +100,7 @@ void _sway_log(const char *filename, int line, log_importance_t verbosity, const
fprintf(stderr, "[%s:%d] ", file, line); fprintf(stderr, "[%s:%d] ", file, line);
} }
va_list args;
va_start(args, format);
vfprintf(stderr, format, args); vfprintf(stderr, format, args);
va_end(args);
if (colored && isatty(STDERR_FILENO)) { if (colored && isatty(STDERR_FILENO)) {
fprintf(stderr, "\x1B[0m"); fprintf(stderr, "\x1B[0m");
@ -111,6 +109,13 @@ void _sway_log(const char *filename, int line, log_importance_t verbosity, const
} }
} }
void _sway_log(const char *filename, int line, log_importance_t verbosity, const char* format, ...) {
va_list args;
va_start(args, format);
_sway_vlog(filename, line, verbosity, format, args);
va_end(args);
}
void sway_log_errno(log_importance_t verbosity, char* format, ...) { void sway_log_errno(log_importance_t verbosity, char* format, ...) {
if (verbosity <= v) { if (verbosity <= v) {
unsigned int c = verbosity; unsigned int c = verbosity;
@ -144,7 +149,7 @@ bool _sway_assert(bool condition, const char* format, ...) {
va_list args; va_list args;
va_start(args, format); va_start(args, format);
sway_log(L_ERROR, format, args); sway_vlog(L_ERROR, format, args);
va_end(args); va_end(args);
#ifndef NDEBUG #ifndef NDEBUG

View file

@ -28,6 +28,9 @@ void _sway_log(const char *filename, int line, log_importance_t verbosity, const
#define sway_log(VERBOSITY, FMT, ...) \ #define sway_log(VERBOSITY, FMT, ...) \
_sway_log(__FILE__, __LINE__, VERBOSITY, FMT, ##__VA_ARGS__) _sway_log(__FILE__, __LINE__, VERBOSITY, FMT, ##__VA_ARGS__)
#define sway_vlog(VERBOSITY, FMT, VA_ARGS) \
_sway_vlog(__FILE__, __LINE__, VERBOSITY, FMT, VA_ARGS)
void error_handler(int sig); void error_handler(int sig);
#endif #endif