[njs] Shell: introduced error, info and warn methods for console object.

Dmitry Volyntsev xeioex at nginx.com
Fri Sep 22 19:59:41 UTC 2023


details:   https://hg.nginx.org/njs/rev/49f6f5c81a11
branches:  
changeset: 2204:49f6f5c81a11
user:      Dmitry Volyntsev <xeioex at nginx.com>
date:      Wed Sep 20 21:48:41 2023 -0700
description:
Shell: introduced error, info and warn methods for console object.

diffstat:

 external/njs_shell.c |  74 ++++++++++++++++++++++++++++++++++++++++-----------
 test/shell_test.exp  |  22 +++++++++-----
 2 files changed, 72 insertions(+), 24 deletions(-)

diffs (196 lines):

diff -r 6a94953b4053 -r 49f6f5c81a11 external/njs_shell.c
--- a/external/njs_shell.c	Wed Sep 20 19:19:58 2023 -0700
+++ b/external/njs_shell.c	Wed Sep 20 21:48:41 2023 -0700
@@ -122,7 +122,7 @@ static char *njs_completion_generator(co
 #endif
 
 static njs_int_t njs_ext_console_log(njs_vm_t *vm, njs_value_t *args,
-    njs_uint_t nargs, njs_index_t indent, njs_value_t *retval);
+    njs_uint_t nargs, njs_index_t magic, njs_value_t *retval);
 static njs_int_t njs_ext_console_time(njs_vm_t *vm, njs_value_t *args,
     njs_uint_t nargs, njs_index_t unused, njs_value_t *retval);
 static njs_int_t njs_ext_console_time_end(njs_vm_t *vm, njs_value_t *args,
@@ -156,7 +156,33 @@ static njs_external_t  njs_ext_console[]
         .enumerable = 1,
         .u.method = {
             .native = njs_ext_console_log,
-            .magic8 = 1,
+#define NJS_LOG_DUMP  16
+#define NJS_LOG_MASK  15
+            .magic8 = NJS_LOG_LEVEL_INFO | NJS_LOG_DUMP,
+        }
+    },
+
+    {
+        .flags = NJS_EXTERN_METHOD,
+        .name.string = njs_str("error"),
+        .writable = 1,
+        .configurable = 1,
+        .enumerable = 1,
+        .u.method = {
+            .native = njs_ext_console_log,
+            .magic8 = NJS_LOG_LEVEL_ERROR,
+        }
+    },
+
+    {
+        .flags = NJS_EXTERN_METHOD,
+        .name.string = njs_str("info"),
+        .writable = 1,
+        .configurable = 1,
+        .enumerable = 1,
+        .u.method = {
+            .native = njs_ext_console_log,
+            .magic8 = NJS_LOG_LEVEL_INFO,
         }
     },
 
@@ -168,6 +194,7 @@ static njs_external_t  njs_ext_console[]
         .enumerable = 1,
         .u.method = {
             .native = njs_ext_console_log,
+            .magic8 = NJS_LOG_LEVEL_INFO,
         }
     },
 
@@ -201,6 +228,18 @@ static njs_external_t  njs_ext_console[]
         }
     },
 
+    {
+        .flags = NJS_EXTERN_METHOD,
+        .name.string = njs_str("warn"),
+        .writable = 1,
+        .configurable = 1,
+        .enumerable = 1,
+        .u.method = {
+            .native = njs_ext_console_log,
+            .magic8 = NJS_LOG_LEVEL_WARN,
+        }
+    },
+
 };
 
 
@@ -1337,30 +1376,28 @@ next:
 
 static njs_int_t
 njs_ext_console_log(njs_vm_t *vm, njs_value_t *args, njs_uint_t nargs,
-    njs_index_t indent, njs_value_t *retval)
+    njs_index_t magic, njs_value_t *retval)
 {
-    njs_str_t   msg;
-    njs_uint_t  n;
+    njs_str_t        msg;
+    njs_uint_t       n;
+    njs_log_level_t  level;
 
     n = 1;
+    level = (njs_log_level_t) magic & NJS_LOG_MASK;
 
     while (n < nargs) {
-        if (njs_vm_value_dump(vm, &msg, njs_argument(args, n), 1, indent)
+        if (njs_vm_value_dump(vm, &msg, njs_argument(args, n), 1,
+                              !!(magic & NJS_LOG_DUMP))
             == NJS_ERROR)
         {
             return NJS_ERROR;
         }
 
-        njs_vm_log(vm, "%s", (n != 1) ? " " : "");
-        njs_vm_log(vm, "%*s", msg.length, msg.start);
+        njs_vm_logger(vm, level, "%*s\n", msg.length, msg.start);
 
         n++;
     }
 
-    if (nargs > 1) {
-        njs_vm_log(vm, "\n");
-    }
-
     njs_value_undefined_set(retval);
 
     return NJS_OK;
@@ -1604,11 +1641,16 @@ 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)
 {
-    if (level == NJS_LOG_LEVEL_ERROR) {
-        njs_stderror("%*s", length, start);
-
-    } else {
+    switch (level) {
+    case NJS_LOG_LEVEL_INFO:
         njs_printf("%*s", length, start);
+        break;
+    case NJS_LOG_LEVEL_WARN:
+        njs_printf("W: %*s", length, start);
+        break;
+    case NJS_LOG_LEVEL_ERROR:
+        njs_printf("E: %*s", length, start);
+        break;
     }
 }
 
diff -r 6a94953b4053 -r 49f6f5c81a11 test/shell_test.exp
--- a/test/shell_test.exp	Wed Sep 20 19:19:58 2023 -0700
+++ b/test/shell_test.exp	Wed Sep 20 21:48:41 2023 -0700
@@ -252,9 +252,9 @@ njs_test {
     {"console.log(1)\r\n"
      "console.log(1)\r\n1\r\nundefined\r\n>> "}
     {"console.log(1, 'a')\r\n"
-     "console.log(1, 'a')\r\n1 a\r\nundefined\r\n>> "}
+     "console.log(1, 'a')\r\n1\r\na\r\nundefined\r\n>> "}
     {"print(1, 'a')\r\n"
-     "print(1, 'a')\r\n1 a\r\nundefined\r\n>> "}
+     "print(1, 'a')\r\n1\r\na\r\nundefined\r\n>> "}
     {"console.log('\\tабв\\nгд')\r\n"
      "console.log('\\\\tабв\\\\nгд')\r\n\tабв\r\nгд\r\nundefined\r\n>> "}
     {"console.dump()\r\n"
@@ -262,7 +262,13 @@ njs_test {
     {"console.dump(1)\r\n"
      "console.dump(1)\r\n1\r\nundefined\r\n>> "}
     {"console.dump(1, 'a')\r\n"
-     "console.dump(1, 'a')\r\n1 a\r\nundefined\r\n>> "}
+     "console.dump(1, 'a')\r\n1\r\na\r\nundefined\r\n>> "}
+    {"console.error(42)\r\n"
+     "console.error(42)\r\nE: 42\r\nundefined\r\n>> "}
+    {"console.info(23)\r\n"
+     "console.info(23)\r\n23\r\nundefined\r\n>> "}
+    {"console.warn(37)\r\n"
+     "console.warn(37)\r\nW: 37\r\nundefined\r\n>> "}
 }
 
 # console.time* functions
@@ -307,9 +313,9 @@ njs_test {
 
 njs_test {
     {"var print = console.log.bind(console); print(1, 'a', [1, 2])\r\n"
-     "1 a \\\[1,2]\r\nundefined\r\n>> "}
+     "1\r\na\r\n\\\[1,2]\r\nundefined\r\n>> "}
     {"var print = console.dump.bind(console); print(1, 'a', [1, 2])\r\n"
-     "1 a \\\[\r\n 1,\r\n 2\r\n]\r\nundefined\r\n>> "}
+     "1\r\na\r\n\\\[\r\n 1,\r\n 2\r\n]\r\nundefined\r\n>> "}
     {"var print = console.log.bind(console); print(console.a.a)\r\n"
      "TypeError: cannot get property \"a\" of undefined"}
     {"print(console.a.a)\r\n"
@@ -318,8 +324,8 @@ njs_test {
 
 # Backtraces for external objects
 njs_test {
-    {"console.log(console.a.a)\r\n"
-     "console.log(console.a.a)\r\nThrown:\r\nTypeError:*at console.log (native)"}
+    {"console.info(console.a.a)\r\n"
+     "console.info(console.a.a)\r\nThrown:\r\nTypeError:*at console.info (native)"}
 }
 
 # dumper
@@ -400,7 +406,7 @@ njs_test {
 
 njs_test {
     {"var t = setImmediate(console.log, 'a', 'aa')\r\n"
-     "undefined\r\na aa"}
+     "undefined\r\na\r\naa"}
 }
 
 njs_test {


More information about the nginx-devel mailing list