2021-01-23 17:47:20 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020, Denis Campredon <deni_@hotmail.fr>
|
2021-05-06 15:28:44 +03:00
|
|
|
* Copyright (c) 2021, the SerenityOS developers.
|
2021-01-23 17:47:20 +03:00
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2021-01-23 17:47:20 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
2023-12-16 17:19:34 +03:00
|
|
|
#include <AK/ByteString.h>
|
2021-04-25 06:05:38 +03:00
|
|
|
#include <AK/SourceLocation.h>
|
2021-01-23 17:47:20 +03:00
|
|
|
#include <AK/StringBuilder.h>
|
|
|
|
|
|
|
|
namespace AK {
|
2021-05-06 15:28:44 +03:00
|
|
|
template<bool = true>
|
2021-01-23 17:47:20 +03:00
|
|
|
class ScopeLogger {
|
|
|
|
public:
|
2022-04-01 20:58:27 +03:00
|
|
|
ScopeLogger(StringView extra, SourceLocation const& location = SourceLocation::current())
|
2021-04-25 06:05:38 +03:00
|
|
|
: m_location(location)
|
2021-04-26 20:55:54 +03:00
|
|
|
, m_extra(extra)
|
2021-01-23 17:47:20 +03:00
|
|
|
{
|
|
|
|
StringBuilder sb;
|
|
|
|
|
|
|
|
for (auto indent = m_depth++; indent > 0; indent--)
|
|
|
|
sb.append(' ');
|
2021-04-26 20:55:54 +03:00
|
|
|
if (m_extra.is_empty())
|
2023-12-16 17:19:34 +03:00
|
|
|
dbgln("\033[1;{}m{}entering {}\033[0m", m_depth % 8 + 30, sb.to_byte_string(), m_location);
|
2021-04-26 20:55:54 +03:00
|
|
|
else
|
2023-12-16 17:19:34 +03:00
|
|
|
dbgln("\033[1;{}m{}entering {}\033[0m ({})", m_depth % 8 + 30, sb.to_byte_string(), m_location, m_extra);
|
2021-01-23 17:47:20 +03:00
|
|
|
}
|
2021-04-26 20:55:54 +03:00
|
|
|
|
|
|
|
ScopeLogger(SourceLocation location = SourceLocation::current())
|
|
|
|
: ScopeLogger({}, move(location))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2021-01-23 17:47:20 +03:00
|
|
|
~ScopeLogger()
|
|
|
|
{
|
|
|
|
StringBuilder sb;
|
|
|
|
|
2021-04-26 20:55:54 +03:00
|
|
|
auto depth = m_depth;
|
2021-01-23 17:47:20 +03:00
|
|
|
for (auto indent = --m_depth; indent > 0; indent--)
|
|
|
|
sb.append(' ');
|
2021-04-26 20:55:54 +03:00
|
|
|
if (m_extra.is_empty())
|
2023-12-16 17:19:34 +03:00
|
|
|
dbgln("\033[1;{}m{}leaving {}\033[0m", depth % 8 + 30, sb.to_byte_string(), m_location);
|
2021-04-26 20:55:54 +03:00
|
|
|
else
|
2023-12-16 17:19:34 +03:00
|
|
|
dbgln("\033[1;{}m{}leaving {}\033[0m ({})", depth % 8 + 30, sb.to_byte_string(), m_location, m_extra);
|
2021-01-23 17:47:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
static inline size_t m_depth = 0;
|
2021-04-25 06:05:38 +03:00
|
|
|
SourceLocation m_location;
|
2023-12-16 17:19:34 +03:00
|
|
|
ByteString m_extra;
|
2021-01-23 17:47:20 +03:00
|
|
|
};
|
2021-05-06 15:28:44 +03:00
|
|
|
|
|
|
|
template<>
|
|
|
|
class ScopeLogger<false> {
|
|
|
|
public:
|
|
|
|
template<typename... Args>
|
|
|
|
ScopeLogger(Args...) { }
|
|
|
|
};
|
|
|
|
|
2021-01-23 17:47:20 +03:00
|
|
|
}
|
|
|
|
|
2022-11-26 14:18:30 +03:00
|
|
|
#if USING_AK_GLOBALLY
|
2021-01-23 17:47:20 +03:00
|
|
|
using AK::ScopeLogger;
|
2022-11-26 14:18:30 +03:00
|
|
|
#endif
|