Commit 35cb961b authored by Vladimír Čunát's avatar Vladimír Čunát

--verbose: make it more efficient when not --verbose

In particular, don't require to call an externally defined function to
find if in --verbose mode or not.  Now it's just an extern bool.
I'm confident the performance impact of not using -DNLOGDEBUG should be
negligible now.

This comes with a small incompatible API+ABI change, but that shouldn't
matter as we've had a couple of those already since the last release.
parent 07ef0d92
......@@ -143,7 +143,7 @@ static int l_verbose(lua_State *L)
if (lua_isboolean(L, 1) || lua_isnumber(L, 1)) {
kr_debug_set(lua_toboolean(L, 1));
}
lua_pushboolean(L, kr_debug_status());
lua_pushboolean(L, kr_debug_do_log);
return 1;
}
......
......@@ -343,7 +343,7 @@ static int run_worker(uv_loop_t *loop, struct engine *engine, fd_array_t *ipc_se
}
memcpy(&engine->ipc_set, ipc_set, sizeof(*ipc_set));
tls_setup_logging(kr_debug_status());
tls_setup_logging(kr_debug_do_log);
/* Notify supervisor. */
#ifdef HAS_SYSTEMD
sd_notify(0, "READY=1");
......
......@@ -21,11 +21,11 @@
#ifndef NLOGDEBUG
/** @internal Print a debug message related to resolution. */
#define QRDEBUG(query, cls, fmt, ...) do { \
#define QRDEBUG(query, cls, fmt, ...) WITH_DEBUG { \
unsigned _ind = 0; \
for (struct kr_query *q = (query); q; q = q->parent, _ind += 2); \
kr_log_debug("[%s] %*s" fmt, cls, _ind, "", ## __VA_ARGS__); \
} while (0)
}
#else
#define QRDEBUG(query, cls, fmt, ...)
#endif
......
......@@ -33,8 +33,14 @@
#include "lib/module.h"
#include "lib/resolve.h"
/* Always compile-in log symbols, even if disabled. */
#undef kr_debug_do_log
#undef kr_debug_set
#undef kr_log_debug
/* Logging & debugging */
static bool _env_debug = false;
bool kr_debug_do_log = false;
/** @internal CSPRNG context */
static isaac_ctx ISAAC;
......@@ -65,24 +71,17 @@ static inline int u16tostr(uint8_t *dst, uint16_t num)
* Cleanup callbacks.
*/
/* Always compile-in log symbols, even if disabled. */
#undef kr_debug_set
#undef kr_debug_status
#undef kr_log_debug
bool kr_debug_set(bool status)
{
return _env_debug = status;
}
bool kr_debug_status(void)
{
return _env_debug;
#ifndef NLOGDEBUG
kr_debug_do_log = status;
#endif
return kr_debug_do_log;
}
void kr_log_debug(const char *fmt, ...)
{
if (_env_debug) {
if (kr_debug_do_log) {
va_list args;
va_start(args, fmt);
vprintf(fmt, args);
......
......@@ -31,20 +31,28 @@
*/
#define kr_log_info(fmt, ...) do { printf((fmt), ## __VA_ARGS__); fflush(stdout); } while(0)
#define kr_log_error(fmt, ...) fprintf(stderr, (fmt), ## __VA_ARGS__)
#ifndef NLOGDEBUG
/* Toggle debug messages */
/* Always export these, but override direct calls by macros conditionally. */
/** Whether in --verbose mode. Only use this for reading. */
KR_EXPORT extern bool kr_debug_do_log;
/** Set --verbose mode. Not available if compiled with -DNLOGDEBUG. */
KR_EXPORT bool kr_debug_set(bool status);
KR_EXPORT KR_PURE bool kr_debug_status(void);
/** Log a message if in --verbose mode. */
KR_EXPORT void kr_log_debug(const char *fmt, ...);
/* Debug block */
#define WITH_DEBUG if(__builtin_expect(kr_debug_status(), 0))
#else
#define kr_debug_status() false
#ifdef NLOGDEBUG
/* Efficient compile-time disabling of debugging messages. */
#define kr_debug_do_log false
#define kr_debug_set(x)
#define kr_log_debug(fmt, ...)
#define WITH_DEBUG if(0)
#endif
/** Block run in --verbose mode; optimized when not run. */
#define WITH_DEBUG if(__builtin_expect(kr_debug_do_log, false))
#define kr_log_debug WITH_DEBUG kr_log_debug
/* C11 compatibility, but without any implementation so far. */
#ifndef static_assert
#define static_assert(cond, msg)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment