/* * Copyright (c) 2022, Ben Abraham * * SPDX-License-Identifier: BSD-2-Clause */ #include #include #include #include #include #include namespace Web::HTML { WorkerDebugConsoleClient::WorkerDebugConsoleClient(JS::Console& console) : ConsoleClient(console) { } void WorkerDebugConsoleClient::clear() { dbgln("\033[3J\033[H\033[2J"); m_group_stack_depth = 0; fflush(stdout); } void WorkerDebugConsoleClient::end_group() { if (m_group_stack_depth > 0) m_group_stack_depth--; } // 2.3. Printer(logLevel, args[, options]), https://console.spec.whatwg.org/#printer JS::ThrowCompletionOr WorkerDebugConsoleClient::printer(JS::Console::LogLevel log_level, PrinterArguments arguments) { auto& vm = m_console.realm().vm(); auto indent = TRY_OR_THROW_OOM(vm, String::repeated(' ', m_group_stack_depth * 2)); if (log_level == JS::Console::LogLevel::Trace) { auto trace = arguments.get(); JS::ThrowableStringBuilder builder(vm); if (!trace.label.is_empty()) MUST_OR_THROW_OOM(builder.appendff("{}\033[36;1m{}\033[0m\n", indent, trace.label)); for (auto& function_name : trace.stack) MUST_OR_THROW_OOM(builder.appendff("{}-> {}\n", indent, function_name)); dbgln("{}", builder.string_view()); return JS::js_undefined(); } if (log_level == JS::Console::LogLevel::Group || log_level == JS::Console::LogLevel::GroupCollapsed) { auto group = arguments.get(); dbgln("{}\033[36;1m{}\033[0m", indent, group.label); m_group_stack_depth++; return JS::js_undefined(); } auto output = TRY(generically_format_values(arguments.get>())); m_console.output_debug_message(log_level, output); switch (log_level) { case JS::Console::LogLevel::Debug: dbgln("{}\033[36;1m{}\033[0m", indent, output); break; case JS::Console::LogLevel::Error: case JS::Console::LogLevel::Assert: dbgln("{}\033[31;1m{}\033[0m", indent, output); break; case JS::Console::LogLevel::Info: dbgln("{}(i) {}", indent, output); break; case JS::Console::LogLevel::Log: dbgln("{}{}", indent, output); break; case JS::Console::LogLevel::Warn: case JS::Console::LogLevel::CountReset: dbgln("{}\033[33;1m{}\033[0m", indent, output); break; default: dbgln("{}{}", indent, output); break; } return JS::js_undefined(); } } // namespace Web::HTML