diff --git a/src/builtins.yaml b/src/builtins.yaml index 8098ba7a..25f4788d 100644 --- a/src/builtins.yaml +++ b/src/builtins.yaml @@ -2194,11 +2194,6 @@ objects: type: object id: bi_thread_constructor duktape: true - - key: "Logger" - value: - type: object - id: bi_logger_constructor - duktape: true - key: "info" value: @@ -2419,117 +2414,6 @@ objects: magic: 0 # magic = to_string duktape: true - - id: bi_logger_constructor - class: Function - internal_prototype: bi_function_prototype - varargs: true - native: duk_bi_logger_constructor - callable: true - constructable: true - duktape: true - - properties: - - key: "length" - value: 1 - attributes: "" - duktape: true - - key: "prototype" - value: - type: object - id: bi_logger_prototype - attributes: "" - duktape: true - - key: "name" - value: "Logger" - attributes: "" - duktape: true - - - id: bi_logger_prototype - class: Object - internal_prototype: bi_object_prototype - duktape: true - - properties: - - key: "constructor" - value: - type: object - id: bi_logger_constructor - attributes: "wc" - duktape: true - - # default log level: 2 = info - - key: "l" - value: 2 - attributes: "w" - duktape: true - - # default logger name (if undefined given or fileName of caller not known) - - key: "n" - value: "anon" - attributes: "w" - duktape: true - - - key: "fmt" - value: - type: function - native: duk_bi_logger_prototype_fmt - length: 1 - duktape: true - - key: "raw" - value: - type: function - native: duk_bi_logger_prototype_raw - length: 1 - duktape: true - - key: "trace" - value: - type: function - native: duk_bi_logger_prototype_log_shared - length: 0 - varargs: true - magic: 0 - duktape: true - - key: "debug" - value: - type: function - native: duk_bi_logger_prototype_log_shared - length: 0 - varargs: true - magic: 1 - duktape: true - - key: "info" - value: - type: function - native: duk_bi_logger_prototype_log_shared - length: 0 - varargs: true - magic: 2 - duktape: true - - key: "warn" - value: - type: function - native: duk_bi_logger_prototype_log_shared - length: 0 - varargs: true - magic: 3 - duktape: true - - key: "error" - value: - type: function - native: duk_bi_logger_prototype_log_shared - length: 0 - varargs: true - magic: 4 - duktape: true - - key: "fatal" - value: - type: function - native: duk_bi_logger_prototype_log_shared - length: 0 - varargs: true - magic: 5 - duktape: true - # This is an Error *instance* used to avoid allocation when a "double error" occurs. # The object is "frozen and sealed" to avoid code accidentally modifying the instance. # This is important because the error is rethrown as is. @@ -4098,10 +3982,6 @@ builtins: duktape: true - id: bi_pointer_prototype duktape: true - - id: bi_logger_constructor - duktape: true - - id: bi_logger_prototype - duktape: true - id: bi_double_error duktape: true diff --git a/src/duk_api_logging.c b/src/duk_api_logging.c deleted file mode 100644 index aa9784f3..00000000 --- a/src/duk_api_logging.c +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Logging - * - * Current logging primitive is a sprintf-style log which is convenient - * for most C code. Another useful primitive would be to log N arguments - * from value stack (like the Ecmascript binding does). - */ - -#include "duk_internal.h" - -DUK_EXTERNAL void duk_log_va(duk_context *ctx, duk_int_t level, const char *fmt, va_list ap) { - /* stridx_logfunc[] must be static to allow initializer with old compilers like BCC */ - static const duk_uint16_t stridx_logfunc[6] = { - DUK_STRIDX_LC_TRACE, DUK_STRIDX_LC_DEBUG, DUK_STRIDX_LC_INFO, - DUK_STRIDX_LC_WARN, DUK_STRIDX_LC_ERROR, DUK_STRIDX_LC_FATAL - }; - - DUK_ASSERT_CTX_VALID(ctx); - - if (level < 0) { - level = 0; - } else if (level > (int) (sizeof(stridx_logfunc) / sizeof(duk_uint16_t)) - 1) { - level = (int) (sizeof(stridx_logfunc) / sizeof(duk_uint16_t)) - 1; - } - - duk_push_hobject_bidx(ctx, DUK_BIDX_LOGGER_CONSTRUCTOR); - duk_get_prop_stridx(ctx, -1, DUK_STRIDX_CLOG); - duk_get_prop_stridx(ctx, -1, stridx_logfunc[level]); - duk_dup(ctx, -2); - - /* [ ... Logger clog logfunc clog ] */ - - duk_push_vsprintf(ctx, fmt, ap); - - /* [ ... Logger clog logfunc clog(=this) msg ] */ - - duk_call_method(ctx, 1 /*nargs*/); - - /* [ ... Logger clog res ] */ - - duk_pop_3(ctx); -} - -DUK_EXTERNAL void duk_log(duk_context *ctx, duk_int_t level, const char *fmt, ...) { - va_list ap; - - DUK_ASSERT_CTX_VALID(ctx); - - va_start(ap, fmt); - duk_log_va(ctx, level, fmt, ap); - va_end(ap); -} diff --git a/src/duk_api_public.h.in b/src/duk_api_public.h.in index 2032edf5..de0d6159 100644 --- a/src/duk_api_public.h.in +++ b/src/duk_api_public.h.in @@ -247,14 +247,6 @@ struct duk_time_components { #define DUK_EXEC_SUCCESS 0 #define DUK_EXEC_ERROR 1 -/* Log levels */ -#define DUK_LOG_TRACE 0 -#define DUK_LOG_DEBUG 1 -#define DUK_LOG_INFO 2 -#define DUK_LOG_WARN 3 -#define DUK_LOG_ERROR 4 -#define DUK_LOG_FATAL 5 - /* * If no variadic macros, __FILE__ and __LINE__ are passed through globals * which is ugly and not thread safe. @@ -897,13 +889,6 @@ DUK_EXTERNAL_DECL duk_int_t duk_compile_raw(duk_context *ctx, const char *src_bu DUK_EXTERNAL_DECL void duk_dump_function(duk_context *ctx); DUK_EXTERNAL_DECL void duk_load_function(duk_context *ctx); -/* - * Logging - */ - -DUK_EXTERNAL_DECL void duk_log(duk_context *ctx, duk_int_t level, const char *fmt, ...); -DUK_EXTERNAL_DECL void duk_log_va(duk_context *ctx, duk_int_t level, const char *fmt, va_list ap); - /* * Debugging */ diff --git a/src/duk_bi_date.c b/src/duk_bi_date.c index 1d5c7b7e..e7012dab 100644 --- a/src/duk_bi_date.c +++ b/src/duk_bi_date.c @@ -1266,27 +1266,6 @@ DUK_LOCAL void duk__set_parts_from_args(duk_context *ctx, duk_double_t *dparts, (double) dparts[6], (double) dparts[7])); } -/* - * Helper to format a time value into caller buffer, used by logging. - * 'out_buf' must be at least DUK_BI_DATE_ISO8601_BUFSIZE long. - */ - -DUK_INTERNAL void duk_bi_date_format_timeval(duk_double_t timeval, duk_uint8_t *out_buf) { - duk_int_t parts[DUK_DATE_IDX_NUM_PARTS]; - - duk_bi_date_timeval_to_parts(timeval, - parts, - NULL, - DUK_DATE_FLAG_ONEBASED); - - duk__format_parts_iso8601(parts, - 0 /*tzoffset*/, - DUK_DATE_FLAG_TOSTRING_DATE | - DUK_DATE_FLAG_TOSTRING_TIME | - DUK_DATE_FLAG_SEP_T /*flags*/, - out_buf); -} - /* * Indirect magic value lookup for Date methods. * diff --git a/src/duk_bi_logger.c b/src/duk_bi_logger.c deleted file mode 100644 index d2b89e5c..00000000 --- a/src/duk_bi_logger.c +++ /dev/null @@ -1,300 +0,0 @@ -/* - * Logging support - */ - -#include "duk_internal.h" - -/* 3-letter log level strings */ -DUK_LOCAL const duk_uint8_t duk__log_level_strings[] = { - (duk_uint8_t) DUK_ASC_UC_T, (duk_uint8_t) DUK_ASC_UC_R, (duk_uint8_t) DUK_ASC_UC_C, - (duk_uint8_t) DUK_ASC_UC_D, (duk_uint8_t) DUK_ASC_UC_B, (duk_uint8_t) DUK_ASC_UC_G, - (duk_uint8_t) DUK_ASC_UC_I, (duk_uint8_t) DUK_ASC_UC_N, (duk_uint8_t) DUK_ASC_UC_F, - (duk_uint8_t) DUK_ASC_UC_W, (duk_uint8_t) DUK_ASC_UC_R, (duk_uint8_t) DUK_ASC_UC_N, - (duk_uint8_t) DUK_ASC_UC_E, (duk_uint8_t) DUK_ASC_UC_R, (duk_uint8_t) DUK_ASC_UC_R, - (duk_uint8_t) DUK_ASC_UC_F, (duk_uint8_t) DUK_ASC_UC_T, (duk_uint8_t) DUK_ASC_UC_L -}; - -/* Constructor */ -DUK_INTERNAL duk_ret_t duk_bi_logger_constructor(duk_context *ctx) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_idx_t nargs; - - /* Calling as a non-constructor is not meaningful. */ - if (!duk_is_constructor_call(ctx)) { - return DUK_RET_TYPE_ERROR; - } - - nargs = duk_get_top(ctx); - duk_set_top(ctx, 1); - - duk_push_this(ctx); - - /* [ name this ] */ - - if (nargs == 0) { - /* Automatic defaulting of logger name from caller. This would - * work poorly with tail calls, but constructor calls are currently - * never tail calls, so tail calls are not an issue now. - */ - - if (thr->callstack_top >= 2) { - duk_activation *act_caller = thr->callstack + thr->callstack_top - 2; - duk_hobject *func_caller; - - func_caller = DUK_ACT_GET_FUNC(act_caller); - if (func_caller) { - /* Stripping the filename might be a good idea - * ("/foo/bar/quux.js" -> logger name "quux"), - * but now used verbatim. - */ - duk_push_hobject(ctx, func_caller); - duk_get_prop_stridx(ctx, -1, DUK_STRIDX_FILE_NAME); - duk_replace(ctx, 0); - } - } - } - /* the stack is unbalanced here on purpose; we only rely on the - * initial two values: [ name this ]. - */ - - if (duk_is_string(ctx, 0)) { - duk_dup(ctx, 0); - duk_put_prop_stridx(ctx, 1, DUK_STRIDX_LC_N); - } else { - /* don't set 'n' at all, inherited value is used as name */ - } - - duk_compact(ctx, 1); - - return 0; /* keep default instance */ -} - -/* Default function to format objects. Tries to use toLogString() but falls - * back to toString(). Any errors are propagated out without catching. - */ -DUK_INTERNAL duk_ret_t duk_bi_logger_prototype_fmt(duk_context *ctx) { - if (duk_get_prop_stridx(ctx, 0, DUK_STRIDX_TO_LOG_STRING)) { - /* [ arg toLogString ] */ - - duk_dup(ctx, 0); - duk_call_method(ctx, 0); - - /* [ arg result ] */ - return 1; - } - - /* [ arg undefined ] */ - duk_pop(ctx); - duk_to_string(ctx, 0); - return 1; -} - -/* Default function to write a formatted log line. Writes to stderr, - * appending a newline to the log line. - * - * The argument is a buffer whose visible size contains the log message. - * This function should avoid coercing the buffer to a string to avoid - * string table traffic. - */ -DUK_INTERNAL duk_ret_t duk_bi_logger_prototype_raw(duk_context *ctx) { - const char *data; - duk_size_t data_len; - - DUK_UNREF(ctx); - DUK_UNREF(data); - DUK_UNREF(data_len); - -#ifdef DUK_USE_FILE_IO - data = (const char *) duk_require_buffer(ctx, 0, &data_len); - DUK_FWRITE((const void *) data, 1, data_len, DUK_STDERR); - DUK_FPUTC((int) '\n', DUK_STDERR); - DUK_FFLUSH(DUK_STDERR); -#else - /* nop */ -#endif - return 0; -} - -/* Log frontend shared helper, magic value indicates log level. Provides - * frontend functions: trace(), debug(), info(), warn(), error(), fatal(). - * This needs to have small footprint, reasonable performance, minimal - * memory churn, etc. - */ -DUK_INTERNAL duk_ret_t duk_bi_logger_prototype_log_shared(duk_context *ctx) { - duk_hthread *thr = (duk_hthread *) ctx; - duk_double_t now; - duk_small_int_t entry_lev = duk_get_current_magic(ctx); - duk_small_int_t logger_lev; - duk_int_t nargs; - duk_int_t i; - duk_size_t tot_len; - const duk_uint8_t *arg_str; - duk_size_t arg_len; - duk_uint8_t *buf, *p; - const duk_uint8_t *q; - duk_uint8_t date_buf[DUK_BI_DATE_ISO8601_BUFSIZE]; - duk_size_t date_len; - duk_small_int_t rc; - - DUK_ASSERT(entry_lev >= 0 && entry_lev <= 5); - DUK_UNREF(thr); - - /* XXX: sanitize to printable (and maybe ASCII) */ - /* XXX: better multiline */ - - /* - * Logger arguments are: - * - * magic: log level (0-5) - * this: logger - * stack: plain log args - * - * We want to minimize memory churn so a two-pass approach - * is used: first pass formats arguments and computes final - * string length, second pass copies strings either into a - * pre-allocated and reused buffer (short messages) or into a - * newly allocated fixed buffer. If the backend function plays - * nice, it won't coerce the buffer to a string (and thus - * intern it). - */ - - nargs = duk_get_top(ctx); - - /* [ arg1 ... argN this ] */ - - /* - * Log level check - */ - - duk_push_this(ctx); - - duk_get_prop_stridx(ctx, -1, DUK_STRIDX_LC_L); - logger_lev = (duk_small_int_t) duk_get_int(ctx, -1); - if (entry_lev < logger_lev) { - return 0; - } - /* log level could be popped but that's not necessary */ - - now = DUK_USE_DATE_GET_NOW(ctx); - duk_bi_date_format_timeval(now, date_buf); - date_len = DUK_STRLEN((const char *) date_buf); - - duk_get_prop_stridx(ctx, -2, DUK_STRIDX_LC_N); - duk_to_string(ctx, -1); - DUK_ASSERT(duk_is_string(ctx, -1)); - - /* [ arg1 ... argN this loggerLevel loggerName ] */ - - /* - * Pass 1 - */ - - /* Line format: