mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-21 20:15:17 +00:00
LibJS: Remove ConsoleMessage from LibJS
We don't need to store the past messages in LibJS. We'll implement a way to let LibJS users expand the vanilla Console.
This commit is contained in:
parent
7dd49047f3
commit
046f9cf115
Notes:
sideshowbarker
2024-07-19 06:59:06 +09:00
Author: https://github.com/emanuele6 Commit: https://github.com/SerenityOS/serenity/commit/046f9cf115a Pull-request: https://github.com/SerenityOS/serenity/pull/2098 Reviewed-by: https://github.com/awesomekling
3 changed files with 20 additions and 84 deletions
|
@ -37,44 +37,34 @@ Console::Console(Interpreter& interpreter)
|
|||
{
|
||||
}
|
||||
|
||||
void Console::add_message(ConsoleMessageKind kind, String text)
|
||||
{
|
||||
ConsoleMessage message = { kind, text };
|
||||
m_messages.append(message);
|
||||
|
||||
if (on_new_message)
|
||||
on_new_message(message);
|
||||
}
|
||||
|
||||
void Console::debug(String text)
|
||||
{
|
||||
add_message(ConsoleMessageKind::Debug, text);
|
||||
dbg() << "debug: " << text;
|
||||
}
|
||||
|
||||
void Console::error(String text)
|
||||
{
|
||||
add_message(ConsoleMessageKind::Error, text);
|
||||
dbg() << "error: " << text;
|
||||
}
|
||||
|
||||
void Console::info(String text)
|
||||
{
|
||||
add_message(ConsoleMessageKind::Info, text);
|
||||
dbg() << "info: " << text;
|
||||
}
|
||||
|
||||
void Console::log(String text)
|
||||
{
|
||||
add_message(ConsoleMessageKind::Log, text);
|
||||
dbg() << "log: " << text;
|
||||
}
|
||||
|
||||
void Console::warn(String text)
|
||||
{
|
||||
add_message(ConsoleMessageKind::Warn, text);
|
||||
dbg() << "warn: " << text;
|
||||
}
|
||||
|
||||
void Console::clear()
|
||||
{
|
||||
m_messages.clear();
|
||||
add_message(ConsoleMessageKind::Clear, {});
|
||||
dbg() << "clear:";
|
||||
}
|
||||
|
||||
void Console::trace(String title)
|
||||
|
@ -86,38 +76,38 @@ void Console::trace(String title)
|
|||
// -2 to skip the console.trace() call frame
|
||||
for (ssize_t i = call_stack.size() - 2; i >= 0; --i) {
|
||||
auto function_name = call_stack[i].function_name;
|
||||
message_text.append("\n\t");
|
||||
message_text.append("\n -> ");
|
||||
if (String(function_name).is_empty())
|
||||
function_name = "<anonymous>";
|
||||
message_text.append(function_name);
|
||||
}
|
||||
|
||||
add_message(ConsoleMessageKind::Trace, message_text.build());
|
||||
dbg() << "log: " << message_text.build();
|
||||
}
|
||||
|
||||
unsigned Console::count(String label)
|
||||
void Console::count(String label)
|
||||
{
|
||||
auto counter_value = m_counters.get(label);
|
||||
if (!counter_value.has_value()) {
|
||||
add_message(ConsoleMessageKind::Count, String::format("%s: 1", label.characters()));
|
||||
dbg() << "log: " << label << ": 1";
|
||||
m_counters.set(label, 1);
|
||||
return 1;
|
||||
return;
|
||||
}
|
||||
|
||||
auto new_counter_value = counter_value.value() + 1;
|
||||
add_message(ConsoleMessageKind::Count, String::format("%s: %u", label.characters(), new_counter_value));
|
||||
dbg() << "log: " << label << ": " << new_counter_value;
|
||||
m_counters.set(label, new_counter_value);
|
||||
return new_counter_value;
|
||||
}
|
||||
|
||||
bool Console::count_reset(String label)
|
||||
void Console::count_reset(String label)
|
||||
{
|
||||
if (!m_counters.contains(label))
|
||||
return false;
|
||||
if (m_counters.contains(label)) {
|
||||
dbg() << "warn: \"" << label << "\" doesn't have a count";
|
||||
return;
|
||||
}
|
||||
|
||||
m_counters.remove(label);
|
||||
add_message(ConsoleMessageKind::Count, String::format("%s: 0", label.characters()));
|
||||
return true;
|
||||
dbg() << "log: " << label << ": 0";
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -33,22 +33,6 @@
|
|||
|
||||
namespace JS {
|
||||
|
||||
enum class ConsoleMessageKind {
|
||||
Clear,
|
||||
Count,
|
||||
Debug,
|
||||
Error,
|
||||
Info,
|
||||
Log,
|
||||
Trace,
|
||||
Warn,
|
||||
};
|
||||
|
||||
struct ConsoleMessage {
|
||||
ConsoleMessageKind kind;
|
||||
String text;
|
||||
};
|
||||
|
||||
class Console {
|
||||
AK_MAKE_NONCOPYABLE(Console);
|
||||
AK_MAKE_NONMOVABLE(Console);
|
||||
|
@ -70,17 +54,12 @@ public:
|
|||
|
||||
void trace(String title);
|
||||
|
||||
unsigned count(String label = "default");
|
||||
bool count_reset(String label = "default");
|
||||
|
||||
void add_message(ConsoleMessageKind, String text);
|
||||
|
||||
AK::Function<void(ConsoleMessage&)> on_new_message;
|
||||
void count(String label = "default");
|
||||
void count_reset(String label = "default");
|
||||
|
||||
private:
|
||||
Interpreter& m_interpreter;
|
||||
|
||||
Vector<ConsoleMessage> m_messages;
|
||||
HashMap<String, unsigned> m_counters;
|
||||
};
|
||||
|
||||
|
|
|
@ -388,37 +388,6 @@ void sigint_handler()
|
|||
interrupt_interpreter();
|
||||
}
|
||||
|
||||
void console_message_handler(JS::ConsoleMessage& message)
|
||||
{
|
||||
switch (message.kind) {
|
||||
case JS::ConsoleMessageKind::Count:
|
||||
case JS::ConsoleMessageKind::Log:
|
||||
case JS::ConsoleMessageKind::Info:
|
||||
case JS::ConsoleMessageKind::Trace:
|
||||
puts(message.text.characters());
|
||||
break;
|
||||
case JS::ConsoleMessageKind::Debug:
|
||||
printf("\033[36;1m");
|
||||
puts(message.text.characters());
|
||||
printf("\033[0m");
|
||||
break;
|
||||
case JS::ConsoleMessageKind::Warn:
|
||||
printf("\033[33;1m");
|
||||
puts(message.text.characters());
|
||||
printf("\033[0m");
|
||||
break;
|
||||
case JS::ConsoleMessageKind::Error:
|
||||
printf("\033[31;1m");
|
||||
puts(message.text.characters());
|
||||
printf("\033[0m");
|
||||
break;
|
||||
case JS::ConsoleMessageKind::Clear:
|
||||
printf("\033[3J\033[H\033[2J");
|
||||
fflush(stdout);
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
bool gc_on_every_allocation = false;
|
||||
|
@ -446,7 +415,6 @@ int main(int argc, char** argv)
|
|||
|
||||
if (script_path == nullptr) {
|
||||
interpreter = JS::Interpreter::create<ReplObject>();
|
||||
interpreter->console().on_new_message = console_message_handler;
|
||||
interpreter->heap().set_should_collect_on_every_allocation(gc_on_every_allocation);
|
||||
if (test_mode)
|
||||
enable_test_mode(*interpreter);
|
||||
|
@ -671,7 +639,6 @@ int main(int argc, char** argv)
|
|||
repl(*interpreter);
|
||||
} else {
|
||||
interpreter = JS::Interpreter::create<JS::GlobalObject>();
|
||||
interpreter->console().on_new_message = console_message_handler;
|
||||
interpreter->heap().set_should_collect_on_every_allocation(gc_on_every_allocation);
|
||||
if (test_mode)
|
||||
enable_test_mode(*interpreter);
|
||||
|
|
Loading…
Add table
Reference in a new issue