/src/serenity/Userland/Libraries/LibJS/Console.h
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2020, Emanuele Torre <torreemanuele6@gmail.com> |
3 | | * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org> |
4 | | * |
5 | | * SPDX-License-Identifier: BSD-2-Clause |
6 | | */ |
7 | | |
8 | | #pragma once |
9 | | |
10 | | #include <AK/Function.h> |
11 | | #include <AK/HashMap.h> |
12 | | #include <AK/Noncopyable.h> |
13 | | #include <AK/String.h> |
14 | | #include <AK/Variant.h> |
15 | | #include <AK/Vector.h> |
16 | | #include <LibCore/ElapsedTimer.h> |
17 | | #include <LibJS/Forward.h> |
18 | | #include <LibJS/Heap/Cell.h> |
19 | | #include <LibJS/Heap/CellAllocator.h> |
20 | | #include <LibJS/Runtime/Value.h> |
21 | | |
22 | | namespace JS { |
23 | | |
24 | | class ConsoleClient; |
25 | | |
26 | | // https://console.spec.whatwg.org |
27 | | class Console : public Cell { |
28 | | JS_CELL(Console, Cell); |
29 | | JS_DECLARE_ALLOCATOR(Console); |
30 | | |
31 | | public: |
32 | | virtual ~Console() override; |
33 | | |
34 | | // These are not really levels, but that's the term used in the spec. |
35 | | enum class LogLevel { |
36 | | Assert, |
37 | | Count, |
38 | | CountReset, |
39 | | Debug, |
40 | | Dir, |
41 | | DirXML, |
42 | | Error, |
43 | | Group, |
44 | | GroupCollapsed, |
45 | | Info, |
46 | | Log, |
47 | | TimeEnd, |
48 | | TimeLog, |
49 | | Table, |
50 | | Trace, |
51 | | Warn, |
52 | | }; |
53 | | |
54 | | struct Group { |
55 | | String label; |
56 | | }; |
57 | | |
58 | | struct Trace { |
59 | | String label; |
60 | | Vector<String> stack; |
61 | | }; |
62 | | |
63 | 0 | void set_client(ConsoleClient& client) { m_client = &client; } |
64 | | |
65 | 0 | Realm& realm() const { return m_realm; } |
66 | | |
67 | | MarkedVector<Value> vm_arguments(); |
68 | | |
69 | 0 | HashMap<String, unsigned>& counters() { return m_counters; } |
70 | 0 | HashMap<String, unsigned> const& counters() const { return m_counters; } |
71 | | |
72 | | ThrowCompletionOr<Value> assert_(); |
73 | | Value clear(); |
74 | | ThrowCompletionOr<Value> debug(); |
75 | | ThrowCompletionOr<Value> error(); |
76 | | ThrowCompletionOr<Value> info(); |
77 | | ThrowCompletionOr<Value> log(); |
78 | | ThrowCompletionOr<Value> table(); |
79 | | ThrowCompletionOr<Value> trace(); |
80 | | ThrowCompletionOr<Value> warn(); |
81 | | ThrowCompletionOr<Value> dir(); |
82 | | ThrowCompletionOr<Value> count(); |
83 | | ThrowCompletionOr<Value> count_reset(); |
84 | | ThrowCompletionOr<Value> group(); |
85 | | ThrowCompletionOr<Value> group_collapsed(); |
86 | | ThrowCompletionOr<Value> group_end(); |
87 | | ThrowCompletionOr<Value> time(); |
88 | | ThrowCompletionOr<Value> time_log(); |
89 | | ThrowCompletionOr<Value> time_end(); |
90 | | |
91 | | void output_debug_message(LogLevel log_level, String const& output) const; |
92 | | void report_exception(JS::Error const&, bool) const; |
93 | | |
94 | | private: |
95 | | explicit Console(Realm&); |
96 | | |
97 | | virtual void visit_edges(Visitor&) override; |
98 | | |
99 | | ThrowCompletionOr<String> value_vector_to_string(MarkedVector<Value> const&); |
100 | | ThrowCompletionOr<String> format_time_since(Core::ElapsedTimer timer); |
101 | | |
102 | | NonnullGCPtr<Realm> m_realm; |
103 | | GCPtr<ConsoleClient> m_client; |
104 | | |
105 | | HashMap<String, unsigned> m_counters; |
106 | | HashMap<String, Core::ElapsedTimer> m_timer_table; |
107 | | Vector<Group> m_group_stack; |
108 | | }; |
109 | | |
110 | | class ConsoleClient : public Cell { |
111 | | JS_CELL(ConsoleClient, Cell); |
112 | | JS_DECLARE_ALLOCATOR(ConsoleClient); |
113 | | |
114 | | public: |
115 | | using PrinterArguments = Variant<Console::Group, Console::Trace, MarkedVector<Value>>; |
116 | | |
117 | | ThrowCompletionOr<Value> logger(Console::LogLevel log_level, MarkedVector<Value> const& args); |
118 | | ThrowCompletionOr<MarkedVector<Value>> formatter(MarkedVector<Value> const& args); |
119 | | virtual ThrowCompletionOr<Value> printer(Console::LogLevel log_level, PrinterArguments) = 0; |
120 | | |
121 | 0 | virtual void add_css_style_to_current_message(StringView) { } |
122 | 0 | virtual void report_exception(JS::Error const&, bool) { } |
123 | | |
124 | | virtual void clear() = 0; |
125 | | virtual void end_group() = 0; |
126 | | |
127 | | ThrowCompletionOr<String> generically_format_values(MarkedVector<Value> const&); |
128 | | |
129 | | protected: |
130 | | explicit ConsoleClient(Console&); |
131 | | virtual ~ConsoleClient() override; |
132 | | virtual void visit_edges(Visitor& visitor) override; |
133 | | |
134 | | NonnullGCPtr<Console> m_console; |
135 | | }; |
136 | | |
137 | | } |