1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 06:37:44 +00:00

LibJS+WebContent+js: Reimplement console.log() and friends to spec

This implements the Logger and Printer abstract operations defined in
the console spec, and stubs out the Formatter AO. These are then used
for the "output a categorized log message" functions.
This commit is contained in:
Sam Atkins 2021-12-10 12:26:25 +00:00 committed by Andreas Kling
parent fd7163b125
commit 260836135a
5 changed files with 211 additions and 125 deletions

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2020, Emanuele Torre <torreemanuele6@gmail.com>
* Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -9,17 +10,39 @@
#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,
};
explicit Console(GlobalObject&);
void set_client(ConsoleClient& client) { m_client = &client; }
@ -28,15 +51,16 @@ public:
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; }
Value debug();
Value error();
Value info();
Value log();
Value warn();
ThrowCompletionOr<Value> debug();
ThrowCompletionOr<Value> error();
ThrowCompletionOr<Value> info();
ThrowCompletionOr<Value> log();
ThrowCompletionOr<Value> warn();
Value clear();
Value trace();
Value count();
@ -46,6 +70,8 @@ public:
unsigned counter_increment(String label);
bool counter_reset(String label);
void output_debug_message(LogLevel log_level, String output) const;
private:
GlobalObject& m_global_object;
ConsoleClient* m_client { nullptr };
@ -60,11 +86,10 @@ public:
{
}
virtual Value debug() = 0;
virtual Value error() = 0;
virtual Value info() = 0;
virtual Value log() = 0;
virtual Value warn() = 0;
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, Vector<Value>&) = 0;
virtual Value clear() = 0;
virtual Value trace() = 0;
virtual Value count() = 0;