captainslog
Version:
Universal log wrapper for browser development. Runs on stardates.
272 lines (224 loc) • 6.83 kB
JavaScript
!function(e){if("object"==typeof exports)module.exports=e();else if("function"==typeof define&&define.amd)define(e);else{var f;"undefined"!=typeof window?f=window:"undefined"!=typeof global?f=global:"undefined"!=typeof self&&(f=self),f.Captain=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);throw new Error("Cannot find module '"+o+"'")}var f=n[o]={exports:{}};t[o][0].call(f.exports,function(e){var n=t[o][1][e];return s(n?n:e)},f,f.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(_dereq_,module,exports){
// Generated by CoffeeScript 1.6.3
var StarDate;
module.exports = StarDate = (function() {
function StarDate(d) {
var t, t0, t1, y;
if (d == null) {
d = new Date();
}
switch (typeof d) {
case 'number':
this.stardate = d;
y = Math.floor(d);
t0 = Date.UTC(y, 0, 1, 0, 0, 0, 0);
t1 = Date.UTC(y + 1, 0, 1, 0, 0, 0, 0);
this.date = new Date(t0 + (t1 - t0) * (d - y));
break;
case 'object':
if (d instanceof Date) {
this.date = new Date(d);
y = this.date.getUTCFullYear();
t0 = Date.UTC(y, 0, 1, 0, 0, 0, 0);
t1 = Date.UTC(y + 1, 0, 1, 0, 0, 0, 0);
t = Date.UTC(y, this.date.getUTCMonth(), this.date.getUTCDate(), this.date.getUTCHours(), this.date.getUTCMinutes(), this.date.getUTCSeconds(), this.date.getUTCMilliseconds());
this.stardate = y + (t - t0) / (t1 - t0);
} else if (d instanceof StarDate) {
this.stardate = d.stardate;
this.date = new Date(d.date);
} else {
throw "Unable to convert " + d + " to StarDate";
}
break;
default:
throw "Unable to convert " + d + " to StarDate";
}
}
StarDate.prototype.canonical = function() {
return this.stardate.toFixed(15);
};
StarDate.prototype.short = function() {
return this.stardate.toFixed(3);
};
return StarDate;
})();
},{}],2:[function(_dereq_,module,exports){
(function (global){
;
// Dependencies
var StarDate = _dereq_('stardate');
/**
* @function noop
* @desc
* No operation function
*/
function noop() {
}
/**
* @function logWriter
* @param {Object} arg
* @desc
* Formats arg if arg is an error. "Inspired" by https://github.com/angular/angular.js/blob/master/src/ng/log.js#L123
*/
function formatError(arg) {
if (arg instanceof Error) {
if (arg.stack) {
arg = (arg.message && arg.stack.indexOf(arg.message) === -1) ? 'Error: ' + arg.message + '\n' + arg.stack : arg.stack;
} else if (arg.sourceURL) {
arg = arg.message + '\n' + arg.sourceURL + ':' + arg.line;
}
}
return arg;
}
/**
* @function logFactory
* @param {String} type
* @return {Function} log
* @desc
* Factory function that creates single method of logging. "Inspired" by https://github.com/angular/angular.js/blob/master/src/ng/log.js#L136
*/
function logFactory(type) {
var console = global.console || {},
logFn = console[type] || console.log || noop,
hasApply = false;
// Reading logFn.apply throws an error in IE11 in IE8 document mode.
try {
hasApply = !!logFn.apply;
} catch (e) {}
if (hasApply) {
return function() {
var args = [];
for (var i = 0; i < arguments.length; i++) {
args.push(formatError(arguments[i]));
}
return logFn.apply(console, args);
};
}
return function(arg1, arg2) {
logFn(arg1, arg2 === null ? '' : arg2);
};
}
/**
* @constant computer
* @desc
* Private object that all captains use for logging.
*/
var computer = {
log: logFactory('log'),
debug: logFactory('debug'),
warn: logFactory('warn'),
error: logFactory('error')
};
/**
* @constant defaults
* @desc
* Default settings object
*/
var defaults = {
name: 'James T. Kirk', // The only real Captain
debug: true
};
/**
* @class Captain
* @param {String} name
* @desc
* Constructor class.
*/
function Captain(name) {
this.settings = {
name: typeof name === 'string' ? name : defaults.name,
debug: defaults.debug
};
this.history = [];
return this;
}
/**
* @method Captain.toggleDebug
* @param {Bool} bool - truthy/falsy value to set debug mode to. If undefined, will act as a switch from the current value.
* @desc
* Toggles debug mode. If false, all debug logs won't be outputted to console.
*/
Captain.prototype.toggleDebug = function(bool) {
this.settings.debug = bool === undefined ? !this.settings.debug : !!bool;
return this.settings.debug;
}
/**
* @method Captain._entry
* @param {String} type
* @param {Array} args
* @param {Bool} silent
* @return {Object} entry
* @desc
* Internal helper. Enters a log entry (`args`) into the Captain log using `type`.
*/
Captain.prototype._entry = function(type, args, silent) {
if (!silent && computer[type]) {
computer[type].apply(this, args);
}
var entry = {
stardate: new StarDate(),
type: type,
message: args
};
this.history.push(entry);
return entry;
};
/**
* @method Captain.log
* @return {Object} entry
* @desc
* Enters a log entry (`arguments`) into the Captain log using `log`.
*/
Captain.prototype.log = function() {
return this._entry('log', arguments);
};
/**
* @method Captain.debug
* @return {Object} entry
* @desc
* Enters a log entry (`arguments`) into the Captain log using `debug`. Only outputs if `Captain.settings.debug === true`.
*/
Captain.prototype.debug = function() {
return this._entry('debug', arguments, !this.settings.debug);
};
/**
* @method Captain.log
* @return {Object} entry
* @desc
* Enters a log entry (`arguments`) into the Captain log using `warn`.
*/
Captain.prototype.warn = function() {
return this._entry('warn', arguments);
};
/**
* @method Captain.log
* @return {Object} entry
* @desc
* Enters a log entry (`arguments`) into the Captain log using `error`.
*/
Captain.prototype.error = function() {
return this._entry('error', arguments);
};
/**
* @method Captain.log
* @return {Object} entry
* @desc
* Outputs all log entries in `Captain.history` using `type`, showing the entered `stardate` and `message` (`arguments`).
*/
Captain.prototype.read = function() {
var args;
for (var i = 0; i < this.history.length; i++) {
args = ['Captain\'s log, star date: ', this.history[i].stardate.stardate, '\n', this.history[i].message];
if (computer[this.history[i].type]) {
computer[this.history[i].type].apply(this, args);
} else {
computer.log.apply(this, args);
}
}
};
// Export
module.exports = Captain;
}).call(this,typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
},{"stardate":1}]},{},[2])
(2)
});