logger.js

来自「国外很不错的一个开源OA系统Group-Office」· JavaScript 代码 · 共 1,560 行 · 第 1/3 页

JS
1,560
字号
/*Copyright (c) 2006, Yahoo! Inc. All rights reserved.Code licensed under the BSD License:http://developer.yahoo.com/yui/license.txtversion: 0.12.0*//****************************************************************************//****************************************************************************//****************************************************************************//** * The LogMsg class defines a single log message. * * @class LogMsg * @constructor * @param oConfigs {Object} Object literal of configuration params. */ YAHOO.widget.LogMsg = function(oConfigs) {    // Parse configs    if (typeof oConfigs == "object") {        for(var param in oConfigs) {            this[param] = oConfigs[param];        }    } }; ///////////////////////////////////////////////////////////////////////////////// Public member variables////////////////////////////////////////////////////////////////////////////////** * Log message. * * @property msg * @type String */YAHOO.widget.LogMsg.prototype.msg = null; /** * Log timestamp. * * @property time * @type Date */YAHOO.widget.LogMsg.prototype.time = null;/** * Log category. * * @property category * @type String */YAHOO.widget.LogMsg.prototype.category = null;/** * Log source. The first word passed in as the source argument. * * @property source * @type String */YAHOO.widget.LogMsg.prototype.source = null;/** * Log source detail. The remainder of the string passed in as the source argument, not * including the first word (if any). * * @property sourceDetail * @type String */YAHOO.widget.LogMsg.prototype.sourceDetail = null;/****************************************************************************//****************************************************************************//****************************************************************************//** * The LogWriter class provides a mechanism to log messages through * YAHOO.widget.Logger from a named source. * * @class LogWriter * @constructor * @param sSource {String} Source of LogWriter instance. */YAHOO.widget.LogWriter = function(sSource) {    if(!sSource) {        YAHOO.log("Could not instantiate LogWriter due to invalid source.",            "error", "LogWriter");        return;    }    this._source = sSource; };///////////////////////////////////////////////////////////////////////////////// Public methods/////////////////////////////////////////////////////////////////////////////// /** * Public accessor to the unique name of the LogWriter instance. * * @method toString * @return {String} Unique name of the LogWriter instance. */YAHOO.widget.LogWriter.prototype.toString = function() {    return "LogWriter " + this._sSource;};/** * Logs a message attached to the source of the LogWriter. * * @method log * @param sMsg {String} The log message. * @param sCategory {String} Category name. */YAHOO.widget.LogWriter.prototype.log = function(sMsg, sCategory) {    YAHOO.widget.Logger.log(sMsg, sCategory, this._source);};/** * Public accessor to get the source name. * * @method getSource * @return {String} The LogWriter source. */YAHOO.widget.LogWriter.prototype.getSource = function() {    return this._sSource;};/** * Public accessor to set the source name. * * @method setSource * @param sSource {String} Source of LogWriter instance. */YAHOO.widget.LogWriter.prototype.setSource = function(sSource) {    if(!sSource) {        YAHOO.log("Could not set source due to invalid source.", "error", this.toString());        return;    }    else {        this._sSource = sSource;    }};///////////////////////////////////////////////////////////////////////////////// Private member variables////////////////////////////////////////////////////////////////////////////////** * Source of the LogWriter instance. * * @property _source * @type String * @private */YAHOO.widget.LogWriter.prototype._source = null;/****************************************************************************//****************************************************************************//****************************************************************************//** * The LogReader class provides UI to read messages logged to YAHOO.widget.Logger. * * @class LogReader * @constructor * @param elContainer {HTMLElement} (optional) DOM element reference of an existing DIV. * @param elContainer {String} (optional) String ID of an existing DIV. * @param oConfigs {Object} (optional) Object literal of configuration params. */YAHOO.widget.LogReader = function(elContainer, oConfigs) {    var oSelf = this;    this._sName = YAHOO.widget.LogReader._index;    YAHOO.widget.LogReader._index++;    // Parse config vars here    if (typeof oConfigs == "object") {        for(var param in oConfigs) {            this[param] = oConfigs[param];        }    }    // Attach container...    if(elContainer) {        if(typeof elContainer == "string") {            this._elContainer = document.getElementById(elContainer);        }        else if(elContainer.tagName) {            this._elContainer = elContainer;        }        this._elContainer.className = "yui-log";    }    // ...or create container from scratch    if(!this._elContainer) {        if(YAHOO.widget.LogReader._elDefaultContainer) {            this._elContainer =  YAHOO.widget.LogReader._elDefaultContainer;        }        else {            this._elContainer = document.body.appendChild(document.createElement("div"));            this._elContainer.id = "yui-log";            this._elContainer.className = "yui-log";            YAHOO.widget.LogReader._elDefaultContainer = this._elContainer;        }        // If implementer has provided container values, trust and set those        var containerStyle = this._elContainer.style;        if(this.width) {            containerStyle.width = this.width;        }        if(this.left) {            containerStyle.left = this.left;        }        if(this.right) {            containerStyle.right = this.right;        }        if(this.bottom) {            containerStyle.bottom = this.bottom;        }        if(this.top) {            containerStyle.top = this.top;        }        if(this.fontSize) {            containerStyle.fontSize = this.fontSize;        }    }    if(this._elContainer) {        // Create header        if(!this._elHd) {            this._elHd = this._elContainer.appendChild(document.createElement("div"));            this._elHd.id = "yui-log-hd" + this._sName;            this._elHd.className = "yui-log-hd";            this._elCollapse = this._elHd.appendChild(document.createElement("div"));            this._elCollapse.className = "yui-log-btns";            this._btnCollapse = document.createElement("input");            this._btnCollapse.type = "button";            this._btnCollapse.style.fontSize =                YAHOO.util.Dom.getStyle(this._elContainer,"fontSize");            this._btnCollapse.className = "yui-log-button";            this._btnCollapse.value = "Collapse";            this._btnCollapse = this._elCollapse.appendChild(this._btnCollapse);            YAHOO.util.Event.addListener(                oSelf._btnCollapse,'click',oSelf._onClickCollapseBtn,oSelf);            this._title = this._elHd.appendChild(document.createElement("h4"));            this._title.innerHTML = "Logger Console";            // If Drag and Drop utility is available...            // ...and this container was created from scratch...            // ...then make the header draggable            if(YAHOO.util.DD &&            (YAHOO.widget.LogReader._elDefaultContainer == this._elContainer)) {                var ylog_dd = new YAHOO.util.DD(this._elContainer.id);                ylog_dd.setHandleElId(this._elHd.id);                this._elHd.style.cursor = "move";            }        }        // Ceate console        if(!this._elConsole) {            this._elConsole =                this._elContainer.appendChild(document.createElement("div"));            this._elConsole.className = "yui-log-bd";            // If implementer has provided console, trust and set those            if(this.height) {                this._elConsole.style.height = this.height;            }        }        // Don't create footer if disabled        if(!this._elFt && this.footerEnabled) {            this._elFt = this._elContainer.appendChild(document.createElement("div"));            this._elFt.className = "yui-log-ft";            this._elBtns = this._elFt.appendChild(document.createElement("div"));            this._elBtns.className = "yui-log-btns";            this._btnPause = document.createElement("input");            this._btnPause.type = "button";            this._btnPause.style.fontSize =                YAHOO.util.Dom.getStyle(this._elContainer,"fontSize");            this._btnPause.className = "yui-log-button";            this._btnPause.value = "Pause";            this._btnPause = this._elBtns.appendChild(this._btnPause);            YAHOO.util.Event.addListener(                oSelf._btnPause,'click',oSelf._onClickPauseBtn,oSelf);            this._btnClear = document.createElement("input");            this._btnClear.type = "button";            this._btnClear.style.fontSize =                YAHOO.util.Dom.getStyle(this._elContainer,"fontSize");            this._btnClear.className = "yui-log-button";            this._btnClear.value = "Clear";            this._btnClear = this._elBtns.appendChild(this._btnClear);            YAHOO.util.Event.addListener(                oSelf._btnClear,'click',oSelf._onClickClearBtn,oSelf);            this._elCategoryFilters = this._elFt.appendChild(document.createElement("div"));            this._elCategoryFilters.className = "yui-log-categoryfilters";            this._elSourceFilters = this._elFt.appendChild(document.createElement("div"));            this._elSourceFilters.className = "yui-log-sourcefilters";        }    }    // Initialize internal vars    if(!this._buffer) {        this._buffer = []; // output buffer    }    // Timestamp of last log message to console    this._lastTime = YAHOO.widget.Logger.getStartTime();         // Subscribe to Logger custom events    YAHOO.widget.Logger.newLogEvent.subscribe(this._onNewLog, this);    YAHOO.widget.Logger.logResetEvent.subscribe(this._onReset, this);    // Initialize category filters    this._categoryFilters = [];    var catsLen = YAHOO.widget.Logger.categories.length;    if(this._elCategoryFilters) {        for(var i=0; i < catsLen; i++) {            this._createCategoryCheckbox(YAHOO.widget.Logger.categories[i]);        }    }    // Initialize source filters    this._sourceFilters = [];    var sourcesLen = YAHOO.widget.Logger.sources.length;    if(this._elSourceFilters) {        for(var j=0; j < sourcesLen; j++) {            this._createSourceCheckbox(YAHOO.widget.Logger.sources[j]);        }    }    YAHOO.widget.Logger.categoryCreateEvent.subscribe(this._onCategoryCreate, this);    YAHOO.widget.Logger.sourceCreateEvent.subscribe(this._onSourceCreate, this);    this._filterLogs();    YAHOO.log("LogReader initialized", null, this.toString());};///////////////////////////////////////////////////////////////////////////////// Public member variables////////////////////////////////////////////////////////////////////////////////** * Whether or not the log reader is enabled to output log messages. * * @property logReaderEnabled * @type Boolean * @default true */YAHOO.widget.LogReader.prototype.logReaderEnabled = true;/** * Public member to access CSS width of the log reader container. * * @property width * @type String */YAHOO.widget.LogReader.prototype.width = null;/** * Public member to access CSS height of the log reader container. * * @property height * @type String */YAHOO.widget.LogReader.prototype.height = null;/** * Public member to access CSS top position of the log reader container. * * @property top * @type String */YAHOO.widget.LogReader.prototype.top = null;/** * Public member to access CSS left position of the log reader container. * * @property left * @type String */YAHOO.widget.LogReader.prototype.left = null;/** * Public member to access CSS right position of the log reader container. * * @property right * @type String */YAHOO.widget.LogReader.prototype.right = null;/** * Public member to access CSS bottom position of the log reader container. * * @property bottom * @type String */YAHOO.widget.LogReader.prototype.bottom = null;/** * Public member to access CSS font size of the log reader container. * * @property fontSize * @type String */YAHOO.widget.LogReader.prototype.fontSize = null;/** * Whether or not the footer UI is enabled for the log reader. * * @property footerEnabled * @type Boolean * @default true */YAHOO.widget.LogReader.prototype.footerEnabled = true;/** * Whether or not output is verbose (more readable). Setting to true will make * output more compact (less readable). * * @property verboseOutput * @type Boolean * @default true */YAHOO.widget.LogReader.prototype.verboseOutput = true;/** * Whether or not newest message is printed on top. * * @property newestOnTop * @type Boolean */YAHOO.widget.LogReader.prototype.newestOnTop = true;/** * Maximum number of messages a LogReader console will display. * * @property thresholdMax * @type Number * @default 500 */YAHOO.widget.LogReader.prototype.thresholdMax = 500;/** * When a LogReader console reaches its thresholdMax, it will clear out messages * and print out the latest thresholdMin number of messages. * * @property thresholdMin * @type Number * @default 100 */YAHOO.widget.LogReader.prototype.thresholdMin = 100;///////////////////////////////////////////////////////////////////////////////// Public methods/////////////////////////////////////////////////////////////////////////////// /** * Public accessor to the unique name of the LogReader instance. * * @method toString * @return {String} Unique name of the LogReader instance. */YAHOO.widget.LogReader.prototype.toString = function() {    return "LogReader instance" + this._sName;};/** * Pauses output of log messages. While paused, log messages are not lost, but * get saved to a buffer and then output upon resume of log reader. * * @method pause */YAHOO.widget.LogReader.prototype.pause = function() {    this._timeout = null;    this.logReaderEnabled = false;};/** * Resumes output of log messages, including outputting any log messages that * have been saved to buffer while paused. * * @method resume */YAHOO.widget.LogReader.prototype.resume = function() {    this.logReaderEnabled = true;    this._printBuffer();};/** * Hides UI of log reader. Logging functionality is not disrupted. * * @method hide */YAHOO.widget.LogReader.prototype.hide = function() {    this._elContainer.style.display = "none";};/** * Shows UI of log reader. Logging functionality is not disrupted. * * @method show */YAHOO.widget.LogReader.prototype.show = function() {    this._elContainer.style.display = "block";};

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?