Browse Source

devex: add command to start/stop recording logs to a file

KernelDeimos 11 tháng trước cách đây
mục cha
commit
d14a30e089
1 tập tin đã thay đổi với 32 bổ sung1 xóa
  1. 32 1
      packages/backend/src/services/runtime-analysis/LogService.js

+ 32 - 1
packages/backend/src/services/runtime-analysis/LogService.js

@@ -161,6 +161,7 @@ class DevLogger {
     constructor (log, opt_delegate) {
         this.log = log;
         this.off = false;
+        this.recto = null;
 
         if ( opt_delegate ) {
             this.delegate = opt_delegate;
@@ -179,11 +180,19 @@ class DevLogger {
         const prefix = globalThis.dev_console_indent_on
             ? Array(ld ?? 0).fill('    ').join('')
             : '';
-        this.log(stringify_log_entry({
+        this.log_(stringify_log_entry({
             prefix,
             log_lvl, crumbs, message, fields, objects,
         }));
     }
+    
+    log_ (text) {
+        if ( this.recto ) {
+            const fs = require('node:fs');
+            fs.appendFileSync(this.recto, text + '\n');
+        }
+        this.log(text);
+    }
 }
 
 class NullLogger {
@@ -291,6 +300,28 @@ class LogService extends BaseService {
                     this.devlogger && (this.devlogger.off = ! this.devlogger.off);
                 }
             },
+            {
+                id: 'rec',
+                description: 'start recording to a file via dev logger',
+                handler: async (args, ctx) => {
+                    const [name] = args;
+                    const {log} = ctx;
+                    if ( ! this.devlogger ) {
+                        log('no dev logger; what are you doing?');
+                    }
+                    this.devlogger.recto = name;
+                }
+            },
+            {
+                id: 'stop',
+                description: 'stop recording to a file via dev logger',
+                handler: async ([name], log) => {
+                    if ( ! this.devlogger ) {
+                        log('no dev logger; what are you doing?');
+                    }
+                    this.devlogger.recto = null;
+                }
+            },
             {
                 id: 'indent',
                 description: 'toggle log indentation',