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
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
#include <AK/DeprecatedString.h>
|
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:
|
2022-04-01 17:58:27 +00:00
|
|
|
ScopeLogger(StringView extra, SourceLocation const& 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())
|
2022-12-06 01:12:49 +00:00
|
|
|
dbgln("\033[1;{}m{}entering {}\033[0m", m_depth % 8 + 30, sb.to_deprecated_string(), m_location);
|
2021-04-26 17:55:54 +00:00
|
|
|
else
|
2022-12-06 01:12:49 +00:00
|
|
|
dbgln("\033[1;{}m{}entering {}\033[0m ({})", m_depth % 8 + 30, sb.to_deprecated_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())
|
2022-12-06 01:12:49 +00:00
|
|
|
dbgln("\033[1;{}m{}leaving {}\033[0m", depth % 8 + 30, sb.to_deprecated_string(), m_location);
|
2021-04-26 17:55:54 +00:00
|
|
|
else
|
2022-12-06 01:12:49 +00:00
|
|
|
dbgln("\033[1;{}m{}leaving {}\033[0m ({})", depth % 8 + 30, sb.to_deprecated_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;
|
2022-12-04 18:02:33 +00:00
|
|
|
DeprecatedString 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
|
|
|
}
|
|
|
|
|
2022-11-26 11:18:30 +00:00
|
|
|
#if USING_AK_GLOBALLY
|
2021-01-23 14:47:20 +00:00
|
|
|
using AK::ScopeLogger;
|
2022-11-26 11:18:30 +00:00
|
|
|
#endif
|