]> git.ipfire.org Git - thirdparty/knot-resolver.git/commitdiff
lib/log: add kr_log_is_debug*()
authorVladimír Čunát <vladimir.cunat@nic.cz>
Mon, 12 Jul 2021 20:19:20 +0000 (22:19 +0200)
committerTomas Krizek <tomas.krizek@nic.cz>
Thu, 29 Jul 2021 09:42:34 +0000 (11:42 +0200)
... and optimize a suspect hot path (through VERBOSE_MSG)

lib/log.c
lib/log.h

index 255f70af341145565c69567d824a2bab189aba75..d99a17a424464fe956214dfc0839f14af37586fe 100644 (file)
--- a/lib/log.c
+++ b/lib/log.c
@@ -281,15 +281,25 @@ void kr_log_req1(const struct kr_request * const req, uint32_t qry_uid,
        va_end(args);
 }
 
+bool kr_log_is_debug_fun(enum kr_log_group group, const struct kr_request *req)
+{
+       return kr_log_rtrace_enabled(req)
+               || kr_log_group_is_set(group)
+               || KR_LOG_LEVEL_IS(LOG_DEBUG);
+}
+
 void kr_log_q1(const struct kr_query * const qry,
                enum kr_log_group group, const char *tag, const char *fmt, ...)
 {
-       // TODO(optim.): early return if there's nothing to do
+       // Optimize: this is probably quite a hot path.
+       const struct kr_request *req = likely(qry != NULL) ? qry->request : NULL;
+       if (likely(!kr_log_is_debug_fun(group, req)))
+               return;
+
        unsigned ind = 0;
        for (const struct kr_query *q = qry; q; q = q->parent)
                ind += 2;
        const uint32_t qry_uid = qry ? qry->uid : 0;
-       const struct kr_request *req = qry ? qry->request : NULL;
 
        va_list args;
        va_start(args, fmt);
index 993ad72e6c51edd870e9bc84c3b0aab4c4f0a0d4..3970e1a146dc4644d6572384446bfe3fe7c060c9 100644 (file)
--- a/lib/log.h
+++ b/lib/log.h
@@ -231,3 +231,16 @@ void kr_log_q1(const struct kr_query *qry, enum kr_log_group group, const char *
 #define VERBOSE_STATUS __builtin_expect(KR_LOG_LEVEL_IS(LOG_DEBUG), false) // TODO vyhodit
 #define WITH_VERBOSE(query) if(__builtin_expect(KR_LOG_LEVEL_IS(LOG_DEBUG) || kr_log_qtrace_enabled(query), false))
 
+/** Return whether a particular log group in a request is in debug/verbose mode.
+ *
+ * Typically you use this as condition to compute some data to be logged,
+ * in case that's considered too expensive to do unless it really gets logged.
+ *
+ * The request can be NULL, and there's a _qry() shortand to specify query instead.
+ */
+#define kr_log_is_debug(grp, req) \
+       __builtin_expect(kr_log_is_debug_fun(LOG_GRP_ ## grp, (req)), false)
+#define kr_log_is_debug_qry(grp, qry) kr_log_is_debug(grp, (qry)->request)
+KR_EXPORT
+bool kr_log_is_debug_fun(enum kr_log_group group, const struct kr_request *req);
+