Browse Source

Remove default logging framework from core sources

pull/746/head
Sami Vaarala 9 years ago
parent
commit
0f1c501830
  1. 120
      src/builtins.yaml
  2. 52
      src/duk_api_logging.c
  3. 15
      src/duk_api_public.h.in
  4. 21
      src/duk_bi_date.c
  5. 300
      src/duk_bi_logger.c
  6. 5
      src/duk_bi_protos.h
  7. 5
      src/duk_initjs.js
  8. 38
      src/strings.yaml

120
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

52
src/duk_api_logging.c

@ -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);
}

15
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
*/

21
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.
*

300
src/duk_bi_logger.c

@ -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: <time> <entryLev> <loggerName>: <msg> */
tot_len = 0;
tot_len += 3 + /* separators: space, space, colon */
3 + /* level string */
date_len + /* time */
duk_get_length(ctx, -1); /* loggerName */
for (i = 0; i < nargs; i++) {
/* When formatting an argument to a string, errors may happen from multiple
* causes. In general we want to catch obvious errors like a toLogString()
* throwing an error, but we don't currently try to catch every possible
* error. In particular, internal errors (like out of memory or stack) are
* not caught. Also, we expect Error toString() to not throw an error.
*/
if (duk_is_object(ctx, i)) {
/* duk_pcall_prop() may itself throw an error, but we're content
* in catching the obvious errors (like toLogString() throwing an
* error).
*/
duk_push_hstring_stridx(ctx, DUK_STRIDX_FMT);
duk_dup(ctx, i);
/* [ arg1 ... argN this loggerLevel loggerName 'fmt' arg ] */
/* call: this.fmt(arg) */
rc = duk_pcall_prop(ctx, -5 /*obj_index*/, 1 /*nargs*/);
if (rc) {
/* Keep the error as the result (coercing it might fail below,
* but we don't catch that now).
*/
;
}
duk_replace(ctx, i);
}
(void) duk_to_lstring(ctx, i, &arg_len);
tot_len++; /* sep (even before first one) */
tot_len += arg_len;
}
/*
* Pass 2
*/
/* XXX: There used to be a shared log buffer here, but it was removed
* when dynamic buffer spare was removed. The problem with using
* bufwriter is that, without the spare, the buffer gets passed on
* as an argument to the raw() call so it'd need to be resized
* (reallocated) anyway. If raw() call convention is changed, this
* could be made more efficient.
*/
buf = (duk_uint8_t *) duk_push_fixed_buffer(ctx, tot_len);
DUK_ASSERT(buf != NULL);
p = buf;
DUK_MEMCPY((void *) p, (const void *) date_buf, (size_t) date_len);
p += date_len;
*p++ = (duk_uint8_t) DUK_ASC_SPACE;
q = duk__log_level_strings + (entry_lev * 3);
DUK_MEMCPY((void *) p, (const void *) q, (size_t) 3);
p += 3;
*p++ = (duk_uint8_t) DUK_ASC_SPACE;
arg_str = (const duk_uint8_t *) duk_get_lstring(ctx, -2, &arg_len);
DUK_MEMCPY((void *) p, (const void *) arg_str, (size_t) arg_len);
p += arg_len;
*p++ = (duk_uint8_t) DUK_ASC_COLON;
for (i = 0; i < nargs; i++) {
*p++ = (duk_uint8_t) DUK_ASC_SPACE;
arg_str = (const duk_uint8_t *) duk_get_lstring(ctx, i, &arg_len);
DUK_ASSERT(arg_str != NULL);
DUK_MEMCPY((void *) p, (const void *) arg_str, (size_t) arg_len);
p += arg_len;
}
DUK_ASSERT(buf + tot_len == p);
/* [ arg1 ... argN this loggerLevel loggerName buffer ] */
#if defined(DUK_USE_DEBUGGER_SUPPORT) && defined(DUK_USE_DEBUGGER_FWD_LOGGING)
/* Do debugger forwarding before raw() because the raw() function
* doesn't get the log level right now.
*/
if (DUK_HEAP_IS_DEBUGGER_ATTACHED(thr->heap)) {
const char *log_buf;
duk_size_t sz_buf;
log_buf = (const char *) duk_get_buffer(ctx, -1, &sz_buf);
DUK_ASSERT(log_buf != NULL);
duk_debug_write_notify(thr, DUK_DBG_CMD_LOG);
duk_debug_write_int(thr, (duk_int32_t) entry_lev);
duk_debug_write_string(thr, (const char *) log_buf, sz_buf);
duk_debug_write_eom(thr);
}
#endif
/* Call this.raw(msg); look up through the instance allows user to override
* the raw() function in the instance or in the prototype for maximum
* flexibility.
*/
duk_push_hstring_stridx(ctx, DUK_STRIDX_RAW);
duk_dup(ctx, -2);
/* [ arg1 ... argN this loggerLevel loggerName buffer 'raw' buffer ] */
duk_call_prop(ctx, -6, 1); /* this.raw(buffer) */
return 0;
}

