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
|
|
|
|
|
2022-12-04 21:02:33 +03:00
|
|
|
#include <AK/DeprecatedString.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())
|
2022-12-06 04:12:49 +03:00
|
|
|
dbgln("\033[1;{}m{}entering {}\033[0m", m_depth % 8 + 30, sb.to_deprecated_string(), m_location);
|
2021-04-26 20:55:54 +03:00
|
|
|
else
|
2022-12-06 04:12:49 +03:00
|
|
|
dbgln("\033[1;{}m{}entering {}\033[0m ({})", m_depth % 8 + 30, sb.to_deprecated_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())
|
2022-12-06 04:12:49 +03:00
|
|
|
dbgln("\033[1;{}m{}leaving {}\033[0m", depth % 8 + 30, sb.to_deprecated_string(), m_location);
|
2021-04-26 20:55:54 +03:00
|
|
|
else
|
2022-12-06 04:12:49 +03:00
|
|
|
dbgln("\033[1;{}m{}leaving {}\033[0m ({})", depth % 8 + 30, sb.to_deprecated_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;
|
2022-12-04 21:02:33 +03:00
|
|
|
DeprecatedString 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
|