[njs] Making ngx_js_logger() reusable by QuickJS code.

noreply at nginx.com noreply at nginx.com
Fri Aug 16 15:27:02 UTC 2024


details:   https://github.com/nginx/njs/commit/d47f7beda262a63d0eef72839c5b2b545b0699d9
branches:  master
commit:    d47f7beda262a63d0eef72839c5b2b545b0699d9
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Mon, 8 Jul 2024 23:38:28 -0700
description:
Making ngx_js_logger() reusable by QuickJS code.


---
 nginx/ngx_js.c | 39 ++++++++++++++++++++++++---------------
 nginx/ngx_js.h |  2 +-
 2 files changed, 25 insertions(+), 16 deletions(-)

diff --git a/nginx/ngx_js.c b/nginx/ngx_js.c
index 9a20a684..4c1f29f3 100644
--- a/nginx/ngx_js.c
+++ b/nginx/ngx_js.c
@@ -815,10 +815,11 @@ 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, level;
+    char              *p;
+    ngx_int_t          lvl;
+    njs_str_t          msg;
+    njs_uint_t         n, level;
+    ngx_connection_t  *c;
 
     p = njs_vm_external(vm, NJS_PROTO_ID_ANY, njs_argument(args, 0));
     if (p == NULL) {
@@ -840,6 +841,8 @@ ngx_js_ext_log(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
         n = 1;
     }
 
+    c = ngx_external_connection(vm, p);
+
     for (; n < nargs; n++) {
         if (njs_vm_value_dump(vm, &msg, njs_argument(args, n), 1,
                               !!(magic & NGX_JS_LOG_DUMP))
@@ -848,7 +851,7 @@ ngx_js_ext_log(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
             return NJS_ERROR;
         }
 
-        ngx_js_logger(vm, p, level, msg.start, msg.length);
+        ngx_js_logger(c, level, msg.start, msg.length);
     }
 
     njs_value_undefined_set(retval);
@@ -1185,30 +1188,36 @@ void
 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];
+    u_char            *p;
+    va_list            args;
+    ngx_connection_t  *c;
+    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);
+    if (external != NULL) {
+        c = ngx_external_connection(vm, external);
+
+    } else {
+        c = NULL;
+    }
+
+    ngx_js_logger(c, 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_js_logger(ngx_connection_t *c, ngx_uint_t level, const u_char *start,
+    size_t length)
 {
     ngx_log_t           *log;
-    ngx_connection_t    *c;
     ngx_log_handler_pt   handler;
 
     handler = NULL;
 
-    if (external != NULL) {
-        c = ngx_external_connection(vm, external);
+    if (c != NULL) {
         log =  c->log;
         handler = log->handler;
         log->handler = NULL;
@@ -1222,7 +1231,7 @@ ngx_js_logger(njs_vm_t *vm, njs_external_ptr_t external, ngx_uint_t level,
 
     ngx_log_error(level, log, 0, "js: %*s", length, start);
 
-    if (external != NULL) {
+    if (c != NULL) {
         log->handler = handler;
     }
 }
diff --git a/nginx/ngx_js.h b/nginx/ngx_js.h
index beccbfdf..aad88b54 100644
--- a/nginx/ngx_js.h
+++ b/nginx/ngx_js.h
@@ -192,7 +192,7 @@ 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_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,
+void ngx_js_logger(ngx_connection_t *c, 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);


More information about the nginx-devel mailing list