| 
									
										
										
										
											2022-02-17 13:31:09 +01:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * Copyright (c) 2022, Ben Abraham <ben.d.abraham@gmail.com> | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-09-06 08:29:01 -04:00
										 |  |  | #include <AK/StringBuilder.h>
 | 
					
						
							| 
									
										
										
										
											2024-12-26 14:32:52 +01:00
										 |  |  | #include <LibGC/RootVector.h>
 | 
					
						
							| 
									
										
										
										
											2022-02-17 13:31:09 +01:00
										 |  |  | #include <LibJS/Runtime/Completion.h>
 | 
					
						
							| 
									
										
										
										
											2023-02-10 11:40:52 -05:00
										 |  |  | #include <LibJS/Runtime/Realm.h>
 | 
					
						
							|  |  |  | #include <LibJS/Runtime/VM.h>
 | 
					
						
							| 
									
										
										
										
											2022-02-17 13:31:09 +01:00
										 |  |  | #include <LibWeb/HTML/WorkerDebugConsoleClient.h>
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace Web::HTML { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-11-15 04:01:23 +13:00
										 |  |  | GC_DEFINE_ALLOCATOR(WorkerDebugConsoleClient); | 
					
						
							| 
									
										
										
										
											2024-04-20 21:19:51 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-02-17 13:31:09 +01:00
										 |  |  | 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<JS::Value> WorkerDebugConsoleClient::printer(JS::Console::LogLevel log_level, PrinterArguments arguments) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2024-04-20 21:19:51 +02:00
										 |  |  |     auto& vm = m_console->realm().vm(); | 
					
						
							| 
									
										
										
										
											2023-02-10 11:40:52 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     auto indent = TRY_OR_THROW_OOM(vm, String::repeated(' ', m_group_stack_depth * 2)); | 
					
						
							| 
									
										
										
										
											2022-02-17 13:31:09 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (log_level == JS::Console::LogLevel::Trace) { | 
					
						
							|  |  |  |         auto trace = arguments.get<JS::Console::Trace>(); | 
					
						
							| 
									
										
										
										
											2023-09-06 08:29:01 -04:00
										 |  |  |         StringBuilder builder; | 
					
						
							| 
									
										
										
										
											2022-02-17 13:31:09 +01:00
										 |  |  |         if (!trace.label.is_empty()) | 
					
						
							| 
									
										
										
										
											2023-09-06 08:29:01 -04:00
										 |  |  |             builder.appendff("{}\033[36;1m{}\033[0m\n", indent, trace.label); | 
					
						
							| 
									
										
										
										
											2022-02-17 13:31:09 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         for (auto& function_name : trace.stack) | 
					
						
							| 
									
										
										
										
											2023-09-06 08:29:01 -04:00
										 |  |  |             builder.appendff("{}-> {}\n", indent, function_name); | 
					
						
							| 
									
										
										
										
											2022-02-17 13:31:09 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         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<JS::Console::Group>(); | 
					
						
							|  |  |  |         dbgln("{}\033[36;1m{}\033[0m", indent, group.label); | 
					
						
							|  |  |  |         m_group_stack_depth++; | 
					
						
							|  |  |  |         return JS::js_undefined(); | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-26 14:32:52 +01:00
										 |  |  |     auto output = TRY(generically_format_values(arguments.get<GC::RootVector<JS::Value>>())); | 
					
						
							| 
									
										
										
										
											2024-04-20 21:19:51 +02:00
										 |  |  |     m_console->output_debug_message(log_level, output); | 
					
						
							| 
									
										
										
										
											2022-02-17 13:31:09 +01:00
										 |  |  |     return JS::js_undefined(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } // namespace Web::HTML
 |