diff --git a/plugins/c9.vfs.client/log-service.js b/plugins/c9.vfs.client/log-service.js new file mode 100644 index 00000000..0b503eff --- /dev/null +++ b/plugins/c9.vfs.client/log-service.js @@ -0,0 +1,10 @@ + +module.exports = function(vfs, options, register) { + register(null, { + log: function(message, callback) { + console.log("VFSLOG: " + message); + console.error("VFSERROR: " + message); + callback(); + } + }) +} \ No newline at end of file diff --git a/plugins/c9.vfs.client/vfs.log.js b/plugins/c9.vfs.client/vfs.log.js new file mode 100644 index 00000000..e515a690 --- /dev/null +++ b/plugins/c9.vfs.client/vfs.log.js @@ -0,0 +1,77 @@ +/** Sends client side logs to vfs via the websocket connection **/ + +define(function (require, exports, module) { + "use strict"; + + main.consumes = ["Plugin", "ext", "c9"]; + main.provides = ["vfs.log"]; + return main; + + function main(options, imports, register) { + var Plugin = imports.Plugin; + var c9 = imports.c9; + var ext = imports.ext; + var plugin = new Plugin("Ajax.org", main.consumes); + + var loaded = false; + var server = null; + + function load() { + if (loaded) return false; + loaded = true; + + ext.loadRemotePlugin("log", { + code: require("text!./log-service.js"), + redefine: true + }, function(err, remote) { + if (err) return console.error(err); + + server = remote; + }); + + c9.on("stateChange", function(e) { + if (e.state & c9.NETWORK) { + load(); + } + else { + loaded = false; + server = null; + } + }, plugin); + + } + + + function log() { + if (!server) return console.error("Cannot log, client is offline"); + + var args = Array.prototype.slice.call(arguments); + var message = ""; + args.forEach(function (arg) { + if (typeof arg === "object") { + return message += JSON.stringify(arg); + } + message += arg; + }); + + server.log(message); + } + + plugin.on("load", function() { + load(); + }); + + plugin.on("unload", function() { + loaded = false; + server = null; + }); + + plugin.freezePublicAPI({ + log: log + }); + + register(null, { + "vfs.log": plugin + }); + } +}) \ No newline at end of file