/*
 * Copyright (C) 2008-2010 APN, LLC. All rights reserved
 * References to external sources embedded in the code.
 * 
 * Author: Vishal V. Shah
 * Description: A generic file based toolbar logger.
 * 
 * NOTE: DO NOT INTRODUCE DEPENDENCIES TO OTHER MODULES IN THIS ONE. ALL OTHER MODULES SHOULD
 *       BE ABLE TO SAFELY DEPEND ON IT.
 */
var ATB_Logger=function(){try{this.PREFS_SERVICE=Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);this.PREFS_BRANCH=this.PREFS_SERVICE.getBranch(ATB.Constants.ATB_EXT_PREFS_BRANCH);this.loggingEnabled=this.PREFS_BRANCH.getBoolPref(ATB.Constants.ATB_LOGGING_ENABLED_PREF);this.logLevel=this.PREFS_BRANCH.getCharPref(ATB.Constants.ATB_LOG_LEVEL_PREF);this.toolbarId=this.PREFS_BRANCH.getCharPref(ATB.Constants.ATB_ID_PREF);this.logFile=ATB_Utilities.getProfileDirectory();this.logFile.append("extensions");this.logFile.append(this.PREFS_BRANCH.getCharPref(ATB.Constants.ATB_ID_PREF));if(this.logFile.exists()&&this.logFile.isFile()){var b=ATB_Utilities.getExtensionDirectoryFromFile(this.logFile);if(b){this.logFile=b}}this.logFile.append("logs");this.consoleService=Components.classes["@mozilla.org/consoleservice;1"].getService(Components.interfaces.nsIConsoleService);this.WARN_LOGTYPE_HTML="<div class='logType' style='background-color:#FFFF66;'>WARN</div>";this.DEBUG_LOGTYPE_HTML="<div class='logType' style='background-color:#33CC33;'>DEBUG</div>";this.ERROR_LOGTYPE_HTML="<div class='logType' style='background-color:#FF6666;'>ERROR</div>";this.INFO_LOGTYPE_HTML="<div class='logType' style='background-color:#CCFFFF;'>INFO</div>";this.WARN_PREFIX="*** WARN ***";this.DEBUG_PREFIX="~~~ DEBUG ~~~";this.ERROR_PREFIX="!!!*** ERROR ***!!!";this.INFO_PREFIX="___ INFO ___";this.logfilePath=null;this.logFileStream=null;this.infoEnabled=false;this.debugEnabled=false;this.warnEnabled=false;this.errorEnabled=false;this.construct()}catch(a){try{if(this.consoleService&&this.consoleService.logStringMessage){this.consoleService.logStringMessage("Error while creating error logger: "+a)}}catch(a){}}};ATB_Logger.prototype={cacheDebugLevels:function(){this.infoEnabled=(this.logLevel=="all"||(this.logLevel.indexOf("info")!=-1));this.debugEnabled=(this.logLevel=="all"||(this.logLevel.indexOf("debug")!=-1));this.warnEnabled=(this.logLevel=="all"||(this.logLevel.indexOf("warn")!=-1));this.errorEnabled=(this.logLevel=="all"||(this.logLevel.indexOf("error")!=-1))},initializeLogFile:function(){this.cleanupLogFilesIfExceedQuota(this.logFile);this.logFile.append("asktb-log-"+new Date().getTime()+".html");this.logFile.createUnique(Components.interfaces.nsIFile.NORMAL_FILE_TYPE,438);this.logFilePath=this.logFile.path;this.logFileStream=Components.classes["@mozilla.org/network/file-output-stream;1"].createInstance(Components.interfaces.nsIFileOutputStream);this.logFileStream.init(this.logFile,2|16,438,0)},cleanupLogFilesIfExceedQuota:function(b){if(b&&b.exists()){var a=b.directoryEntries;var f=0;var g=new Array();if(a){var e=null;while(a.hasMoreElements()){e=a.getNext().QueryInterface(Components.interfaces.nsIFile);if(e&&e.isFile()){g.push(e);++f}}}if(f>25){try{for(var d=0;d<g.length;++d){if(g[d].isFile()&&g[d].isWritable()){g[d].remove(false)}}}catch(c){}g=null}}return},construct:function(){if(this.loggingEnabled){this.cacheDebugLevels();if(!this.logFile||!this.logFileStream){this.initializeLogFile();if(this.logFile&&this.logFileStream){var c=new Date();var e="ATB Log File - "+c.toLocaleDateString()+" - "+c.toTimeString();this.log('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html lang="en"><head><title>'+e+'</title>\n<style type="text/css">\nbody {font-family:arial,sans-serif;}thead,tfoot {color: #ffffff; font-variant: small-caps; background: #3366FF none repeat scroll 0 0; padding: 4px; text-align: center;}thead td,tfoot td {font-size: 18px;}td {padding: 2px; font-size: 12px;}.logType {font-variant: small-caps; padding: 4px;}</style>\n<script language="javascript" type="text/javascript">\n function displayRows(type){var allRows = document.getElementById("mainLogTable").rows;\n if(type!="all"){\n for(i=1;i<allRows.length;i++){\n if(allRows[i].getAttribute("name") == type){allRows[i].style.display = "";}\n else {allRows[i].style.display = "none";}\n }\n }\n else {for(i=1;i<allRows.length;i++){allRows[i].style.display ="";}}\n}\n<\/script>\n</head>\n<body>\n');var a="<center><h3>"+e+"</h3>";var d="<table border='1'><caption><em>Filter Messages by Type</em></caption><tr><td><input type='button' onclick='displayRows(\"all\")'  style='background-color:white' value='ALL'/></td>\n<td><input type='button' onclick='displayRows(\"warn\")' style='background-color:#FFFF66;' value='WARN'/></td><td><input type='button' onclick='displayRows(\"debug\")' style='background-color:#33CC33;' value='DEBUG'/></td><td><input type='button' onclick='displayRows(\"error\")' style='background-color:#FF6666;' value='ERROR'/></td><td><input type='button' onclick='displayRows(\"info\")' style='background-color:#CCFFFF;' value='INFO'/></td></tr>  </table></center>";var b="<tr><td>Type</td><td>Timestamp</td><td>Message</td></tr></thead>";this.log(a+d+"<table width='100%' border='1' id='mainLogTable'<thead>"+b+"</thead>\n<tfoot>"+b+"</tfoot>\n<tbody>\n")}}}},destruct:function(){if(this.loggingEnabled){this.info("Destructing ATB.Logger...");this.info("Log file closed.");this.log("</tbody></table></body></html>");if(this.logFileStream){this.logFileStream.close();this.logFileStream=null;this.logFile=null}}},log:function(i,d,g,f){if(this.loggingEnabled){var a="";if(typeof(g)!="undefined"&&g&&g.length>0){if(typeof(f)=="undefined"||!f||f.length==0){f="#ffffff"}var b=new Date();a="\n<tr name="+d+"><td>"+g+"</td><td>"+b.getTime()+" ["+b.toLocaleTimeString()+"]</td><td style='background-color: "+f+";'>"+i+"</td></tr>"}else{a=i}try{this.logFileStream.write(a,a.length)}catch(c){try{if(this.consoleService&&this.consoleService.logStringMessage){this.consoleService.logStringMessage('Error encountered while writing: "'+a+'" to log file: '+c)}}catch(h){}}}},isInfoEnabled:function(){return this.infoEnabled},isDebugEnabled:function(){return this.debugEnabled},isWarnEnabled:function(){return this.warnEnabled},isErrorEnabled:function(){return this.errorEnabled},info:function(){if(this.infoEnabled){this.log(Array.prototype.slice.call(arguments).join(""),"info",this.INFO_LOGTYPE_HTML,"#CCFFFF")}},debug:function(){if(this.debugEnabled){this.log(Array.prototype.slice.call(arguments).join(""),"debug",this.DEBUG_LOGTYPE_HTML)}},warn:function(){if(this.warnEnabled){this.log(Array.prototype.slice.call(arguments).join(""),"warn",this.WARN_LOGTYPE_HTML,"#FFFF66")}},error:function(){if(this.errorEnabled){this.log(Array.prototype.slice.call(arguments).join(""),"error",this.ERROR_LOGTYPE_HTML,"#FF6666")}}};