From bf095ecc5b2d90c46e31be97c091188135e82fff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A5vard=20Pettersson?= Date: Thu, 9 Feb 2017 22:04:39 -0800 Subject: [PATCH] Hook into Tox logs if built without NDEBUG This also requires libtoxcore to be built with logging enabled. --- src/twc-profile.c | 5 +++++ src/twc-tox-callbacks.c | 38 ++++++++++++++++++++++++++++++++++++++ src/twc-tox-callbacks.h | 8 ++++++++ 3 files changed, 51 insertions(+) diff --git a/src/twc-profile.c b/src/twc-profile.c index 7efbabf..51c5cd0 100644 --- a/src/twc-profile.c +++ b/src/twc-profile.c @@ -208,6 +208,11 @@ twc_profile_set_options(struct Tox_Options *options, TWC_PROFILE_OPTION_BOOLEAN(profile, TWC_PROFILE_OPTION_UDP); options->ipv6_enabled = TWC_PROFILE_OPTION_BOOLEAN(profile, TWC_PROFILE_OPTION_IPV6); + +#ifndef NDEBUG + options->log_callback = twc_tox_log_callback; + options->log_user_data = profile; +#endif /* !NDEBUG */ } void diff --git a/src/twc-tox-callbacks.c b/src/twc-tox-callbacks.c index 0202ee8..e4053bc 100644 --- a/src/twc-tox-callbacks.c +++ b/src/twc-tox-callbacks.c @@ -18,6 +18,7 @@ */ #include +#include #include #include @@ -515,3 +516,40 @@ twc_group_title_callback(Tox *tox, free(topic); } +#ifndef NDEBUG +void +twc_tox_log_callback(Tox *tox, + TOX_LOG_LEVEL level, + const char *file, uint32_t line, const char *func, + const char *message, void *user_data) +{ + struct t_twc_profile *const profile = user_data; + + char const *color; + switch (level) + { + case TOX_LOG_LEVEL_TRACE: + color = weechat_color("gray"); + break; + case TOX_LOG_LEVEL_DEBUG: + color = weechat_color("white"); + break; + case TOX_LOG_LEVEL_INFO: + color = weechat_color("lightblue"); + break; + case TOX_LOG_LEVEL_WARNING: + color = weechat_color("yellow"); + break; + case TOX_LOG_LEVEL_ERROR: + color = weechat_color("red"); + break; + default: + color = weechat_color("reset"); + } + + weechat_printf(profile->buffer, "%stox\t%s%s:%"PRIu32" [%s]%s %s", + color, weechat_color("reset"), file, line, func, + weechat_color("lightred"), message); +} +#endif /* !NDEBUG */ + diff --git a/src/twc-tox-callbacks.h b/src/twc-tox-callbacks.h index 883c21f..ae8639a 100644 --- a/src/twc-tox-callbacks.h +++ b/src/twc-tox-callbacks.h @@ -78,5 +78,13 @@ twc_group_title_callback(Tox *tox, const uint8_t *title, size_t length, void *data); +#ifndef NDEBUG +void +twc_tox_log_callback(Tox *tox, + TOX_LOG_LEVEL level, + const char *file, uint32_t line, const char *func, + const char *message, void *user_data); +#endif /* !NDEBUG */ + #endif // TOX_WEECHAT_TOX_CALLBACKS_H