Created
March 6, 2019 20:03
-
-
Save noize-e/f9c3bf83d44317e87e360d4933d06f67 to your computer and use it in GitHub Desktop.
Logging Functions toolkit
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Logging Functions Toolkit. | |
* | |
* Logs with a prefixed message, if console.log isn't available, none op is performed | |
* @param {object} arguments | |
*/ | |
function log(){ | |
if ( window.console && console.log ) | |
console.log.apply( this, prefixedArray( arguments ) ); | |
} | |
/** | |
* Logs with error's prefix & format, if console.error isn't available use log function. | |
* | |
* @param {object} arguments | |
* @see {function} log() | |
*/ | |
function warn(){ | |
var data = prefixedArray( arguments, "[GA:NOTICE]" ); | |
if ( window.console && console.warn ) | |
return console.warn.apply( this, data ); | |
log( data ); | |
} | |
/** | |
* Logs with error's prefix & format, if console.error isn't available use log function. | |
* | |
* @param {object} arguments | |
* @see {function} log() | |
*/ | |
function error(){ | |
var data = prefixedArray( arguments, "[GA:ERROR]" ); | |
if ( window.console && console.error ) | |
return console.error.apply( this, data ); | |
log( data ); | |
} | |
/** | |
* Gets a function that when called will log information about itself if logging is turned on. | |
* | |
* @param {function} fucntion: to add into the scope and start tracing it. | |
* @param {string} name: of the function to trace. | |
*/ | |
function tracer( func, name ) { | |
return function() { | |
log( "Function '" + name + "' executed.", arguments ); | |
return func.apply( this, arguments ); | |
}; | |
} | |
/** | |
* Decorate any function to catch an exception and log it. | |
*/ | |
function logex( func, context ) { | |
return function() { | |
try { | |
return func.apply( ( context || this ), arguments ); | |
} catch( err ) { | |
error( ( context || this ), err ); | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment