ladybird/Userland/Libraries/LibJS/Console.h
Sam Atkins ff5e07d718 LibJS+WebContent+js: Bring console.trace() to spec
The spec very kindly defines `Printer` as accepting
"Implementation-specific representations of printable things such as a
stack trace or group." for the `args`. We make use of that here by
passing the `Trace` itself to `Printer`, instead of having to produce a
representation of the stack trace in advance and then pass that to
`Printer`. That both avoids the hassle of tracking whether the data has
been html-encoded or not, and means clients don't have to implement the
whole `trace()` algorithm, but only the code needed to output the trace.
2021-12-27 21:44:07 +01:00

109 lines
2.6 KiB
C++

/*
* Copyright (c) 2020, Emanuele Torre <torreemanuele6@gmail.com>
* Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Function.h>
#include <AK/HashMap.h>
#include <AK/Noncopyable.h>
#include <AK/Vector.h>
#include <LibJS/Forward.h>
#include <LibJS/Runtime/Value.h>
namespace JS {
class ConsoleClient;
// https://console.spec.whatwg.org
class Console {
AK_MAKE_NONCOPYABLE(Console);
AK_MAKE_NONMOVABLE(Console);
public:
// These are not really levels, but that's the term used in the spec.
enum class LogLevel {
Assert,
Count,
CountReset,
Debug,
Dir,
DirXML,
Error,
Group,
GroupCollapsed,
Info,
Log,
TimeEnd,
TimeLog,
Trace,
Warn,
};
struct Trace {
String label;
Vector<String> stack;
};
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();
Vector<Value> vm_arguments();
HashMap<String, unsigned>& counters() { return m_counters; }
const HashMap<String, unsigned>& counters() const { return m_counters; }
ThrowCompletionOr<Value> debug();
ThrowCompletionOr<Value> error();
ThrowCompletionOr<Value> info();
ThrowCompletionOr<Value> log();
ThrowCompletionOr<Value> warn();
Value clear();
ThrowCompletionOr<Value> trace();
ThrowCompletionOr<Value> count();
ThrowCompletionOr<Value> count_reset();
ThrowCompletionOr<Value> assert_();
void output_debug_message(LogLevel log_level, String output) const;
private:
GlobalObject& m_global_object;
ConsoleClient* m_client { nullptr };
HashMap<String, unsigned> m_counters;
};
class ConsoleClient {
public:
explicit ConsoleClient(Console& console)
: m_console(console)
{
}
ThrowCompletionOr<Value> logger(Console::LogLevel log_level, Vector<Value>& args);
ThrowCompletionOr<Vector<Value>> formatter(Vector<Value>& args);
virtual ThrowCompletionOr<Value> printer(Console::LogLevel log_level, Variant<Vector<Value>, Console::Trace>) = 0;
virtual void clear() = 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(); }
Console& m_console;
};
}