Skip to content

Commit

Permalink
auto collapse the same log
Browse files Browse the repository at this point in the history
  • Loading branch information
Maiz committed Feb 2, 2019
1 parent 40764b9 commit 0952e6e
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 43 deletions.
32 changes: 15 additions & 17 deletions src/core/core.less
Original file line number Diff line number Diff line change
Expand Up @@ -213,28 +213,26 @@

.vc-item-content {
margin-right: 60em / @font;
display: block;
display: inline-block;
}

.vc-item-meta {
color: #888;
float: right;
width: 60em / @font;
text-align: right;
.vc-item-repeat {
display: inline-block;
margin-right: 4em / @font;
padding: 0 @fontSize / 2;
color: #D7E0EF;
background-color: #42597F;
border-radius: @fontSize / 1.5;
}

}

.vc-item.vc-item-nometa {

.vc-item-content {
margin-right: 0;
}

.vc-item-meta {
display: none;
}

.vc-item.vc-item-error .vc-item-repeat {
color: #901818;
background-color: #DC2727;
}
.vc-item.vc-item-warn .vc-item-repeat {
color: #987D20;
background-color: #F4BD02;
}

.vc-item .vc-item-code {
Expand Down
8 changes: 5 additions & 3 deletions src/log/default.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,11 @@ class VConsoleDefaultTab extends VConsoleLogTab {
//print error stack info
let stack = !!error && !!error.stack;
let statckInfo = (stack && error.stack.toString()) || '';
that.printLog({logType: 'error', logs: [msg, statckInfo], noOrigin: true});
that.printLog({
logType: 'error',
logs: [msg, statckInfo],
noOrigin: true
});
if (tool.isFunction(that.windowOnError)) {
that.windowOnError.call(window, message, source, lineNo, colNo, error);
}
Expand All @@ -224,7 +228,6 @@ class VConsoleDefaultTab extends VConsoleLogTab {
this.printLog({
logType: 'log',
content: $.render(tplItemCode, {content: cmd, type: 'input'}),
noMeta: true,
style: ''
});
// do not use `eval` or `new Function` to avoid `unsafe-eval` CSP rule
Expand Down Expand Up @@ -289,7 +292,6 @@ class VConsoleDefaultTab extends VConsoleLogTab {
this.printLog({
logType: 'log',
content: $content,
noMeta: true,
style: ''
});
window.winKeys = Object.getOwnPropertyNames(window).sort();
Expand Down
3 changes: 1 addition & 2 deletions src/log/item.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<div class="vc-item vc-item-{{logType}} {{if (!noMeta)}}vc-item-nometa{{/if}} {{style}}">
<span class="vc-item-meta">{{if (!noMeta)}}{{meta}}{{/if}}</span>
<div id="{{_id}}" class="vc-item vc-item-{{logType}} {{style}}">
<div class="vc-item-content"></div>
</div>
99 changes: 79 additions & 20 deletions src/log/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ import tplFoldCode from './item_fold_code.html';

const DEFAULT_MAX_LOG_NUMBER = 1000;
const ADDED_LOG_TAB_ID = [];
let preLog = {
// _id: string
// logType: string
// logText: string
};

class VConsoleLogTab extends VConsolePlugin {
static AddedLogID = [];
Expand Down Expand Up @@ -264,7 +269,7 @@ class VConsoleLogTab extends VConsolePlugin {
window.console[method] = (...args) => {
this.printLog({
logType: method,
logs: args
logs: args,
});
};
});
Expand Down Expand Up @@ -292,6 +297,7 @@ class VConsoleLogTab extends VConsolePlugin {
clearLog() {
$.one('.vc-log', this.$tabbox).innerHTML = '';
this.logNumber = 0;
preLog = {};
}

/**
Expand All @@ -307,15 +313,14 @@ class VConsoleLogTab extends VConsolePlugin {
/**
* print a log to log box
* @protected
* @param string tabName auto|default|system
* @param string _id random unique id
* @param string tabName default|system
* @param string logType log|info|debug|error|warn
* @param array logs `logs` or `content` can't be empty
* @param object content `logs` or `content` can't be empty
* @param boolean noOrigin
* @param boolean noMeta
* @param int date
* @param string style
* @param string meta
*/
printLog(item) {
let logs = item.logs || [];
Expand Down Expand Up @@ -364,6 +369,11 @@ class VConsoleLogTab extends VConsolePlugin {
return;
}

// add id
if (!item._id) {
item._id = '__vc_' + Math.random().toString(36).substring(2, 8);
}

// save log date
if (!item.date) {
item.date = (+new Date());
Expand All @@ -383,17 +393,76 @@ class VConsoleLogTab extends VConsolePlugin {
}
}

// use date as meta
if (!item.meta) {
let date = tool.getDate(item.date);
item.meta = date.hour + ':' + date.minute + ':' + date.second;
// make for previous log
const curLog = {
_id: item._id,
logType: item.logType,
logText: [],
hasContent: !!item.content,
count: 1,
};
for (let i = 0; i < logs.length; i++) {
if (tool.isFunction(logs[i])) {
curLog.logText.push(logs[i].toString());
} else if (tool.isObject(logs[i]) || tool.isArray(logs[i])) {
curLog.logText.push(tool.JSONStringify(logs[i]));
} else {
curLog.logText.push(logs[i]);
}
}
curLog.logText = curLog.logText.join(' ');

// check repeat
if (!curLog.hasContent && preLog.logType === curLog.logType && preLog.logText === curLog.logText) {
this.printRepeatLog();
} else {
this.printNewLog(item, logs);
// save previous log
preLog = curLog;
}


// scroll to bottom if it is in the bottom before
if (this.isInBottom) {
this.autoScrollToBottom();
}

// print log to origin console
if (!item.noOrigin) {
this.printOriginLog(item);
}
}

/**
*
* @protected
*/
printRepeatLog() {
const $item = $.one('#' + preLog._id);
let $repeat = $.one('.vc-item-repeat', $item);
if (!$repeat) {
$repeat = document.createElement('i');
$repeat.className = 'vc-item-repeat';
$item.insertBefore($repeat, $item.lastChild);
}
if (!preLog.count) {
// preLog.count = 1;
}
preLog.count++;
$repeat.innerHTML = preLog.count;
return;
}

/**
*
* @protected
*/
printNewLog(item, logs) {

// create line
let $line = $.render(tplItem, {
_id: item._id,
logType: item.logType,
noMeta: !!item.noMeta,
meta: item.meta,
style: item.style || ''
});

Expand Down Expand Up @@ -437,16 +506,6 @@ class VConsoleLogTab extends VConsolePlugin {
// remove overflow logs
this.logNumber++;
this.limitMaxLogs();

// scroll to bottom if it is in the bottom before
if (this.isInBottom) {
this.autoScrollToBottom();
}

// print log to origin console
if (!item.noOrigin) {
this.printOriginLog(item);
}
}

/**
Expand Down
16 changes: 15 additions & 1 deletion test/log.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<a onclick="circularArray()" href="javascript:;" class="weui_btn weui_btn_default">circularArray</a>
<a onclick="largeObject()" href="javascript:;" class="weui_btn weui_btn_default">largeObject</a>
<a onclick="smallArray()" href="javascript:;" class="weui_btn weui_btn_default">smallArray</a>
<a onclick="repeatLog()" href="javascript:;" class="weui_btn weui_btn_default">repeatLog</a>
<a onclick="windowError()" href="javascript:;" class="weui_btn weui_btn_default">window.error</a>
</div>
</body>
Expand All @@ -40,7 +41,7 @@
function formattedLog() {
console.info('formattedLog() Start');
console.log('[default]', 'This log should be shown in Log tab.');
console.log('[default] Switch to System tab to see next log.');
console.log('[default]', 'Switch to System tab to see next log.');
console.log('[system]', 'This log should be shown in System tab.');
console.info('formattedLog() End');
}
Expand Down Expand Up @@ -98,6 +99,19 @@
console.info('smallArray() End');
}

function repeatLog() {
console.log('repeatLog() Start');
var count = 0;
var timer = setInterval(() => {
count ++;
console.log('repeat', 'foo bar');
if (count >= 100) {
clearInterval(timer);
console.log('repeatLog() End');
}
}, 50);
}

function windowError() {
console.info('windowError() Start');
a.b = 1;
Expand Down

0 comments on commit 0952e6e

Please sign in to comment.