From 080f2c0e3e8656d23ec4b4ee73d827495b8ddcb3 Mon Sep 17 00:00:00 2001 From: Ali Mohammad Pur Date: Mon, 26 Apr 2021 22:25:54 +0430 Subject: [PATCH] AK: Add an optional 'extra' descriptive field to ScopeLogger Often it's easier to know what the functions are doing with a unique name next to the function's name :) --- AK/ScopeLogger.h | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/AK/ScopeLogger.h b/AK/ScopeLogger.h index d9e301d1a3..d077f9dd1d 100644 --- a/AK/ScopeLogger.h +++ b/AK/ScopeLogger.h @@ -13,27 +13,42 @@ namespace AK { class ScopeLogger { public: #ifdef DEBUG_SPAM - ScopeLogger(const SourceLocation& location = SourceLocation::current()) + ScopeLogger(StringView extra, const SourceLocation& location = SourceLocation::current()) : m_location(location) + , m_extra(extra) { StringBuilder sb; for (auto indent = m_depth++; indent > 0; indent--) sb.append(' '); - dbgln("\033[1;{}m{}entering {}\033[0m", m_depth % 8 + 30, sb.to_string(), m_location); + if (m_extra.is_empty()) + dbgln("\033[1;{}m{}entering {}\033[0m", m_depth % 8 + 30, sb.to_string(), m_location); + else + dbgln("\033[1;{}m{}entering {} ({})\033[0m", m_depth % 8 + 30, sb.to_string(), m_location, m_extra); } + + ScopeLogger(SourceLocation location = SourceLocation::current()) + : ScopeLogger({}, move(location)) + { + } + ~ScopeLogger() { StringBuilder sb; + auto depth = m_depth; for (auto indent = --m_depth; indent > 0; indent--) sb.append(' '); - dbgln("\033[1;{}m{}leaving {}\033[0m", (m_depth + 1) % 8 + 30, sb.to_string(), m_location); + if (m_extra.is_empty()) + dbgln("\033[1;{}m{}leaving {}\033[0m", depth % 8 + 30, sb.to_string(), m_location); + else + dbgln("\033[1;{}m{}leaving {} ({})\033[0m", depth % 8 + 30, sb.to_string(), m_location, m_extra); } private: static inline size_t m_depth = 0; SourceLocation m_location; + StringView m_extra; #else ScopeLogger() = default; #endif