ソースを参照

Yet another attempt at smoking out the segfault in AdminLog, also fixed a bug with AdminLog_unsubscribe() taking the wrong entry and improved cjdnslog to unsubscribe on ctrl+c

Caleb James DeLisle 9 年 前
コミット
c69d982d55
2 ファイル変更29 行追加19 行削除
  1. 16 19
      admin/AdminLog.c
  2. 13 0
      tools/cjdnslog

+ 16 - 19
admin/AdminLog.c

@@ -120,8 +120,7 @@ static Dict* makeLogMessage(struct Subscription* subscription,
                             enum Log_Level logLevel,
                             const char* file,
                             uint32_t line,
-                            const char* format,
-                            va_list vaArgs,
+                            String* message,
                             struct Allocator* alloc)
 {
     int64_t now = (int64_t) Time_currentTimeSeconds(logger->base);
@@ -133,12 +132,6 @@ static Dict* makeLogMessage(struct Subscription* subscription,
     Dict_putString(out, LEVEL, String_new(Log_nameForLevel(logLevel), alloc), alloc);
     Dict_putString(out, STR_FILE, String_new(file, alloc), alloc);
     Dict_putInt(out, LINE, line, alloc);
-    String* message = String_vprintf(alloc, format, vaArgs);
-
-    // Strip all of the annoying \n marks in the log entries.
-    if (message->len > 0 && message->bytes[message->len - 1] == '\n') {
-        message->len--;
-    }
     Dict_putString(out, MESSAGE, message, alloc);
 
     return out;
@@ -188,7 +181,7 @@ static void doLog(struct Log* genericLog,
                   va_list args)
 {
     struct AdminLog* log = Identity_check((struct AdminLog*) genericLog);
-    Dict* message = NULL;
+    String* message = NULL;
     struct Allocator* logLineAlloc = NULL;
 
     if (log->logging) { return; }
@@ -202,16 +195,20 @@ static void doLog(struct Log* genericLog,
         }
         if (!message) {
             logLineAlloc = Allocator_child(log->alloc);
-            message = makeLogMessage(&log->subscriptions[i],
-                                     log,
-                                     logLevel,
-                                     fullFilePath,
-                                     line,
-                                     format,
-                                     args,
-                                     logLineAlloc);
+            message = String_vprintf(logLineAlloc, format, args);
+            // Strip all of the annoying \n marks in the log entries.
+            if (message->len > 0 && message->bytes[message->len - 1] == '\n') {
+                message->len--;
+            }
         }
-        int ret = Admin_sendMessage(message, log->subscriptions[i].txid, log->admin);
+        Dict* d = makeLogMessage(&log->subscriptions[i],
+                                 log,
+                                 logLevel,
+                                 fullFilePath,
+                                 line,
+                                 message,
+                                 logLineAlloc);
+        int ret = Admin_sendMessage(d, log->subscriptions[i].txid, log->admin);
         if (ret == Admin_sendMessage_CHANNEL_CLOSED) {
             removeSubscription(log, &log->subscriptions[i]);
         } else if (ret) {
@@ -283,7 +280,7 @@ static void unsubscribe(Dict* args, void* vcontext, String* txid, struct Allocat
     } else {
         error = "No such subscription.";
         for (int i = 0; i < (int)log->subscriptionCount; i++) {
-            if (!String_equals(streamIdHex, log->subscriptions[i].streamId)) {
+            if (String_equals(streamIdHex, log->subscriptions[i].streamId)) {
                 removeSubscription(log, &log->subscriptions[i]);
                 error = "none";
                 break;

+ 13 - 0
tools/cjdnslog

@@ -52,6 +52,7 @@ Cjdns.connectWithAdminInfo(function (cjdns) {
     });
     cjdns.AdminLog_subscribe(line, verbosity, file, function (err, ret) {
         if (err) { throw err; }
+        if (ret.error !== 'none') { throw new Error(ret.error); }
 
         // make sure cjdns doesn't think we've gone missing!
         setInterval(function () {
@@ -59,5 +60,17 @@ Cjdns.connectWithAdminInfo(function (cjdns) {
                 if (err) { throw err; }
             });
         }, 10000);
+
+        var sigint = false;
+        process.on('SIGINT', function () {
+            if (sigint) { process.exit(100); }
+            console.log('Disconnecting...');
+            cjdns.AdminLog_unsubscribe(ret.streamId, function (err, ret) {
+                if (err) { throw err; }
+                console.log('done');
+                cjdns.disconnect();
+                process.exit(0);
+            });
+        });
     });
 });