diff --git a/include/iotjs.h b/include/iotjs.h index a15070a863..8ec532d667 100644 --- a/include/iotjs.h +++ b/include/iotjs.h @@ -26,5 +26,7 @@ IOTJS_EXTERN_C int iotjs_entry(int argc, char** argv); +IOTJS_EXTERN_C void iotjs_conf_console_out(int (*fp)(int level, const char* fmt, + ...)); #endif /* IOTJS_IOTJS_H */ diff --git a/src/iotjs.c b/src/iotjs.c index f2800cf33a..be695aebe2 100644 --- a/src/iotjs.c +++ b/src/iotjs.c @@ -172,6 +172,10 @@ static void iotjs_uv_walk_to_close_callback(uv_handle_t* handle, void* arg) { iotjs_handlewrap_close(handle_wrap, NULL); } +void iotjs_conf_console_out(int (*out)(int lv, const char* fmt, ...)) { + iotjs_set_console_out(out); +} + int iotjs_entry(int argc, char** argv) { int ret_code = 0; diff --git a/src/iotjs_debuglog.c b/src/iotjs_debuglog.c index 22c2dcb673..e88f0c7bc2 100644 --- a/src/iotjs_debuglog.c +++ b/src/iotjs_debuglog.c @@ -18,6 +18,12 @@ #include "iotjs_debuglog.h" +iotjs_console_out_t iotjs_console_out = NULL; + +void iotjs_set_console_out(iotjs_console_out_t output) { + iotjs_console_out = output; +} + #ifdef ENABLE_DEBUG_LOG int iotjs_debug_level = DBGLEV_ERR; FILE* iotjs_log_stream; @@ -53,7 +59,6 @@ void iotjs_debuglog_init() { #endif // ENABLE_DEBUG_LOG } - void iotjs_debuglog_release() { #ifdef ENABLE_DEBUG_LOG if (iotjs_log_stream != stderr && iotjs_log_stream != stdout) { diff --git a/src/iotjs_debuglog.h b/src/iotjs_debuglog.h index 0812de5914..8629bcc654 100644 --- a/src/iotjs_debuglog.h +++ b/src/iotjs_debuglog.h @@ -16,6 +16,13 @@ #ifndef IOTJS_DEBUGLOG_H #define IOTJS_DEBUGLOG_H +#define DBGLEV_ERR 1 +#define DBGLEV_WARN 2 +#define DBGLEV_INFO 3 + +typedef int (*iotjs_console_out_t)(int level, const char* format, ...); +extern iotjs_console_out_t iotjs_console_out; +extern void iotjs_set_console_out(iotjs_console_out_t output); #ifdef ENABLE_DEBUG_LOG @@ -25,18 +32,18 @@ extern int iotjs_debug_level; extern FILE* iotjs_log_stream; extern const char* iotjs_debug_prefix[4]; -#define DBGLEV_ERR 1 -#define DBGLEV_WARN 2 -#define DBGLEV_INFO 3 - -#define IOTJS_DLOG(lvl, ...) \ - do { \ - if (0 <= lvl && lvl <= iotjs_debug_level && iotjs_log_stream) { \ - fprintf(iotjs_log_stream, "[%s] ", iotjs_debug_prefix[lvl]); \ - fprintf(iotjs_log_stream, __VA_ARGS__); \ - fprintf(iotjs_log_stream, "\n"); \ - fflush(iotjs_log_stream); \ - } \ +#define IOTJS_DLOG(lvl, ...) \ + do { \ + if (0 <= lvl && lvl <= iotjs_debug_level && iotjs_log_stream) { \ + if (iotjs_console_out) { \ + iotjs_console_out(lvl, __VA_ARGS__); \ + } else { \ + fprintf(iotjs_log_stream, "[%s] ", iotjs_debug_prefix[lvl]); \ + fprintf(iotjs_log_stream, __VA_ARGS__); \ + fprintf(iotjs_log_stream, "\n"); \ + fflush(iotjs_log_stream); \ + } \ + } \ } while (0) #define DLOG(...) IOTJS_DLOG(DBGLEV_ERR, __VA_ARGS__) #define DDLOG(...) IOTJS_DLOG(DBGLEV_WARN, __VA_ARGS__) diff --git a/src/modules/iotjs_module_console.c b/src/modules/iotjs_module_console.c index 2f7381a344..4aaa979c4b 100644 --- a/src/modules/iotjs_module_console.c +++ b/src/modules/iotjs_module_console.c @@ -14,6 +14,7 @@ */ #include "iotjs_def.h" +#include "iotjs_debuglog.h" // This function should be able to print utf8 encoded string // as utf8 is internal string representation in Jerryscript @@ -25,13 +26,19 @@ static jerry_value_t Print(const jerry_value_t* jargv, unsigned str_len = iotjs_string_size(&msg); unsigned idx = 0; - for (idx = 0; idx < str_len; idx++) { - if (str[idx] != 0) { - fprintf(out_fd, "%c", str[idx]); - } else { - fprintf(out_fd, "\\u0000"); + if (iotjs_console_out) { + int level = (out_fd == stdout) ? DBGLEV_INFO : DBGLEV_ERR; + iotjs_console_out(level, "%s", str); + } else { + for (idx = 0; idx < str_len; idx++) { + if (str[idx] != 0) { + fprintf(out_fd, "%c", str[idx]); + } else { + fprintf(out_fd, "\\u0000"); + } } } + iotjs_string_destroy(&msg); return jerry_create_undefined(); }