5
src/duk_bi_protos.h

@ -6,13 +6,13 @@
#ifndef DUK_BUILTIN_PROTOS_H_INCLUDED
#define DUK_BUILTIN_PROTOS_H_INCLUDED
/* Buffer size needed for duk_bi_date_format_timeval().
/* Buffer size needed for ISO 8601 formatting.
* Accurate value is 32 + 1 for NUL termination:
* >>> len('+123456-01-23T12:34:56.123+12:34')
* 32
* Include additional space to be safe.
*/
#define DUK_BI_DATE_ISO8601_BUFSIZE 48
#define DUK_BI_DATE_ISO8601_BUFSIZE 40
/* Maximum length of CommonJS module identifier to resolve. Length includes
* both current module ID, requested (possibly relative) module ID, and a
@ -23,7 +23,6 @@
/* Helpers exposed for internal use */
DUK_INTERNAL_DECL void duk_bi_date_timeval_to_parts(duk_double_t d, duk_int_t *parts, duk_double_t *dparts, duk_small_uint_t flags);
DUK_INTERNAL_DECL duk_double_t duk_bi_date_get_timeval_from_dparts(duk_double_t *dparts, duk_small_uint_t flags);
DUK_INTERNAL_DECL void duk_bi_date_format_timeval(duk_double_t timeval, duk_uint8_t *out_buf);
DUK_INTERNAL_DECL duk_bool_t duk_bi_date_is_leap_year(duk_int_t year);
DUK_INTERNAL_DECL duk_bool_t duk_bi_date_timeval_in_valid_range(duk_double_t x);
DUK_INTERNAL_DECL duk_bool_t duk_bi_date_year_in_valid_range(duk_double_t year);

5
src/duk_initjs.js

@ -18,11 +18,6 @@
def(D, name, value);
}
// Logger object for C code provided by init code now.
if (true) {
def(D.Logger, 'clog', new D.Logger('C'));
}
// Tracking table for CommonJS module loading.
if (true) {
def(D, 'modLoaded', {});

38
src/strings.yaml

@ -694,8 +694,6 @@ strings:
- str: "Thread"
duktape: true
class_name: true
- str: "Logger"
duktape: true
# non-standard built-in object names
- str: "ThrowTypeError" # implementation specific, matches V8
@ -884,38 +882,6 @@ strings:
# Thread prototype
# Logger constructor
# Logger prototype and logger instances
- str: "fmt"
duktape: true
- str: "raw"
duktape: true
- str: "trace"
duktape: true
- str: "debug"
duktape: true
- str: "info"
duktape: true
- str: "warn"
duktape: true
- str: "error"
duktape: true
- str: "fatal"
duktape: true
- str: "n"
duktape: true
- str: "l"
duktape: true
# Auxiliary logger strings
- str: "clog" # C logger
duktape: true
# for controlling log formatting of objects
- str: "toLogString"
duktape: true
# special literals for custom json encodings
- str: "{\"_undef\":true}"
duktape: true
@ -1151,10 +1117,6 @@ special_define_names:
"Pointer": "UC_POINTER"
#"thread": "LC_THREAD"
"Thread": "UC_THREAD"
#"logger": "LC_LOGGER"
"Logger": "UC_LOGGER"
"n": "LC_N"
"l": "LC_L"
"error": "LC_ERROR"
"Error": "UC_ERROR"

Loading…
Cancel
Save