LibJS: Add console.{debug,info,warn,error}()

This commit is contained in:
Linus Groh 2020-04-12 17:00:06 +01:00 committed by Andreas Kling
parent b1e8cc22bd
commit dd7796515f
Notes: sideshowbarker 2024-07-19 07:40:22 +09:00
3 changed files with 56 additions and 7 deletions

View file

@ -0,0 +1,5 @@
console.log("I am a generic log message");
console.debug("I am a debug log message");
console.info("I am an info log message");
console.warn("I am a warning log message");
console.error("I am an error log message");

View file

@ -1,5 +1,6 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2020, Linus Groh <mail@linusgroh.de>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -32,9 +33,23 @@
namespace JS {
static void print_args(Interpreter& interpreter)
{
for (size_t i = 0; i < interpreter.argument_count(); ++i) {
printf("%s", interpreter.argument(i).to_string().characters());
if (i != interpreter.argument_count() - 1)
putchar(' ');
}
putchar('\n');
}
ConsoleObject::ConsoleObject()
{
put_native_function("log", log);
put_native_function("debug", debug);
put_native_function("info", info);
put_native_function("warn", warn);
put_native_function("error", error);
put_native_function("trace", trace);
}
@ -44,18 +59,43 @@ ConsoleObject::~ConsoleObject()
Value ConsoleObject::log(Interpreter& interpreter)
{
for (size_t i = 0; i < interpreter.argument_count(); ++i) {
printf("%s", interpreter.argument(i).to_string().characters());
if (i != interpreter.argument_count() - 1)
putchar(' ');
print_args(interpreter);
return js_undefined();
}
putchar('\n');
Value ConsoleObject::debug(Interpreter& interpreter)
{
printf("\033[36;1m");
print_args(interpreter);
printf("\033[0m");
return js_undefined();
}
Value ConsoleObject::info(Interpreter& interpreter)
{
print_args(interpreter);
return js_undefined();
}
Value ConsoleObject::warn(Interpreter& interpreter)
{
printf("\033[33;1m");
print_args(interpreter);
printf("\033[0m");
return js_undefined();
}
Value ConsoleObject::error(Interpreter& interpreter)
{
printf("\033[31;1m");
print_args(interpreter);
printf("\033[0m");
return js_undefined();
}
Value ConsoleObject::trace(Interpreter& interpreter)
{
log(interpreter);
print_args(interpreter);
auto call_stack = interpreter.call_stack();
// -2 to skip the console.trace() call frame
for (ssize_t i = call_stack.size() - 2; i >= 0; --i) {

View file

@ -39,6 +39,10 @@ private:
virtual const char* class_name() const override { return "ConsoleObject"; }
static Value log(Interpreter&);
static Value debug(Interpreter&);
static Value info(Interpreter&);
static Value warn(Interpreter&);
static Value error(Interpreter&);
static Value trace(Interpreter&);
};