Skip to content

Instantly share code, notes, and snippets.

@esatterwhite
Created August 26, 2011 18:20
Show Gist options
  • Save esatterwhite/1174043 to your computer and use it in GitHub Desktop.
Save esatterwhite/1174043 to your computer and use it in GitHub Desktop.
AMD Style module for adding logging to existing objects
/**
*
* AMD module for logging function calls. Intended for use with require.js ( http://www.requirejs.org )
* inspired by Mootools Log ( https://github.com/mootools/mootools-more/blob/1.2x/Source/Core/Log.js )
* and js-klib ( http://code.google.com/p/js-klib/source/browse/klib.js )
* @author Eric Satterwhite
* @module log
*/
define(['require','exports','array'],function(require, exports, array){
var has_log = ( console && console.log );
var _logged = [];
var api = {};
var enabled = true
function namedWithInner( _fn, orig, prefix ){
var name = getFnName( orig );
if( prefix ){
name = prefix + name;
}
return augment( _fn, {name:name, inner:orig});
};
/**
* Adds properties to an function or object
* @private
* @method augment
* @param _fn {Object} the object to add properties to
* @param obj {Object} object containing the properties to add
* @return _fn {Object} Returns the augmented object
*/
function augment( _fn, obj ){
var key;
for( key in obj ){
if( obj.hasOwnProperty( key ) ){
_fn[key] = obj[key]
}
}
return _fn
};
/**
* Attempts to return the name of a function
* @method getFnName
* @private
* @param fn {Function} A function to retrive a name from
* @return name {String} the name of the function, `anonymous` if one is not found
*/
function getFnName( fn ){
return ( fn.name || 'anonymous' );
};
/**
* returns a wrapped function which attempts to log every function call
* @method loggingWrapped
* @private
* @param _fn {Function} The function to wrap
* @return func {Function} The new function
*/
function loggingWraped(_fn, action ){
action = action || "log";
return namedWithInner(function(){
var result = _fn.apply( this, arguments );
api[action](getFnName( _fn ), "( ", arguments , " )= ", result );
return result;
}, _fn, "debug_");
};
/**
* A wrapper around the "console" object to normalize function calls
* @method log
* @private
*/
function log( ){
var args = array.from( arguments );
var fnName = args.shift();
if( has_log && enabled) {
try{
console[fnName].apply(console, args[0]);
} catch ( e ){
console.log( args[0] );
}
_logged.push( arguments );
} else {
_logged.push( arguments );
}
}
/**
* enables the logging functionality and immediatly logs any back logs
* @method enableLog
* @private
*/
function enableLog( ){
enabled = true;
array.each(_logged, function(args){
log.apply( this, args )
});
_logged = [];
return api;
};
/**
* disables the logging feature. places any calls to log in a back log queue
* @method disableLog
* @private
*/
function disableLog( ){
enabled = false;
return api;
};
api = {
log: function(){
log('log', arguments );
return this;
}
,warn: function(){
log('warn', arguments);
return this;
}
,error: function(){
log('error', arguments);
return this
}
,debug: function(){
log('debug', arguments);
return this;
}
,info: function(){
log('info', arguments);
return this;
}
,enable: function(){
enableLog();
return this;
}
,disable: function(){
disableLog();
return this;
}
/**
* A decorator that, when given an object, will attempt to log out
* all successful function calls from that object via the consoles debug method.
* @decorator debug
* @param obj {Object} The object whos methods you want to debugged
* @return api {Object} return the module object
*/
,$debug: function( obj ){
var key
for( key in obj ){
if( obj.hasOwnProperty( key ) ){
if( typeof obj[key] =='function' ){
obj[key] = loggingWraped( obj[key], 'debug' );
}
}
}
return this;
}
}
return api
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment