Skip to content
Snippets Groups Projects
Unverified Commit 0d26ade2 authored by Michal 'vorner' Vaner's avatar Michal 'vorner' Vaner
Browse files

The sanity() function

Similar to assert, but with a message and not possible to exclude from
compilation. This is better for checking outputs, for example.
parent edc6ff21
No related branches found
No related tags found
No related merge requests found
/*
Ucollect - small utility for real-time analysis of network data
Copyright (C) 2013 CZ.NIC, z.s.p.o. (http://www.nic.cz/)
Copyright (C) 2013-2015 CZ.NIC, z.s.p.o. (http://www.nic.cz/)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
......@@ -25,6 +25,7 @@
#include <sys/types.h>
#include <signal.h>
#include <unistd.h>
#include <alloca.h>
void die(const char *format, ...) {
va_list args;
......@@ -75,3 +76,17 @@ void ulog_internal(enum log_level log_level, const char *format, va_list *args)
fputs(names[log_level], stderr);
vfprintf(stderr, format, *args);
}
void sanity_internal(const char *file, unsigned line, const char *check, const char *format, ...) {
va_list args;
va_start(args, format);
va_list copy;
va_copy(copy, args);
size_t needed = vsnprintf(NULL, 0, format, args);
char *output = alloca(needed + 1);
vsnprintf(output, needed + 1, format, copy);
va_end(args);
va_end(copy);
ulog(LLOG_ERROR, "%s:%u: Failed check '%s': %s", file, line, check, output);
abort();
}
/*
Ucollect - small utility for real-time analysis of network data
Copyright (C) 2013 CZ.NIC, z.s.p.o. (http://www.nic.cz/)
Copyright (C) 2013-2015 CZ.NIC, z.s.p.o. (http://www.nic.cz/)
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
......@@ -49,4 +49,9 @@ static inline void ulog(enum log_level log_level, const char *format, ...) {
va_end(args);
}
void sanity_internal(const char *file, unsigned line, const char *check, const char *format, ...) __attribute__((format(printf, 4, 5))) __attribute__((noreturn));
// An assert-like function, but with printf message that can be added. It is not omitted from compilation like assert may be. Use for checking input parameters, for example. Logs on the ERROR level and aborts, effectively killing a plugin that failed the check.
#define sanity(check, ...) do { if (!(check)) sanity_internal(__FILE__, __LINE__, #check, __VA_ARGS__); } while (0)
#endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment