From 6952a239465811c26838e177d35222fd5229e393 Mon Sep 17 00:00:00 2001 From: Daniel Silverstone Date: Sun, 5 May 2019 20:50:21 +0100 Subject: Provide new browser_window_console_log() API Signed-off-by: Daniel Silverstone --- desktop/browser.c | 43 ++++++++++++++++++++++++++++++ include/netsurf/browser_window.h | 56 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+) diff --git a/desktop/browser.c b/desktop/browser.c index d26abd043..1a7493537 100644 --- a/desktop/browser.c +++ b/desktop/browser.c @@ -3427,3 +3427,46 @@ bool browser_window_exec(struct browser_window *bw, const char *src, size_t srcl */ return content_exec(bw->current_content, src, srclen); } + +/* exported interface documented in browser_window.h */ +nserror browser_window_console_log(struct browser_window *bw, + browser_window_console_source src, + const char *msg, + size_t msglen, + browser_window_console_flags flags) +{ + browser_window_console_flags log_level = flags & BW_CS_FLAG_LEVEL_MASK; + struct browser_window *root = browser_window_get_root(bw); + + assert(msg != NULL); + assert(msglen > 0); + + /* bw is the target of the log, but root is where we log it */ + + NSLOG(netsurf, DEEPDEBUG, "Logging message in %p targetted at %p", root, bw); + NSLOG(netsurf, DEEPDEBUG, "Log came from %s", + ((src == BW_CS_INPUT) ? "user input" : + (src == BW_CS_SCRIPT_ERROR) ? "script error" : + (src == BW_CS_SCRIPT_CONSOLE) ? "script console" : + "unknown input location")); + + switch (log_level) { + case BW_CS_FLAG_LEVEL_LOG: + NSLOG(netsurf, VERBOSE, "%.*s", (int)msglen, msg); + break; + case BW_CS_FLAG_LEVEL_INFO: + NSLOG(netsurf, INFO, "%.*s", (int)msglen, msg); + break; + case BW_CS_FLAG_LEVEL_WARN: + NSLOG(netsurf, WARNING, "%.*s", (int)msglen, msg); + break; + case BW_CS_FLAG_LEVEL_ERROR: + NSLOG(netsurf, ERROR, "%.*s", (int)msglen, msg); + break; + default: + /* Unreachable */ + break; + } + + return NSERROR_OK; +} diff --git a/include/netsurf/browser_window.h b/include/netsurf/browser_window.h index 77a263189..9c6fe7a31 100644 --- a/include/netsurf/browser_window.h +++ b/include/netsurf/browser_window.h @@ -143,6 +143,43 @@ struct browser_window_features { } form_features; }; +/** + * Sources of messages which end up in the browser window console + */ +typedef enum { + BW_CS_INPUT, /**< Input from the client */ + BW_CS_SCRIPT_ERROR, /**< Error from some running script */ + BW_CS_SCRIPT_CONSOLE, /**< Logging from some running script */ +} browser_window_console_source; + +/** + * Flags for browser window console logging. + * + * It is valid to bitwise-or some of these flags together where indicated. + */ +typedef enum { + /** + * The log entry is foldable. + * + * Set this to indicate that the text should be folded on the first + * newline on display. If this is set but there are no newlines in + * the logged text, the core will unset it before passing on to + * callbacks or storing the log entry. + */ + BW_CS_FLAG_FOLDABLE = 1 << 0, + + /** Logged at the 'log' level, please only use one of the LEVEL flags */ + BW_CS_FLAG_LEVEL_LOG = 0 << 1, + /** Logged at the 'info' level, please use only one of the LEVEL flags */ + BW_CS_FLAG_LEVEL_INFO = 1 << 1, + /** Logged at the 'warn' level, please use only one of the LEVEL flags */ + BW_CS_FLAG_LEVEL_WARN = 2 << 1, + /** Logged at the 'error' level, please use only one of the LEVEL flags */ + BW_CS_FLAG_LEVEL_ERROR = 3 << 1, + /** Mask for the error level to allow easy comparison using the above */ + BW_CS_FLAG_LEVEL_MASK = 3 << 1, +} browser_window_console_flags; + /** * Create and open a new root browser window with the given page. * @@ -737,4 +774,23 @@ nserror browser_window_set_name(struct browser_window *bw, const char *name); */ bool browser_window_exec(struct browser_window *bw, const char *src, size_t srclen); +/** + * Log a console message into the browser window console. + * + * If the targetted browser window is a frame, the message will be bubbled + * to the outermost window to be logged. + * + * \param bw The browser window + * \param src The source of the message + * \param msg The text of the message + * \param msglen The length of the text of the message + * \param flags Flags for the message + * \return Whether or not the logged message succeeded in being stored + */ +nserror browser_window_console_log(struct browser_window *bw, + browser_window_console_source src, + const char *msg, + size_t msglen, + browser_window_console_flags flags); + #endif -- cgit v1.2.3