details: https://hg.nginx.org/njs/rev/fc1001f6801b branches: changeset: 2249:fc1001f6801b user: Dmitry Volyntsev <xei...@nginx.com> date: Thu Dec 14 22:32:02 2023 -0800 description: Moving out logger from njs core.
Logger is not part of the JS runtime according to ECMAScript and should be implemented by host environment. diffstat: external/njs_fs_module.c | 1 - external/njs_shell.c | 70 ++++++++++++++++++++++++++++--------------- nginx/ngx_http_js_module.c | 11 +----- nginx/ngx_js.c | 40 ++++++++++++++++++------ nginx/ngx_js.h | 6 ++- nginx/ngx_stream_js_module.c | 6 --- src/njs.h | 26 ---------------- src/njs_builtin.c | 9 +----- src/njs_vm.c | 25 --------------- 9 files changed, 82 insertions(+), 112 deletions(-) diffs (493 lines): diff -r ad1a7ad3c715 -r fc1001f6801b external/njs_fs_module.c --- a/external/njs_fs_module.c Wed Dec 13 18:38:47 2023 -0800 +++ b/external/njs_fs_module.c Thu Dec 14 22:32:02 2023 -0800 @@ -3668,7 +3668,6 @@ njs_fs_filehandle_cleanup(void *data) njs_filehandle_t *fh = data; if (fh->vm != NULL && fh->fd != -1) { - njs_vm_warn(fh->vm, "closing file description %d on cleanup\n", fh->fd); (void) close(fh->fd); } } diff -r ad1a7ad3c715 -r fc1001f6801b external/njs_shell.c --- a/external/njs_shell.c Wed Dec 13 18:38:47 2023 -0800 +++ b/external/njs_shell.c Thu Dec 14 22:32:02 2023 -0800 @@ -53,6 +53,13 @@ typedef struct { } njs_opts_t; +typedef enum { + NJS_LOG_ERROR = 4, + NJS_LOG_WARN = 5, + NJS_LOG_INFO = 7, +} njs_log_level_t; + + typedef struct { size_t index; size_t length; @@ -137,8 +144,9 @@ static njs_int_t njs_ext_console_time(nj static njs_int_t njs_ext_console_time_end(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs, njs_index_t unused, njs_value_t *retval); -static void njs_console_log(njs_vm_t *vm, njs_external_ptr_t external, - njs_log_level_t level, const u_char *start, size_t length); +static void njs_console_log(njs_log_level_t level, const char *fmt, ...); +static void njs_console_logger(njs_log_level_t level, const u_char *start, + size_t length); static intptr_t njs_event_rbtree_compare(njs_rbtree_node_t *node1, njs_rbtree_node_t *node2); @@ -159,7 +167,7 @@ static njs_external_t njs_ext_console[] .native = njs_ext_console_log, #define NJS_LOG_DUMP 16 #define NJS_LOG_MASK 15 - .magic8 = NJS_LOG_LEVEL_INFO | NJS_LOG_DUMP, + .magic8 = NJS_LOG_INFO | NJS_LOG_DUMP, } }, @@ -171,7 +179,7 @@ static njs_external_t njs_ext_console[] .enumerable = 1, .u.method = { .native = njs_ext_console_log, - .magic8 = NJS_LOG_LEVEL_ERROR, + .magic8 = NJS_LOG_ERROR, } }, @@ -183,7 +191,7 @@ static njs_external_t njs_ext_console[] .enumerable = 1, .u.method = { .native = njs_ext_console_log, - .magic8 = NJS_LOG_LEVEL_INFO, + .magic8 = NJS_LOG_INFO, } }, @@ -195,7 +203,7 @@ static njs_external_t njs_ext_console[] .enumerable = 1, .u.method = { .native = njs_ext_console_log, - .magic8 = NJS_LOG_LEVEL_INFO, + .magic8 = NJS_LOG_INFO, } }, @@ -237,7 +245,7 @@ static njs_external_t njs_ext_console[] .enumerable = 1, .u.method = { .native = njs_ext_console_log, - .magic8 = NJS_LOG_LEVEL_WARN, + .magic8 = NJS_LOG_WARN, } }, @@ -268,11 +276,6 @@ static njs_external_t njs_ext_262[] = { }; -static njs_vm_ops_t njs_console_ops = { - njs_console_log, -}; - - njs_module_t njs_console_module = { .name = njs_str("console"), .preinit = NULL, @@ -776,7 +779,6 @@ njs_create_vm(njs_opts_t *opts) vm_options.opcode_debug = opts->opcode_debug; #endif - vm_options.ops = &njs_console_ops; vm_options.addons = njs_console_addon_modules; vm_options.external = &njs_console; vm_options.argv = opts->argv; @@ -1419,7 +1421,7 @@ njs_ext_console_log(njs_vm_t *vm, njs_va return NJS_ERROR; } - njs_vm_logger(vm, level, "%*s\n", msg.length, msg.start); + njs_console_logger(level, msg.start, msg.length); n++; } @@ -1475,7 +1477,8 @@ njs_ext_console_time(njs_vm_t *vm, njs_v label = njs_queue_link_data(link, njs_timelabel_t, link); if (njs_strstr_eq(&name, &label->name)) { - njs_vm_log(vm, "Timer \"%V\" already exists.\n", &name); + njs_console_log(NJS_LOG_INFO, "Timer \"%V\" already exists.", + &name); njs_value_undefined_set(retval); return NJS_OK; } @@ -1546,7 +1549,8 @@ njs_ext_console_time_end(njs_vm_t *vm, n for ( ;; ) { if (link == njs_queue_tail(labels)) { - njs_vm_log(vm, "Timer \"%V\" doesn’t exist.\n", &name); + njs_console_log(NJS_LOG_INFO, "Timer \"%V\" doesn’t exist.", + &name); njs_value_undefined_set(retval); return NJS_OK; } @@ -1566,7 +1570,7 @@ njs_ext_console_time_end(njs_vm_t *vm, n ms = ns / 1000000; ns = ns % 1000000; - njs_vm_log(vm, "%V: %uL.%06uLms\n", &name, ms, ns); + njs_console_log(NJS_LOG_INFO, "%V: %uL.%06uLms", &name, ms, ns); njs_mp_free(njs_vm_memory_pool(vm), label); @@ -1693,20 +1697,36 @@ njs_clear_timeout(njs_vm_t *vm, njs_valu static void -njs_console_log(njs_vm_t *vm, njs_external_ptr_t external, - njs_log_level_t level, const u_char *start, size_t length) +njs_console_log(njs_log_level_t level, const char *fmt, ...) +{ + u_char *p; + va_list args; + u_char buf[2048]; + + va_start(args, fmt); + p = njs_vsprintf(buf, buf + sizeof(buf), fmt, args); + va_end(args); + + njs_console_logger(level, buf, p - buf); +} + + +static void +njs_console_logger(njs_log_level_t level, const u_char *start, size_t length) { switch (level) { - case NJS_LOG_LEVEL_INFO: - njs_printf("%*s", length, start); + case NJS_LOG_WARN: + njs_printf("W: "); break; - case NJS_LOG_LEVEL_WARN: - njs_printf("W: %*s", length, start); + case NJS_LOG_ERROR: + njs_printf("E: "); break; - case NJS_LOG_LEVEL_ERROR: - njs_printf("E: %*s", length, start); + case NJS_LOG_INFO: break; } + + njs_print(start, length); + njs_print("\n", 1); } diff -r ad1a7ad3c715 -r fc1001f6801b nginx/ngx_http_js_module.c --- a/nginx/ngx_http_js_module.c Wed Dec 13 18:38:47 2023 -0800 +++ b/nginx/ngx_http_js_module.c Thu Dec 14 22:32:02 2023 -0800 @@ -839,11 +839,6 @@ static njs_external_t ngx_http_js_ext_p }; -static njs_vm_ops_t ngx_http_js_ops = { - ngx_js_logger, -}; - - static uintptr_t ngx_http_js_uptr[] = { offsetof(ngx_http_request_t, connection), (uintptr_t) ngx_http_js_pool, @@ -1660,8 +1655,9 @@ ngx_http_js_ext_header_out(njs_vm_t *vm, } if (r->header_sent && setval != NULL) { - njs_vm_warn(vm, "ignored setting of response header \"%V\" because" - " headers were already sent", &name); + ngx_log_error(NGX_LOG_WARN, r->connection->log, 0, + "ignored setting of response header \"%V\" because" + " headers were already sent", &name); } for (h = headers_out; h->name.length > 0; h++) { @@ -4509,7 +4505,6 @@ ngx_http_js_init_conf_vm(ngx_conf_t *cf, options.backtrace = 1; options.unhandled_rejection = NJS_VM_OPT_UNHANDLED_REJECTION_THROW; - options.ops = &ngx_http_js_ops; options.metas = &ngx_http_js_metas; options.addons = njs_http_js_addon_modules; options.argv = ngx_argv; diff -r ad1a7ad3c715 -r fc1001f6801b nginx/ngx_js.c --- a/nginx/ngx_js.c Wed Dec 13 18:38:47 2023 -0800 +++ b/nginx/ngx_js.c Thu Dec 14 22:32:02 2023 -0800 @@ -787,11 +787,10 @@ njs_int_t ngx_js_ext_log(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs, njs_index_t magic, njs_value_t *retval) { - char *p; - ngx_int_t lvl; - njs_str_t msg; - njs_uint_t n; - njs_log_level_t level; + char *p; + ngx_int_t lvl; + njs_str_t msg; + njs_uint_t n, level; p = njs_vm_external(vm, NJS_PROTO_ID_ANY, njs_argument(args, 0)); if (p == NULL) { @@ -799,7 +798,7 @@ ngx_js_ext_log(njs_vm_t *vm, njs_value_t return NJS_ERROR; } - level = (njs_log_level_t) magic & NGX_JS_LOG_MASK; + level = magic & NGX_JS_LOG_MASK; if (level == 0) { if (ngx_js_integer(vm, njs_arg(args, nargs, 1), &lvl) != NGX_OK) { @@ -892,7 +891,8 @@ ngx_js_ext_console_time(njs_vm_t *vm, nj label = ngx_queue_data(q, ngx_js_timelabel_t, queue); if (njs_strstr_eq(&name, &label->name)) { - njs_vm_log(vm, "Timer \"%V\" already exists.\n", &name); + ngx_js_log(vm, njs_vm_external_ptr(vm), NGX_LOG_INFO, + "Timer \"%V\" already exists.", &name); njs_value_undefined_set(retval); return NJS_OK; } @@ -988,7 +988,8 @@ ngx_js_ext_console_time_end(njs_vm_t *vm ms = ns / 1000000; ns = ns % 1000000; - njs_vm_log(vm, "%V: %uL.%06uLms\n", &name, ms, ns); + ngx_js_log(vm, njs_vm_external_ptr(vm), NGX_LOG_INFO, "%V: %uL.%06uLms", + &name, ms, ns); njs_value_undefined_set(retval); @@ -996,7 +997,8 @@ ngx_js_ext_console_time_end(njs_vm_t *vm not_found: - njs_vm_log(vm, "Timer \"%V\" doesn't exist.\n", &name); + ngx_js_log(vm, njs_vm_external_ptr(vm), NGX_LOG_INFO, + "Timer \"%V\" doesn't exist.", &name); njs_value_undefined_set(retval); @@ -1152,7 +1154,23 @@ njs_clear_timeout(njs_vm_t *vm, njs_valu void -ngx_js_logger(njs_vm_t *vm, njs_external_ptr_t external, njs_log_level_t level, +ngx_js_log(njs_vm_t *vm, njs_external_ptr_t external, ngx_uint_t level, + const char *fmt, ...) +{ + u_char *p; + va_list args; + u_char buf[NGX_MAX_ERROR_STR]; + + va_start(args, fmt); + p = njs_vsprintf(buf, buf + sizeof(buf), fmt, args); + va_end(args); + + ngx_js_logger(vm, external, level, buf, p - buf); +} + + +void +ngx_js_logger(njs_vm_t *vm, njs_external_ptr_t external, ngx_uint_t level, const u_char *start, size_t length) { ngx_log_t *log; @@ -1174,7 +1192,7 @@ ngx_js_logger(njs_vm_t *vm, njs_external log = ngx_cycle->log; } - ngx_log_error((ngx_uint_t) level, log, 0, "js: %*s", length, start); + ngx_log_error(level, log, 0, "js: %*s", length, start); if (external != NULL) { log->handler = handler; diff -r ad1a7ad3c715 -r fc1001f6801b nginx/ngx_js.h --- a/nginx/ngx_js.h Wed Dec 13 18:38:47 2023 -0800 +++ b/nginx/ngx_js.h Thu Dec 14 22:32:02 2023 -0800 @@ -192,8 +192,10 @@ ngx_int_t ngx_js_exception(njs_vm_t *vm, njs_int_t ngx_js_ext_log(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs, njs_index_t level, njs_value_t *retval); -void ngx_js_logger(njs_vm_t *vm, njs_external_ptr_t external, - njs_log_level_t level, const u_char *start, size_t length); +void ngx_js_log(njs_vm_t *vm, njs_external_ptr_t external, + ngx_uint_t level, const char *fmt, ...); +void ngx_js_logger(njs_vm_t *vm, njs_external_ptr_t external, ngx_uint_t level, + const u_char *start, size_t length); char * ngx_js_import(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); char * ngx_js_preload_object(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); ngx_int_t ngx_js_init_preload_vm(ngx_conf_t *cf, ngx_js_loc_conf_t *conf); diff -r ad1a7ad3c715 -r fc1001f6801b nginx/ngx_stream_js_module.c --- a/nginx/ngx_stream_js_module.c Wed Dec 13 18:38:47 2023 -0800 +++ b/nginx/ngx_stream_js_module.c Thu Dec 14 22:32:02 2023 -0800 @@ -608,11 +608,6 @@ static njs_external_t ngx_stream_js_ext }; -static njs_vm_ops_t ngx_stream_js_ops = { - ngx_js_logger, -}; - - static uintptr_t ngx_stream_js_uptr[] = { offsetof(ngx_stream_session_t, connection), (uintptr_t) ngx_stream_js_pool, @@ -1784,7 +1779,6 @@ ngx_stream_js_init_conf_vm(ngx_conf_t *c options.backtrace = 1; options.unhandled_rejection = NJS_VM_OPT_UNHANDLED_REJECTION_THROW; - options.ops = &ngx_stream_js_ops; options.metas = &ngx_stream_js_metas; options.addons = njs_stream_js_addon_modules; options.argv = ngx_argv; diff -r ad1a7ad3c715 -r fc1001f6801b src/njs.h --- a/src/njs.h Wed Dec 13 18:38:47 2023 -0800 +++ b/src/njs.h Thu Dec 14 22:32:02 2023 -0800 @@ -46,12 +46,6 @@ typedef struct { uint64_t filler[2]; } njs_opaque_value_t; -typedef enum { - NJS_LOG_LEVEL_ERROR = 4, - NJS_LOG_LEVEL_WARN = 5, - NJS_LOG_LEVEL_INFO = 7, -} njs_log_level_t; - /* sizeof(njs_value_t) is 16 bytes. */ #define njs_argument(args, n) \ (njs_value_t *) ((u_char *) args + (n) * 16) @@ -72,13 +66,6 @@ extern const njs_value_t njs_ ((n < nargs) ? njs_argument(args, n) \ : (njs_value_assign(lvalue, &njs_value_undefined), lvalue)) -#define njs_vm_log(vm, fmt, ...) \ - njs_vm_logger(vm, NJS_LOG_LEVEL_INFO, fmt, ##__VA_ARGS__) -#define njs_vm_warn(vm, fmt, ...) \ - njs_vm_logger(vm, NJS_LOG_LEVEL_WARN, fmt, ##__VA_ARGS__) -#define njs_vm_err(vm, fmt, ...) \ - njs_vm_logger(vm, NJS_LOG_LEVEL_ERROR, fmt, ##__VA_ARGS__) - #define njs_vm_error(vm, fmt, ...) \ njs_vm_error2(vm, 0, fmt, ##__VA_ARGS__) #define njs_vm_internal_error(vm, fmt, ...) \ @@ -209,13 +196,6 @@ typedef void * njs_ typedef njs_mod_t *(*njs_module_loader_t)(njs_vm_t *vm, njs_external_ptr_t external, njs_str_t *name); -typedef void (*njs_logger_t)(njs_vm_t *vm, njs_external_ptr_t external, - njs_log_level_t level, const u_char *start, size_t length); - - -typedef struct { - njs_logger_t logger; -} njs_vm_ops_t; typedef struct { @@ -236,7 +216,6 @@ typedef struct { typedef struct { njs_external_ptr_t external; njs_vm_shared_t *shared; - njs_vm_ops_t *ops; njs_vm_meta_t *metas; njs_module_t **addons; njs_str_t file; @@ -246,8 +225,6 @@ typedef struct { njs_uint_t max_stack_size; - njs_log_level_t log_level; - #define NJS_VM_OPT_UNHANDLED_REJECTION_IGNORE 0 #define NJS_VM_OPT_UNHANDLED_REJECTION_THROW 1 @@ -476,9 +453,6 @@ NJS_EXPORT njs_int_t njs_vm_value_dump(n NJS_EXPORT void njs_vm_memory_error(njs_vm_t *vm); -NJS_EXPORT void njs_vm_logger(njs_vm_t *vm, njs_log_level_t level, - const char *fmt, ...); - NJS_EXPORT void njs_value_undefined_set(njs_value_t *value); NJS_EXPORT void njs_value_null_set(njs_value_t *value); NJS_EXPORT void njs_value_invalid_set(njs_value_t *value); diff -r ad1a7ad3c715 -r fc1001f6801b src/njs_builtin.c --- a/src/njs_builtin.c Wed Dec 13 18:38:47 2023 -0800 +++ b/src/njs_builtin.c Thu Dec 14 22:32:02 2023 -0800 @@ -1532,7 +1532,7 @@ njs_env_hash_init(njs_vm_t *vm, njs_lvlh uint32_t cp; njs_int_t ret; const u_char *val, *entry, *s, *end; - njs_object_prop_t *prop, *prev; + njs_object_prop_t *prop; njs_string_prop_t string; njs_lvlhsh_query_t lhq; @@ -1599,13 +1599,6 @@ njs_env_hash_init(njs_vm_t *vm, njs_lvlh * Always using the first element among the duplicates * and ignoring the rest. */ - - prev = lhq.value; - - if (!njs_values_same(njs_prop_value(prop), njs_prop_value(prev))) { - njs_vm_warn(vm, "environment variable \"%V\" has more than one" - " value\n", &lhq.key); - } } } diff -r ad1a7ad3c715 -r fc1001f6801b src/njs_vm.c --- a/src/njs_vm.c Wed Dec 13 18:38:47 2023 -0800 +++ b/src/njs_vm.c Thu Dec 14 22:32:02 2023 -0800 @@ -24,7 +24,6 @@ njs_vm_opt_init(njs_vm_opt_t *options) { njs_memzero(options, sizeof(njs_vm_opt_t)); - options->log_level = NJS_LOG_LEVEL_INFO; options->max_stack_size = NJS_MAX_STACK_SIZE; } @@ -1093,30 +1092,6 @@ njs_vm_memory_error(njs_vm_t *vm) } -njs_noinline void -njs_vm_logger(njs_vm_t *vm, njs_log_level_t level, const char *fmt, ...) -{ - u_char *p; - va_list args; - njs_logger_t logger; - u_char buf[32768]; - - if (vm->options.ops == NULL) { - return; - } - - logger = vm->options.ops->logger; - - if (logger != NULL && vm->options.log_level >= level) { - va_start(args, fmt); - p = njs_vsprintf(buf, buf + sizeof(buf), fmt, args); - va_end(args); - - logger(vm, vm->external, level, buf, p - buf); - } -} - - njs_int_t njs_vm_value_string(njs_vm_t *vm, njs_str_t *dst, njs_value_t *src) { _______________________________________________ nginx-devel mailing list nginx-devel@nginx.org https://mailman.nginx.org/mailman/listinfo/nginx-devel