/src/serenity/AK/ScopeLogger.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2020, Denis Campredon <deni_@hotmail.fr> |
3 | | * Copyright (c) 2021, the SerenityOS developers. |
4 | | * |
5 | | * SPDX-License-Identifier: BSD-2-Clause |
6 | | */ |
7 | | |
8 | | #pragma once |
9 | | |
10 | | #include <AK/ByteString.h> |
11 | | #include <AK/SourceLocation.h> |
12 | | #include <AK/StringBuilder.h> |
13 | | |
14 | | namespace AK { |
15 | | template<bool = true> |
16 | | class ScopeLogger { |
17 | | public: |
18 | | ScopeLogger(StringView extra, SourceLocation const& location = SourceLocation::current()) |
19 | | : m_location(location) |
20 | | , m_extra(extra) |
21 | | { |
22 | | StringBuilder sb; |
23 | | |
24 | | for (auto indent = m_depth++; indent > 0; indent--) |
25 | | sb.append(' '); |
26 | | if (m_extra.is_empty()) |
27 | | dbgln("\033[1;{}m{}entering {}\033[0m", m_depth % 8 + 30, sb.to_byte_string(), m_location); |
28 | | else |
29 | | dbgln("\033[1;{}m{}entering {}\033[0m ({})", m_depth % 8 + 30, sb.to_byte_string(), m_location, m_extra); |
30 | | } |
31 | | |
32 | | ScopeLogger(SourceLocation location = SourceLocation::current()) |
33 | | : ScopeLogger({}, move(location)) |
34 | | { |
35 | | } |
36 | | |
37 | | ~ScopeLogger() |
38 | | { |
39 | | StringBuilder sb; |
40 | | |
41 | | auto depth = m_depth; |
42 | | for (auto indent = --m_depth; indent > 0; indent--) |
43 | | sb.append(' '); |
44 | | if (m_extra.is_empty()) |
45 | | dbgln("\033[1;{}m{}leaving {}\033[0m", depth % 8 + 30, sb.to_byte_string(), m_location); |
46 | | else |
47 | | dbgln("\033[1;{}m{}leaving {}\033[0m ({})", depth % 8 + 30, sb.to_byte_string(), m_location, m_extra); |
48 | | } |
49 | | |
50 | | private: |
51 | | static inline size_t m_depth = 0; |
52 | | SourceLocation m_location; |
53 | | ByteString m_extra; |
54 | | }; |
55 | | |
56 | | template<> |
57 | | class ScopeLogger<false> { |
58 | | public: |
59 | | template<typename... Args> |
60 | 64.1M | ScopeLogger(Args...) { }AK::ScopeLogger<false>::ScopeLogger<>() Line | Count | Source | 60 | 35.9M | ScopeLogger(Args...) { } |
AK::ScopeLogger<false>::ScopeLogger<AK::StringView>(AK::StringView) Line | Count | Source | 60 | 28.2M | ScopeLogger(Args...) { } |
|
61 | | }; |
62 | | |
63 | | } |
64 | | |
65 | | #if USING_AK_GLOBALLY |
66 | | using AK::ScopeLogger; |
67 | | #endif |