Bug 997372 - Add actor type logging to protocol.js. r=fitzgen
authorDave Camp <dcamp@mozilla.com>
Mon, 28 Apr 2014 08:58:26 -0700
changeset 180524 f92e1b127fe802892ca12c4d9eb41fab3b6da3a6
parent 180523 8a6ad7b3b23c9bd0c85b5cce9f929af8ff19607d
child 180525 a2d961fb47899c1c9d64313e5f8ec9969cecb027
push id6540
push userdcamp@campd.org
push dateMon, 28 Apr 2014 15:58:42 +0000
treeherderfx-team@f92e1b127fe8 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfitzgen
bugs997372
milestone31.0a1
Bug 997372 - Add actor type logging to protocol.js. r=fitzgen
toolkit/devtools/server/protocol.js
--- a/toolkit/devtools/server/protocol.js
+++ b/toolkit/devtools/server/protocol.js
@@ -6,16 +6,46 @@
 
 let {Cu} = require("chrome");
 let Services = require("Services");
 let promise = require("devtools/toolkit/deprecated-sync-thenables");
 let {Class} = require("sdk/core/heritage");
 let {EventTarget} = require("sdk/event/target");
 let events = require("sdk/event/core");
 let object = require("sdk/util/object");
+let {PrefsTarget} = require("sdk/preferences/event-target");
+
+
+let gActorLogTypes;
+let gFrontLogTypes;
+
+function updateLogging()
+{
+  let prefService = require("sdk/preferences/service");
+  let typeNames = prefService.get("devtools.log-actors", "").split(",");
+  gActorLogTypes = new Set();
+  for (let name of typeNames) {
+    gActorLogTypes.add(name);
+  }
+
+  typeNames = prefService.get("devtools.log-fronts", "").split(",");
+  gFrontLogTypes = new Set();
+  for (let name of typeNames) {
+    gFrontLogTypes.add(name);
+  }
+};
+updateLogging();
+
+let logTarget = PrefsTarget({ branchName: "devtools." });
+logTarget.on("log-actors", name => {
+  updateLogging();
+});
+logTarget.on("log-fronts", name => {
+  updateLogging();
+});
 
 // Waiting for promise.done() to be added, see bug 851321
 function promiseDone(err) {
   console.error(err);
   return promise.reject(err);
 }
 
 /**
@@ -819,25 +849,32 @@ let Actor = Class({
         let sendEvent = this._sendEvent.bind(this, name)
         this.on(name, (...args) => {
           sendEvent.apply(null, args);
         });
       }
     }
   },
 
+  _sendPacket: function(packet) {
+    if (gActorLogTypes.has(this.typeName)) {
+      console.log("ACTOR SEND:" + this.actorID + ":" + JSON.stringify(packet, null, 2));
+    }
+    this.conn.send(packet);
+  },
+
   _sendEvent: function(name, ...args) {
     if (!this._actorSpec.events.has(name)) {
       // It's ok to emit events that don't go over the wire.
       return;
     }
     let request = this._actorSpec.events.get(name);
     let packet = request.write(args, this);
     packet.from = packet.from || this.actorID;
-    this.conn.send(packet);
+    this._sendPacket(packet);
   },
 
   destroy: function() {
     Pool.prototype.destroy.call(this);
     this.actorID = null;
   },
 
   /**
@@ -846,21 +883,21 @@ let Actor = Class({
    *   Optional string to customize the form.
    * @returns A jsonable object.
    */
   form: function(hint) {
     return { actor: this.actorID }
   },
 
   writeError: function(err) {
-    console.error(err);
+    DevToolsUtils.reportException(err);
     if (err.stack) {
       dump(err.stack);
     }
-    this.conn.send({
+    this._sendPacket({
       from: this.actorID,
       error: "unknownError",
       message: err.toString()
     });
   },
 
   _queueResponse: function(create) {
     let pending = this._pendingResponse || promise.resolve(null);
@@ -932,16 +969,19 @@ let actorProto = function(actorProto) {
       protoSpec.events.set(name, Request(object.merge({type: name}, eventRequest)));
     }
   }
 
   // Generate request handlers for each method definition
   actorProto.requestTypes = Object.create(null);
   protoSpec.methods.forEach(spec => {
     let handler = function(packet, conn) {
+      if (gActorLogTypes.has(this.typeName)) {
+        console.log("ACTOR RECV:" + this.actorID + ":" + JSON.stringify(packet, null, 2));
+      }
       try {
         let args = spec.request.read(packet, this);
 
         let ret = this[spec.name].apply(this, args);
 
         if (spec.oneway) {
           // No need to send a response.
           return;
@@ -955,17 +995,17 @@ let actorProto = function(actorProto) {
             try {
               this.destroy();
             } catch(e) {
               this.writeError(e);
               return;
             }
           }
 
-          conn.send(response);
+          this._sendPacket(response);
         };
 
         this._queueResponse(p => {
           return p
             .then(() => ret)
             .then(sendReturn)
             .then(null, this.writeError.bind(this));
         })
@@ -1052,26 +1092,33 @@ let Front = Class({
   toString: function() { return "[Front for " + this.typeName + "/" + this.actorID + "]" },
 
   /**
    * Update the actor from its representation.
    * Subclasses should override this.
    */
   form: function(form) {},
 
+  _sendPacket: function(packet) {
+    if (gFrontLogTypes.has(this.typeName)) {
+      console.log("FRONT SEND:" + this.actorID + ":" + JSON.stringify(packet, null, 2));
+    }
+    this.conn._transport.send(packet);
+  },
+
   /**
    * Send a packet on the connection.
    */
   send: function(packet) {
     if (packet.to) {
-      this.conn._transport.send(packet);
+      this._sendPacket(packet);
     } else {
       this.actor().then(actorID => {
         packet.to = actorID;
-        this.conn._transport.send(packet);
+        this._sendPacket(packet);
       });
     }
   },
 
   /**
    * Send a two-way request on the connection.
    */
   request: function(packet) {
@@ -1080,16 +1127,20 @@ let Front = Class({
     this.send(packet);
     return deferred.promise;
   },
 
   /**
    * Handler for incoming packets from the client's actor.
    */
   onPacket: function(packet) {
+    if (gFrontLogTypes.has(this.typeName)) {
+      console.log("FRONT RECV:" + this.actorID + ":" + JSON.stringify(packet, null, 2));
+    }
+
     // Pick off event packets
     if (this._clientSpec.events && this._clientSpec.events.has(packet.type)) {
       let event = this._clientSpec.events.get(packet.type);
       let args = event.request.read(packet, this);
       if (event.pre) {
         event.pre.forEach((pre) => pre.apply(this, args));
       }
       events.emit.apply(null, [this, event.name].concat(args));