+/** * Module to log stuff<br> * @license Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0) http://creativecommons.org/licenses/by-sa/3.0/ <br> * {@link http://www.indd-skript.de/ | idshelper original code} * @namespace Log * @memberOf H * @author Gregor Fellenz * @author Bastien Eichenberger (add some modifications) * @requires {@String H} */H.Log = (function (my) { /*********************************************** * Privates ***********************************************/ var log_file, log_level; /** * This function write a log with its severity and its content in the log file * @param {String} message the message to log * @param {String} severity the severity [DEBUG, INFO, WARN, ERROR, OFF] */ function write_log (message, severity) { var str; try { log_file.open("e"); log_file.seek(log_file.length); } catch (ex) { throw { name: 'InvalidArgumentError', message: 'the log file do not exist, please init the Logger before using it', fileName: $.fileName, lineNumber: $.line }; } try { str = '%s [%s] [%s] %s'; str = str.printf(Date(), severity, app.activeScript.name, message); }catch (e) { //We're running from ESTK str = '%s [%s] [ESTK] %s'; str = str.printf(Date(), severity, message); } log_file.writeln(str); log_file.close(); } /*********************************************** * Publics ***********************************************/ /** * Function to init the logger * @function init * @memberOf H.Log * @param {Folder} log_dir * @param {Number} log_level */ my.init = function (log_dir, log_level) { if (!log_dir.exists) { log_dir.create(); } if (!log_file) { log_file = new File(log_dir + '/log.log'); } log_level = log_level; } /** * Function to writes a debug log message * @function debug * @memberOf H.Log * @param {String} _message Message to log. */ my.debug = function (message) { if (log_level <= 0) { write_log(message, 'DEBUG'); } }; /** * Function to writes a info log message * @function info * @memberOf H.Log * @param {String} _message Message to log. */ my.info = function (message) { if (log_level <= 1) { write_log(message, 'INFO'); } }; /** * Function to writes a warn log message * @function warn * @memberOf H.Log * @param {String} _message Message to log. */ my.warn = function (message) { if (log_level <= 2) { write_log(message, 'WARN'); } }; /** * Function to writes an error log message * @function error * @memberOf H.Log * @param {String} _message Message to log. */ my.error = function (message) { if (log_level <= 3) { write_log(message, 'ERROR'); } }; return my;})(H.Log || {});
0 commit comments