/* * Copyright (c) 2020, Emanuele Torre * Copyright (c) 2021, Sam Atkins * * SPDX-License-Identifier: BSD-2-Clause */ #pragma once #include #include #include #include #include #include #include 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 Group { String label; }; struct Trace { String label; Vector 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 vm_arguments(); HashMap& counters() { return m_counters; } const HashMap& counters() const { return m_counters; } ThrowCompletionOr debug(); ThrowCompletionOr error(); ThrowCompletionOr info(); ThrowCompletionOr log(); ThrowCompletionOr warn(); Value clear(); ThrowCompletionOr trace(); ThrowCompletionOr count(); ThrowCompletionOr count_reset(); ThrowCompletionOr assert_(); ThrowCompletionOr group(); ThrowCompletionOr group_collapsed(); ThrowCompletionOr group_end(); ThrowCompletionOr time(); ThrowCompletionOr time_log(); ThrowCompletionOr time_end(); void output_debug_message(LogLevel log_level, String output) const; private: ThrowCompletionOr value_vector_to_string(Vector&); ThrowCompletionOr format_time_since(Core::ElapsedTimer timer); GlobalObject& m_global_object; ConsoleClient* m_client { nullptr }; HashMap m_counters; HashMap m_timer_table; Vector m_group_stack; }; class ConsoleClient { public: explicit ConsoleClient(Console& console) : m_console(console) { } using PrinterArguments = Variant>; ThrowCompletionOr logger(Console::LogLevel log_level, Vector& args); ThrowCompletionOr> formatter(Vector& args); virtual ThrowCompletionOr printer(Console::LogLevel log_level, PrinterArguments) = 0; virtual void clear() = 0; virtual void end_group() = 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; }; }