Skip to content
Browse files

Enables bidirectional console logging

  • Loading branch information...
1 parent b992355 commit 8bb27159022d709e3c1588ba1b6eef5e059eee94 @c4milo committed May 3, 2012
Showing with 48 additions and 9 deletions.
  1. +37 −6 lib/console.js
  2. +1 −1 lib/index.js
  3. +10 −2 webkit-devtools-agent.js
View
43 lib/console.js
@@ -1,16 +1,47 @@
-function ConsoleAgent() {
- this._enable = false;
+var util = require('util');
+
+function ConsoleAgent(sendEvent) {
+ var self = this;
+ this.sendEvent = sendEvent;
+ this.enabled = false;
this.messages = [];
- this.console = console;
+
+ ['log', 'warn', 'info', 'error'].forEach(function(level) {
+ var ref = console[level];
+ console[level] = function() {
+ ref.apply(this, arguments);
+
+ var message = {
+ method: 'Console.messageAdded',
+ params: {
+ message: {
+ text: util.format.apply(this, arguments),
+ level: level,
+ source: 'nodejs'
+ }
+ }
+ };
+
+ if (!self.enabled) {
+ self.messages.push(message);
+ return;
+ }
+
+ sendEvent(message);
+ };
+ });
}
(function() {
this.enable = function(params, sendResult) {
- sendResult({result: this._enable});
+ for(var i = 0, len = this.messages.length; i < len; i++) {
+ this.sendEvent(this.messages[i]);
+ }
+ sendResult({result: this.enabled});
};
this.disable = function(params, sendResult) {
- this._enable = false;
+ this.enabled = false;
sendResult({});
};
@@ -29,5 +60,5 @@ function ConsoleAgent() {
}).call(ConsoleAgent.prototype);
-module.exports = new ConsoleAgent();
+module.exports = ConsoleAgent;
View
2 lib/index.js
@@ -1,9 +1,9 @@
module.exports = {
+ Console : require('./console'),
Profiler : require('./profiler'),
Page : require('./page'),
//FileSystem : require('./filesystem'),
Debugger : require('./debugger'),
- Console : require('./console'),
Network : require('./network'),
Runtime : require('./runtime'),
Timeline : require('./timeline'),
View
12 webkit-devtools-agent.js
@@ -11,6 +11,16 @@ function start() {
console.log('webkit-devtools-agent started on %s:%s', host, port);
wss.on('connection', function(socket) {
+ function sendEvent(event) {
+ socket.send(JSON.stringify(event));
+ }
+
+ for (var agent in agents) {
+ if (typeof agents[agent] == 'function') {
+ agents[agent] = new agents[agent](sendEvent);
+ }
+ }
+
socket.on('message', function(message) {
try {
message = JSON.parse(message);
@@ -34,8 +44,6 @@ function start() {
domain[method](params, function(result) {
socket.send(JSON.stringify({ id: id, result: result }));
- }, function(event) {
- socket.send(JSON.stringify(event));
});
});
});

0 comments on commit 8bb2715

Please sign in to comment.
Something went wrong with that request. Please try again.