2021-01-23 14:47:20 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Denis Campredon <deni_@hotmail.fr>
|
2021-05-06 12:28:44 +00:00
|
|
|
* Copyright (c) 2021, the SerenityOS developers.
|
2021-01-23 14:47:20 +00:00
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2021-01-23 14:47:20 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2021-04-25 03:05:38 +00:00
|
|
|
#include <AK/SourceLocation.h>
|
2021-01-23 14:47:20 +00:00
|
|
|
#include <AK/StringBuilder.h>
|
|
|
|
|
|
|
|
namespace AK {
|
2021-05-06 12:28:44 +00:00
|
|
|
template<bool = true>
|
2021-01-23 14:47:20 +00:00
|
|
|
class ScopeLogger {
|
|
|
|
public:
|
2021-04-26 17:55:54 +00:00
|
|
|
ScopeLogger(StringView extra, const SourceLocation& location = SourceLocation::current())
|
2021-04-25 03:05:38 +00:00
|
|
|
: m_location(location)
|
2021-04-26 17:55:54 +00:00
|
|
|
, m_extra(extra)
|
2021-01-23 14:47:20 +00:00
|
|
|
{
|
|
|
|
StringBuilder sb;
|
|
|
|
|
|
|
|
for (auto indent = m_depth++; indent > 0; indent--)
|
|
|
|
sb.append(' ');
|
2021-04-26 17:55:54 +00:00
|
|
|
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);
|
2021-01-23 14:47:20 +00:00
|
|
|
}
|
2021-04-26 17:55:54 +00:00
|
|
|
|
|
|
|
ScopeLogger(SourceLocation location = SourceLocation::current())
|
|
|
|
: ScopeLogger({}, move(location))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-01-23 14:47:20 +00:00
|
|
|
~ScopeLogger()
|
|
|
|
{
|
|
|
|
StringBuilder sb;
|
|
|
|
|
2021-04-26 17:55:54 +00:00
|
|
|
auto depth = m_depth;
|
2021-01-23 14:47:20 +00:00
|
|
|
for (auto indent = --m_depth; indent > 0; indent--)
|
|
|
|
sb.append(' ');
|
2021-04-26 17:55:54 +00:00
|
|
|
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);
|
2021-01-23 14:47:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
static inline size_t m_depth = 0;
|
2021-04-25 03:05:38 +00:00
|
|
|
SourceLocation m_location;
|
2021-04-26 17:55:54 +00:00
|
|
|
StringView m_extra;
|
2021-01-23 14:47:20 +00:00
|
|
|
};
|
2021-05-06 12:28:44 +00:00
|
|
|
|
|
|
|
template<>
|
|
|
|
class ScopeLogger<false> {
|
|
|
|
public:
|
|
|
|
template<typename... Args>
|
|
|
|
ScopeLogger(Args...) { }
|
|
|
|
};
|
|
|
|
|
2021-01-23 14:47:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
using AK::ScopeLogger;
|