summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS/Console.h
blob: 0fb757f7f7b927e3fc649cf07eddcf98dd06b1d4 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
 * Copyright (c) 2020, Emanuele Torre <torreemanuele6@gmail.com>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <AK/Function.h>
#include <AK/HashMap.h>
#include <AK/Noncopyable.h>
#include <LibJS/Forward.h>

namespace JS {

class ConsoleClient;

class Console {
    AK_MAKE_NONCOPYABLE(Console);
    AK_MAKE_NONMOVABLE(Console);

public:
    explicit Console(GlobalObject&);

    void set_client(ConsoleClient& client) { m_client = &client; }

    GlobalObject& global_object() { return m_global_object; }
    const GlobalObject& global_object() const { return m_global_object; }

    VM& vm();

    HashMap<String, unsigned>& counters() { return m_counters; }
    const HashMap<String, unsigned>& counters() const { return m_counters; }

    Value debug();
    Value error();
    Value info();
    Value log();
    Value warn();
    Value clear();
    Value trace();
    Value count();
    Value count_reset();
    Value assert_();

    unsigned counter_increment(String label);
    bool counter_reset(String label);

private:
    GlobalObject& m_global_object;
    ConsoleClient* m_client { nullptr };

    HashMap<String, unsigned> m_counters;
};

class ConsoleClient {
public:
    explicit ConsoleClient(Console& console)
        : m_console(console)
    {
    }

    virtual Value debug() = 0;
    virtual Value error() = 0;
    virtual Value info() = 0;
    virtual Value log() = 0;
    virtual Value warn() = 0;
    virtual Value clear() = 0;
    virtual Value trace() = 0;
    virtual Value count() = 0;
    virtual Value count_reset() = 0;
    virtual Value assert_() = 0;

protected:
    virtual ~ConsoleClient() = default;

    VM& vm();

    GlobalObject& global_object() { return m_console.global_object(); }
    const GlobalObject& global_object() const { return m_console.global_object(); }

    Vector<String> get_trace() const;

    Console& m_console;
};

}