2016-03-23 15:22:31 +00:00
|
|
|
/** 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");
|
2016-03-23 19:02:05 +00:00
|
|
|
var callback = function(){};
|
2016-03-23 15:22:31 +00:00
|
|
|
|
|
|
|
var args = Array.prototype.slice.call(arguments);
|
2016-03-23 19:02:05 +00:00
|
|
|
if (typeof args[args.length-1] === "function") {
|
|
|
|
callback = args.splice(args.length-1, 1);
|
|
|
|
}
|
|
|
|
|
2016-03-23 15:22:31 +00:00
|
|
|
var message = "";
|
|
|
|
args.forEach(function (arg) {
|
|
|
|
if (typeof arg === "object") {
|
|
|
|
return message += JSON.stringify(arg);
|
|
|
|
}
|
|
|
|
message += arg;
|
|
|
|
});
|
|
|
|
|
2016-03-23 19:02:05 +00:00
|
|
|
server.log(message, callback);
|
2016-03-23 15:22:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
plugin.on("load", function() {
|
|
|
|
load();
|
|
|
|
});
|
|
|
|
|
|
|
|
plugin.on("unload", function() {
|
|
|
|
loaded = false;
|
|
|
|
server = null;
|
|
|
|
});
|
|
|
|
|
|
|
|
plugin.freezePublicAPI({
|
|
|
|
log: log
|
|
|
|
});
|
|
|
|
|
|
|
|
register(null, {
|
|
|
|
"vfs.log": plugin
|
|
|
|
});
|
|
|
|
}
|
|
|
|
})
|