diff options
Diffstat (limited to 'content/handlers/javascript/duktape/Console.bnd')
-rw-r--r-- | content/handlers/javascript/duktape/Console.bnd | 52 |
1 files changed, 37 insertions, 15 deletions
diff --git a/content/handlers/javascript/duktape/Console.bnd b/content/handlers/javascript/duktape/Console.bnd index c4c0c8399..89d13cfad 100644 --- a/content/handlers/javascript/duktape/Console.bnd +++ b/content/handlers/javascript/duktape/Console.bnd @@ -13,28 +13,50 @@ class Console { private unsigned int group; prologue %{ #include <nsutils/time.h> +#include "netsurf/browser_window.h" #define CONSOLE_TIMERS MAGIC(ConsoleTimers) static void -write_log_entry(duk_context *ctx, unsigned int group, char logtype) +write_log_entry(duk_context *ctx, unsigned int group, browser_window_console_flags flags) { /* objs... */ + dukky_push_generics(ctx, "consoleFormatter"); + duk_insert(ctx, 0); + if (dukky_pcall(ctx, duk_get_top(ctx) - 1, false)) { + /* Failed to convert somehow, oh dear, you get to keep + * all the pieces. + */ + duk_pop(ctx); + duk_push_string(ctx, "Oh dear, formatter went banananas"); + } + /* str?objs?... */ for (int i = 0; i < duk_get_top(ctx); ++i) { (void)duk_safe_to_string(ctx, i); } /* strs... */ - duk_push_sprintf(ctx, "%c: ", logtype); - duk_insert(ctx, 0); - /* pfx strs... */ for (unsigned int u = 0; u < group; ++u) { duk_push_lstring(ctx, " ", 1); duk_insert(ctx, 0); } - /* spcs... pfx strs... */ + /* spcs... strs... */ duk_concat(ctx, duk_get_top(ctx)); /* str */ - NSLOG(netsurf, INFO, "%s", duk_safe_to_string(ctx, 0)); + + duk_push_global_object(ctx); + duk_get_prop_string(ctx, -1, PRIVATE_MAGIC); + window_private_t *priv_win = duk_get_pointer(ctx, -1); + duk_pop(ctx); + + duk_size_t msglen; + const char *msg = duk_safe_to_lstring(ctx, 0, &msglen); + + if (priv_win == NULL || priv_win->win == NULL || priv_win->closed_down == true || + browser_window_console_log(priv_win->win, BW_CS_SCRIPT_CONSOLE, + msg, msglen, + flags) != NSERROR_OK) { + NSLOG(netsurf, DEBUG, "Unable to log: %s", duk_safe_to_string(ctx, 0)); + } } %}; @@ -68,37 +90,37 @@ method Console::groupEnd () method Console::info() %{ - write_log_entry(ctx, priv->group, 'I'); + write_log_entry(ctx, priv->group, BW_CS_FLAG_LEVEL_INFO); return 0; %} method Console::debug() %{ - write_log_entry(ctx, priv->group, 'D'); + write_log_entry(ctx, priv->group, BW_CS_FLAG_LEVEL_DEBUG); return 0; %} method Console::error() %{ - write_log_entry(ctx, priv->group, 'E'); + write_log_entry(ctx, priv->group, BW_CS_FLAG_LEVEL_ERROR); return 0; %} method Console::log() %{ - write_log_entry(ctx, priv->group, 'L'); + write_log_entry(ctx, priv->group, BW_CS_FLAG_LEVEL_LOG); return 0; %} method Console::warn() %{ - write_log_entry(ctx, priv->group, 'W'); + write_log_entry(ctx, priv->group, BW_CS_FLAG_LEVEL_WARN); return 0; %} method Console::dir() %{ - write_log_entry(ctx, priv->group, 'd'); + write_log_entry(ctx, priv->group, BW_CS_FLAG_LEVEL_INFO); return 0; %} @@ -160,8 +182,8 @@ method Console::timeEnd() duk_del_prop(ctx, 0); duk_push_string(ctx, "Timer elapsed: "); duk_insert(ctx, 0); - duk_push_sprintf(ctx, "%lu ms", (duk_uint_t)(time_ms - old_time_ms)); - write_log_entry(ctx, priv->group, 'T'); + duk_push_sprintf(ctx, "%u ms", (duk_uint_t)(time_ms - old_time_ms)); + write_log_entry(ctx, priv->group, BW_CS_FLAG_LEVEL_INFO); return 0; %} @@ -172,6 +194,6 @@ method Console::trace () duk_safe_to_string(ctx, -1); duk_insert(ctx, 0); duk_set_top(ctx, 1); - write_log_entry(ctx, priv->group, 'S'); + write_log_entry(ctx, priv->group, BW_CS_FLAG_LEVEL_INFO); return 0; %} |