/* * 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 #include #include #include namespace JS { class ConsoleClient; // https://console.spec.whatwg.org class Console : public Cell { GC_CELL(Console, Cell); GC_DECLARE_ALLOCATOR(Console); public: virtual ~Console() override; // 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, Table, Trace, Warn, }; struct Group { String label; }; struct Trace { String label; Vector stack; }; void set_client(ConsoleClient& client) { m_client = &client; } Realm& realm() const { return m_realm; } GC::MarkedVector vm_arguments(); HashMap& counters() { return m_counters; } HashMap const& counters() const { return m_counters; } ThrowCompletionOr assert_(); Value clear(); ThrowCompletionOr debug(); ThrowCompletionOr error(); ThrowCompletionOr info(); ThrowCompletionOr log(); ThrowCompletionOr table(); ThrowCompletionOr trace(); ThrowCompletionOr warn(); ThrowCompletionOr dir(); ThrowCompletionOr count(); ThrowCompletionOr count_reset(); ThrowCompletionOr group(); ThrowCompletionOr group_collapsed(); ThrowCompletionOr group_end(); ThrowCompletionOr time(); ThrowCompletionOr time_log(); ThrowCompletionOr time_end(); void output_debug_message(LogLevel log_level, String const& output) const; void report_exception(JS::Error const&, bool) const; private: explicit Console(Realm&); virtual void visit_edges(Visitor&) override; ThrowCompletionOr value_vector_to_string(GC::MarkedVector const&); GC::Ref m_realm; GC::Ptr m_client; HashMap m_counters; HashMap m_timer_table; Vector m_group_stack; }; class ConsoleClient : public Cell { GC_CELL(ConsoleClient, Cell); GC_DECLARE_ALLOCATOR(ConsoleClient); public: using PrinterArguments = Variant>; ThrowCompletionOr logger(Console::LogLevel log_level, GC::MarkedVector const& args); ThrowCompletionOr> formatter(GC::MarkedVector const& args); virtual ThrowCompletionOr printer(Console::LogLevel log_level, PrinterArguments) = 0; virtual void add_css_style_to_current_message(StringView) { } virtual void report_exception(JS::Error const&, bool) { } virtual void clear() = 0; virtual void end_group() = 0; ThrowCompletionOr generically_format_values(GC::MarkedVector const&); protected: explicit ConsoleClient(Console&); virtual ~ConsoleClient() override; virtual void visit_edges(Visitor& visitor) override; GC::Ref m_console; }; }