summaryrefslogtreecommitdiff
path: root/AK/ScopeLogger.h
blob: 418027862c55716932c0a2c95433da159bf50681 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
 * Copyright (c) 2020, Denis Campredon <deni_@hotmail.fr>
 * Copyright (c) 2021, the SerenityOS developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/SourceLocation.h>
#include <AK/String.h>
#include <AK/StringBuilder.h>

namespace AK {
template<bool = true>
class ScopeLogger {
public:
    ScopeLogger(StringView extra, const SourceLocation& location = SourceLocation::current())
        : m_location(location)
        , m_extra(extra)
    {
        StringBuilder sb;

        for (auto indent = m_depth++; indent > 0; indent--)
            sb.append(' ');
        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);
    }

    ScopeLogger(SourceLocation location = SourceLocation::current())
        : ScopeLogger({}, move(location))
    {
    }

    ~ScopeLogger()
    {
        StringBuilder sb;

        auto depth = m_depth;
        for (auto indent = --m_depth; indent > 0; indent--)
            sb.append(' ');
        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);
    }

private:
    static inline size_t m_depth = 0;
    SourceLocation m_location;
    String m_extra;
};

template<>
class ScopeLogger<false> {
public:
    template<typename... Args>
    ScopeLogger(Args...) { }
};

}

using AK::ScopeLogger;