Skip to content

Commit

Permalink
Use debug package if present
Browse files Browse the repository at this point in the history
  • Loading branch information
n-riesco committed Feb 5, 2016
1 parent bc435ed commit 7708404
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 28 deletions.
25 changes: 20 additions & 5 deletions bin/ijavascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,21 @@
*/
var DEBUG = false;

var log;
if (DEBUG) {
var console = require("console");
log = function log() {
process.stderr.write("IJS: ");
console.error.apply(this, arguments);
};
} else {
try {
log = require("debug")("IJS:");
} catch (err) {
log = function noop() {};
}
}

var console = require("console");
var exec = require("child_process").exec;
var fs = require("fs");
Expand Down Expand Up @@ -119,9 +134,9 @@ parseCommandArgs(context);
setFrontendInfoAsync(context, function() {
setProtocol(context);

if (DEBUG) console.log("CONTEXT:", context);

installKernelAsync(context, function() {
log("CONTEXT:", context);

if (!context.flag.install) {
spawnFrontend(context);
}
Expand Down Expand Up @@ -225,7 +240,7 @@ function setFrontendInfoAsync(context, callback) {
console.error("Error running `ipython --version`");
console.error(error.toString());
if (stderr) console.error(stderr.toString());
if (DEBUG) console.log("CONTEXT:", context);
log("CONTEXT:", context);
process.exit(1);
}

Expand All @@ -238,7 +253,7 @@ function setFrontendInfoAsync(context, callback) {
"Error parsing IPython version:",
context.version.frontend
);
if (DEBUG) console.log("CONTEXT:", context);
log("CONTEXT:", context);
process.exit(1);
}

Expand Down Expand Up @@ -330,7 +345,7 @@ function installKernelAsync(context, callback) {
console.error(util.format("Error running `%s`", cmd));
console.error(error.toString());
if (stderr) console.error(stderr.toString());
if (DEBUG) console.log("CONTEXT:", context);
log("CONTEXT:", context);
process.exit(1);
}

Expand Down
15 changes: 14 additions & 1 deletion lib/handlers_v4.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,20 @@ module.exports = {

var DEBUG = global.DEBUG || false;

var console = require("console");
var log;
if (DEBUG) {
var console = require("console");
log = function log() {
process.stderr.write("KERNEL: ");
console.error.apply(this, arguments);
};
} else {
try {
log = require("debug")("KERNEL:");
} catch (err) {
log = function noop() {};
}
}

/**
* IPython protocol version
Expand Down
15 changes: 14 additions & 1 deletion lib/handlers_v5.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,20 @@ module.exports = {

var DEBUG = global.DEBUG || false;

var console = require("console");
var log;
if (DEBUG) {
var console = require("console");
log = function log() {
process.stderr.write("KERNEL: ");
console.error.apply(this, arguments);
};
} else {
try {
log = require("debug")("KERNEL:");
} catch (err) {
log = function noop() {};
}
}

/**
* IPython protocol version
Expand Down
50 changes: 29 additions & 21 deletions lib/kernel.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,21 @@

var DEBUG = false;

var log;
if (DEBUG) {
var console = require("console");
log = function log() {
process.stderr.write("KERNEL: ");
console.error.apply(this, arguments);
};
} else {
try {
log = require("debug")("KERNEL:");
} catch (err) {
log = function noop() {};
}
}

var console = require("console");
var fs = require("fs");
var path = require("path");
Expand Down Expand Up @@ -95,7 +110,7 @@ try {
config.connection = JSON.parse(config.connection);

} catch (e) {
console.error("ARGV:", process.argv);
console.error("KERNEL: ARGV:", process.argv);
console.error(usage);
throw e;
}
Expand Down Expand Up @@ -239,9 +254,7 @@ Kernel.prototype._bindSockets = function() {

function onShellMessage(msg) {
if (!msg.signatureOK) {
console.error(
"KERNEL: Bad message signature: %s", util.inspect(msg)
);
log("Bad message signature: %s", util.inspect(msg));
return;
}

Expand Down Expand Up @@ -272,9 +285,7 @@ Kernel.prototype._bindSockets = function() {

function onControlMessage(msg) {
if (!msg.signatureOK) {
console.error(
"KERNEL: Bad message signature: %s", util.inspect(msg)
);
log("Bad message signature: %s", util.inspect(msg));
return;
}

Expand Down Expand Up @@ -311,7 +322,7 @@ Kernel.prototype._relayStandardStreams = function() {
this.session.stderr.on("data", onStderr.bind(this));

function onStdoutV4(data) {
if (DEBUG) console.log("KERNEL: STDOUT: ", data.toString());
log("STDOUT: ", data.toString());

if (this.lastExecuteRequest) {
this.lastExecuteRequest.respond(
Expand All @@ -325,7 +336,7 @@ Kernel.prototype._relayStandardStreams = function() {
}

function onStderrV4(data) {
if (DEBUG) console.log("KERNEL: STDERR: ", data.toString());
log("STDERR: ", data.toString());

if (this.lastExecuteRequest) {
this.lastExecuteRequest.respond(
Expand All @@ -339,7 +350,7 @@ Kernel.prototype._relayStandardStreams = function() {
}

function onStdoutV5(data) {
if (DEBUG) console.log("KERNEL: STDOUT: ", data.toString());
log("STDOUT: ", data.toString());

if (this.lastExecuteRequest) {
this.lastExecuteRequest.respond(
Expand All @@ -353,7 +364,7 @@ Kernel.prototype._relayStandardStreams = function() {
}

function onStderrV5(data) {
if (DEBUG) console.log("KERNEL: STDERR: ", data.toString());
log("STDERR: ", data.toString());

if (this.lastExecuteRequest) {
this.lastExecuteRequest.respond(
Expand Down Expand Up @@ -396,26 +407,23 @@ Kernel.prototype._runStartupScripts = function() {
startupScripts = [];
}

if (DEBUG) console.log("KERNEL: STARTUP: " + startupScripts);
log("STARTUP: " + startupScripts);

startupScripts.forEach((function(script) {
var code;

try {
code = fs.readFileSync(script).toString();
} catch (e) {
var msg = "KERNEL: STARTUP: Cannot read '" + script + "'";
if (DEBUG) console.log(msg);
log("STARTUP: Cannot read '" + script + "'");
return;
}

this.session.execute(code, function onSuccess() {
var msg = "KERNEL: STARTUP: '" + script + "' run successfuly";
if (DEBUG) console.log(msg);
log("STARTUP: '" + script + "' run successfuly");
},
function onError() {
var msg = "KERNEL: STARTUP: '" + script + "' failed to run";
if (DEBUG) console.log(msg);
log("STARTUP: '" + script + "' failed to run");
}
);
}).bind(this));
Expand All @@ -428,7 +436,7 @@ Kernel.prototype._runStartupScripts = function() {
* killed and before closing the sockets
*/
Kernel.prototype.destroy = function(destroyCB) {
if (DEBUG) console.log("KERNEL: Being destroyed");
log("Destroying kernel");

// TODO(NR) Handle socket `this.stdin` once it is implemented
this.controlSocket.removeAllListeners();
Expand Down Expand Up @@ -464,7 +472,7 @@ Kernel.prototype.destroy = function(destroyCB) {
* restarted
*/
Kernel.prototype.restart = function(restartCB) {
if (DEBUG) console.log("KERNEL: Being restarted");
log("Restarting kernel");

this.session.restart("SIGTERM", (function() {
this._initSession();
Expand All @@ -487,7 +495,7 @@ var kernel = new Kernel(config);

// Interpret a SIGINT signal as a request to interrupt the kernel
process.on("SIGINT", function() {
if (DEBUG) console.log("KERNEL: Interrupting kernel");
log("Interrupting kernel");

// TODO(NR) Implement kernel interruption
kernel.restart(function() {
Expand Down

0 comments on commit 7708404

Please sign in to comment.