📄 logger.js
字号:
YAHOO.widget.LogReader.prototype._categoryFiltersEl = null;
/**
* Container element for log reader source filter checkboxes.
*
* @type HTMLElement
* @private
*/
YAHOO.widget.LogReader.prototype._sourceFiltersEl = null;
/**
* Log reader pause button element.
*
* @type HTMLElement
* @private
*/
YAHOO.widget.LogReader.prototype._pauseBtn = null;
/**
* lear button element.
*
* @type HTMLElement
* @private
*/
YAHOO.widget.LogReader.prototype._clearBtn = null;
/***************************************************************************
* Private methods
***************************************************************************/
/**
* Creates the UI for a category filter in the log reader footer element.
*
* @param {string} category Category name
* @private
*/
YAHOO.widget.LogReader.prototype._createCategoryCheckbox = function(category) {
var oSelf = this;
if(this._ftEl) {
var parentEl = this._categoryFiltersEl;
var filters = this._categoryFilters;
var filterEl = parentEl.appendChild(document.createElement("span"));
filterEl.className = "ylog_filtergrp";
// Append el at the end so IE 5.5 can set "type" attribute
var categoryChk = document.createElement("input");
categoryChk.className = "ylog_filter" + category;
categoryChk.type = "checkbox";
categoryChk.category = category;
categoryChk.checked = true;
categoryChk = filterEl.appendChild(categoryChk);
// Add this checked filter to the internal array of filters
filters.push(category);
// Subscribe to the click event
YAHOO.util.Event.addListener(categoryChk,'click',oSelf._onCheckCategory,oSelf);
// Create and class the text label
var categoryChkLbl = filterEl.appendChild(document.createElement("span"));
categoryChkLbl.className = category;
categoryChkLbl.innerHTML = category;
}
};
YAHOO.widget.LogReader.prototype._createSourceCheckbox = function(source) {
var oSelf = this;
if(this._ftEl) {
var parentEl = this._sourceFiltersEl;
var filters = this._sourceFilters;
var filterEl = parentEl.appendChild(document.createElement("span"));
filterEl.className = "ylog_filtergrp";
// Append el at the end so IE 5.5 can set "type" attribute
var sourceChk = document.createElement("input");
sourceChk.className = "ylog_filter" + source;
sourceChk.type = "checkbox";
sourceChk.source = source;
sourceChk.checked = true;
sourceChk = filterEl.appendChild(sourceChk);
// Add this checked filter to the internal array of filters
filters.push(source);
// Subscribe to the click event
YAHOO.util.Event.addListener(sourceChk,'click',oSelf._onCheckSource,oSelf);
// Create and class the text label
var sourceChkLbl = filterEl.appendChild(document.createElement("span"));
sourceChkLbl.className = source;
sourceChkLbl.innerHTML = source;
}
};
/**
* Reprints all log messages in the stack through filters.
*
* @private
*/
YAHOO.widget.LogReader.prototype._filterLogs = function() {
// Reprint stack with new filters
if (this._consoleEl !== null) {
this._clearConsole();
this._printToConsole(YAHOO.widget.Logger.getStack());
}
};
/**
* Clears all outputted log messages from the console and resets the time of the
* last output log message.
*
* @private
*/
YAHOO.widget.LogReader.prototype._clearConsole = function() {
// Clear the buffer of any pending messages
this._timeout = null;
this._buffer = [];
// Reset the rolling timer
this._lastTime = YAHOO.widget.Logger.getStartTime();
var consoleEl = this._consoleEl;
while(consoleEl.hasChildNodes()) {
consoleEl.removeChild(consoleEl.firstChild);
}
};
/**
* Sends buffer of log messages to output and clears buffer.
*
* @private
*/
YAHOO.widget.LogReader.prototype._printBuffer = function() {
this._timeout = null;
if (this._consoleEl !== null) {
var entries = [];
for (var i=0; i<this._buffer.length; i++) {
entries[i] = this._buffer[i];
}
this._buffer = [];
this._printToConsole(entries);
}
};
/**
* Cycles through an array of log messages, and outputs each one to the console
* if its category has not been filtered out.
*
* @param {array} aEntries
* @private
*/
YAHOO.widget.LogReader.prototype._printToConsole = function(aEntries) {
//TODO: much optimization here
//if !compactOutput, set fixed widths for time output
var entriesLen = aEntries.length;
var sourceFiltersLen = this._sourceFilters.length;
var categoryFiltersLen = this._categoryFilters.length;
// Iterate through all log entries to print the ones that filter through
for(var i=0; i<entriesLen; i++) {
var entry = aEntries[i];
var category = entry.category;
var source = entry.source;
var okToPrint = false;
var okToFilterCats = false;
for(var j=0; j<sourceFiltersLen; j++) {
if(source == this._sourceFilters[j]) {
okToFilterCats = true;
break;
}
}
if(okToFilterCats) {
for(var k=0; k<categoryFiltersLen; k++) {
if(category == this._categoryFilters[k]) {
okToPrint = true;
break;
}
}
}
if(okToPrint) {
// To format for console, calculate the elapsed time
// to be from the last item that passed through the filter,
// not the absolute previous item in the stack
var label = entry.category.substring(0,4).toUpperCase();
var time = entry.time;
if (time.toLocaleTimeString) {
var localTime = time.toLocaleTimeString();
}
else {
localTime = time.toString();
}
var msecs = time.getTime();
var startTime = YAHOO.widget.Logger.getStartTime();
var totalTime = msecs - startTime;
var elapsedTime = msecs - this._lastTime;
this._lastTime = msecs;
var compactOutput = (this.compactOutput) ? "" : "<br>";
var output = "<span class='"+category+"'>"+label+"</span> " +
totalTime + " ms (+" +
elapsedTime + ") " + localTime + ": " +
compactOutput+
source + ": "
+ entry.msg;
var oNewElement = (this.newestOnTop) ?
this._consoleEl.insertBefore(document.createElement("p"),this._consoleEl.firstChild):
this._consoleEl.appendChild(document.createElement("p"));
oNewElement.innerHTML = output;
if(!this.newestOnTop) {
this._consoleEl.scrollTop = this._consoleEl.scrollHeight;
}
}
}
};
/***************************************************************************
* Private event handlers
***************************************************************************/
/**
* Handles Logger's categoryCreateEvent.
*
* @param {string} type The event
* @param {array} args Data passed from event firer
* @param {object} oSelf The log reader instance
* @private
*/
YAHOO.widget.LogReader.prototype._onCategoryCreate = function(type, args, oSelf) {
var category = args[0];
if(oSelf._ftEl) {
oSelf._createCategoryCheckbox(category);
}
};
/**
* Handles Logger's sourceCreateEvent.
*
* @param {string} type The event
* @param {array} args Data passed from event firer
* @param {object} oSelf The log reader instance
* @private
*/
YAHOO.widget.LogReader.prototype._onSourceCreate = function(type, args, oSelf) {
var source = args[0];
if(oSelf._ftEl) {
oSelf._createSourceCheckbox(source);
}
};
/**
* Handles check events on the category filter checkboxes.
*
* @param {event} v The click event
* @param {object} oSelf The log reader instance
* @private
*/
YAHOO.widget.LogReader.prototype._onCheckCategory = function(v, oSelf) {
var newFilter = this.category;
var filtersArray = oSelf._categoryFilters;
if(!this.checked) { // Remove category from filters
for(var i=0; i<filtersArray.length; i++) {
if(newFilter == filtersArray[i]) {
filtersArray.splice(i, 1);
break;
}
}
}
else { // Add category to filters
filtersArray.push(newFilter);
}
oSelf._filterLogs();
};
/**
* Handles check events on the category filter checkboxes.
*
* @param {event} v The click event
* @param {object} oSelf The log reader instance
* @private
*/
YAHOO.widget.LogReader.prototype._onCheckSource = function(v, oSelf) {
var newFilter = this.source;
var filtersArray = oSelf._sourceFilters;
if(!this.checked) { // Remove category from filters
for(var i=0; i<filtersArray.length; i++) {
if(newFilter == filtersArray[i]) {
filtersArray.splice(i, 1);
break;
}
}
}
else { // Add category to filters
filtersArray.push(newFilter);
}
oSelf._filterLogs();
};
/**
* Handles click events on the collapse button.
*
* @param {event} v The click event
* @param {object} oSelf The log reader instance
* @private
*/
YAHOO.widget.LogReader.prototype._onClickCollapseBtn = function(v, oSelf) {
var btn = oSelf._collapseBtn;
if(btn.value == "Expand") {
oSelf._consoleEl.style.display = "block";
if(oSelf._ftEl) {
oSelf._ftEl.style.display = "block";
}
btn.value = "Collapse";
}
else {
oSelf._consoleEl.style.display = "none";
if(oSelf._ftEl) {
oSelf._ftEl.style.display = "none";
}
btn.value = "Expand";
}
};
/**
* Handles click events on the pause button.
*
* @param {event} v The click event
* @param {object} oSelf The log reader instance
* @private
*/
YAHOO.widget.LogReader.prototype._onClickPauseBtn = function(v, oSelf) {
var btn = oSelf._pauseBtn;
if(btn.value == "Resume") {
oSelf.resume();
btn.value = "Pause";
}
else {
oSelf.pause();
btn.value = "Resume";
}
};
/**
* Handles click events on the clear button.
*
* @param {event} v The click event
* @param {object} oSelf The log reader instance
* @private
*/
YAHOO.widget.LogReader.prototype._onClickClearBtn = function(v, oSelf) {
oSelf._clearConsole();
};
/**
* Handles Logger's onNewEvent.
*
* @param {string} type The click event
* @param {array} args Data passed from event firer
* @param {object} oSelf The log reader instance
* @private
*/
YAHOO.widget.LogReader.prototype._onNewLog = function(type, args, oSelf) {
var logEntry = args[0];
oSelf._buffer.push(logEntry);
if (oSelf.logReaderEnabled === true && oSelf._timeout === null) {
oSelf._timeout = setTimeout(function(){oSelf._printBuffer();}, 100);
}
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -