serenity/AK/ScopeLogger.h
Linus Groh 6e19ab2bbc AK+Everywhere: Rename String to DeprecatedString
We have a new, improved string type coming up in AK (OOM aware, no null
state), and while it's going to use UTF-8, the name UTF8String is a
mouthful - so let's free up the String name by renaming the existing
class.
Making the old one have an annoying name will hopefully also help with
quick adoption :^)
2022-12-06 08:54:33 +01:00

68 lines
1.7 KiB
C++

/*
* Copyright (c) 2020, Denis Campredon <deni_@hotmail.fr>
* Copyright (c) 2021, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/DeprecatedString.h>
#include <AK/SourceLocation.h>
#include <AK/StringBuilder.h>
namespace AK {
template<bool = true>
class ScopeLogger {
public:
ScopeLogger(StringView extra, SourceLocation const& location = SourceLocation::current())
: m_location(location)
, m_extra(extra)
{
StringBuilder sb;
for (auto indent = m_depth++; indent > 0; indent--)
sb.append(' ');
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(' ');
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;
DeprecatedString m_extra;
};
template<>
class ScopeLogger<false> {
public:
template<typename... Args>
ScopeLogger(Args...) { }
};
}
#if USING_AK_GLOBALLY
using AK::ScopeLogger;
#endif