Minor fixes & improvements.

This commit is contained in:
gSpot
2024-05-12 18:50:13 +03:00
parent 2ca0798081
commit 83e8252f92
10 changed files with 182 additions and 150 deletions
@@ -89,13 +89,15 @@ return view.extend({
let lines = `<tr class="tr"><td class="td center">${_('No entries available...')}</td></tr>`;
let ipTable = E('table', { 'id': 'ipTable', 'class': 'table' });
ipDataArray.sort((a, b) => a[1] - b[1]);
if(ipDataArray.length > 0) {
lines = [];
ipDataArray.forEach((e, i) => {
if(e) {
lines.push(
`<tr class="tr"><td class="td left" data-title="${_('IP address')}">${e[0]}</td>` +
`<td class="td left" data-title="${_('Timeout')}">${(e[1]) ? this.secToTimeString(e[1]) : ''}</td></tr>`
`<td class="td left" data-title="${_('Timeout')}">${this.secToTimeString(e[1] | 0)}</td></tr>`
);
};
});
@@ -104,7 +106,7 @@ return view.extend({
ipTable.append(
E('tr', { 'class': 'tr table-titles' }, [
E('th', { 'class': 'th left', 'style': 'min-width:33%' }, _('IP address')),
(ipDataArray[0][1]) ? E('th', { 'class': 'th left' }, _('Timeout')) : ''
E('th', { 'class': 'th left' }, _('Timeout')),
])
);
};
@@ -0,0 +1,154 @@
'use strict';
'require baseclass';
'require fs';
'require view.ruantiblock.log-widget as widget';
return baseclass.extend({
view: widget.view.extend({
/**
* Pattern for picking application-specific entries from the log.
*
* @property {string} appPattern
*/
appPattern : '^',
/**
* Enable "tail" option for the logread (logread -l).
* Must be disabled for application-specific log.
*
* @property {bool} loggerTail
*/
loggerTail : false,
logdRegexp : new RegExp(/^([^\s]{3}\s+[^\s]{3}\s+\d{1,2}\s+\d{1,2}:\d{1,2}:\d{1,2}\s+\d{4})\s+([a-z0-9]+)\.([a-z]+)\s+(.*)$/),
syslog_ngRegexp: new RegExp(/^([^\s]{3}\s+\d{1,2}\s+\d{1,2}:\d{1,2}:\d{1,2})\s+([^\s]+)\s+(.*)$/),
entryRegexp : null,
isLoggerChecked: false,
entriesHandler : null,
logger : null,
getLogHash() {
return this.getLogData(1, true).then(data => {
return data || '';
});
},
// logd
logdHandler(strArray, lineNum) {
return [
lineNum, // # (Number)
strArray[1], // Timestamp (String)
null, // Host (String)
strArray[2], // Facility (String)
strArray[3], // Level (String)
this.htmlEntities(strArray[4]) || ' ', // Message (String)
];
},
// syslog-ng
syslog_ngHandler(strArray, lineNum) {
if(!(strArray[2] in this.logHosts)) {
this.logHosts[strArray[2]] = this.makeLogHostsDropdownItem(strArray[2]);
};
return [
lineNum, // # (Number)
strArray[1], // Timestamp (String)
strArray[2], // Host (String)
null, // Facility (String)
null, // Level (String)
this.htmlEntities(strArray[3]) || ' ', // Message (String)
];
},
checkLogread() {
return Promise.all([
L.resolveDefault(fs.stat('/sbin/logread'), null),
L.resolveDefault(fs.stat('/usr/sbin/logread'), null),
]).then(stat => {
let logger = (stat[0]) ? stat[0].path : (stat[1]) ? stat[1].path : null;
if(logger) {
this.logger = logger;
} else {
throw new Error(_('Logread not found'));
};
});
},
async getLogData(tail, extraTstamp=false) {
if(!this.logger) {
await this.checkLogread();
};
let loggerArgs = [];
if(this.loggerTail && tail) {
loggerArgs.push('-l', String(tail));
};
loggerArgs.push('-e', this.appPattern);
if(extraTstamp) {
loggerArgs.push('-t');
};
return fs.exec_direct(this.logger, loggerArgs, 'text').catch(err => {
throw new Error(_('Unable to load log data:') + ' ' + err.message);
});
},
parseLogData(logdata, tail) {
if(!logdata) {
return [];
};
let unsupportedLog = false;
let strings = logdata.trim().split(/\n/);
if(!this.loggerTail && tail && tail > 0 && strings) {
strings = strings.slice(-tail);
};
this.totalLogLines = strings.length;
let entriesArray = strings.map((e, i) => {
if(!this.isLoggerChecked) {
if(this.logdRegexp.test(e)) {
this.entryRegexp = this.logdRegexp;
this.isFacilities = true;
this.isLevels = true;
this.logHosts = {};
this.entriesHandler = this.logdHandler;
}
else if(this.syslog_ngRegexp.test(e)) {
this.entryRegexp = this.syslog_ngRegexp;
this.isHosts = true;
this.logFacilities = {};
this.logLevels = {};
this.entriesHandler = this.syslog_ngHandler;
} else {
unsupportedLog = true;
return;
};
this.isLoggerChecked = true;
};
let strArray = e.match(this.entryRegexp);
if(strArray) {
return this.entriesHandler(strArray, i + 1);
} else {
unsupportedLog = true;
return;
};
});
if(unsupportedLog) {
throw new Error(_('Unable to load log data:') + ' ' + _('Unsupported log format'));
} else {
if(this.logSortingValue === 'desc') {
entriesArray.reverse();
};
return entriesArray;
};
},
}),
});
@@ -171,6 +171,13 @@ return baseclass.extend({
*/
title : null,
/**
* Enable auto refresh log.
*
* @property {bool} autoRefresh
*/
autoRefresh : false,
pollInterval : L.env.pollinterval,
logFacilities : {
@@ -443,7 +450,7 @@ return baseclass.extend({
* @param {string} logdata
* @param {number} tail
* @returns {Array<number, string|null, string|null, string|null, string|null, string|null>}
* Returns an array of values: [ #, Timestamp, Host, Level, Facility, Message ].
* Returns an array of values: [ #, Timestamp, Host, Facility, Level, Message ].
*/
parseLogData(logdata, tail) {
throw new Error('parseLogData must be overridden by a subclass');
@@ -885,7 +892,7 @@ return baseclass.extend({
load() {
this.restoreSettingsFromLocalStorage();
if(typeof(this.getLogHash) != 'function') {
if(!this.autoRefresh || typeof(this.getLogHash) != 'function') {
this.isAutorefresh = false;
this.autoRefreshValue = false;
};
@@ -1026,13 +1033,7 @@ return baseclass.extend({
if(this.fastTailValue > 0) {
this.fastTailValue += this.fastTailIncrement;
};
return this.reloadLog(this.fastTailValue)/*.then(() => {
if(this.logSortingValue == 'desc') {
this.scrollToBottom();
} else {
this.scrollToTop();
};
});*/
return this.reloadLog(this.fastTailValue);
}),
}, `+${this.fastTailIncrement}`);
@@ -1,7 +1,7 @@
'use strict';
'require baseclass';
'require ui';
'require view.ruantiblock.log-base as abc';
'require view.ruantiblock.log-base as base';
document.head.append(E('style', {'type': 'text/css'},
`
@@ -26,7 +26,7 @@ document.head.append(E('style', {'type': 'text/css'},
`));
return baseclass.extend({
view: abc.view.extend({
view: base.view.extend({
filterHighlightFunc(match) {
return `<span class="log-highlight-item">${match}</span>`;
@@ -1,135 +1,10 @@
'require fs';
'require rpc';
'require ui';
'require view.ruantiblock.log-widget as abc';
'use strict';
'require view.ruantiblock.log-abstract as abc';
'require view.ruantiblock.tools as tools';
return abc.view.extend({
viewName : 'ruantiblock',
title : _('Ruantiblock') + ' - ' + _('Log'),
logdRegexp : new RegExp(/^([^\s]{3}\s+[^\s]{3}\s+\d{1,2}\s+\d{1,2}:\d{1,2}:\d{1,2}\s+\d{4})\s+([a-z]+)\.([a-z]+)\s+(.*)$/),
syslog_ngRegexp: new RegExp(/^([^\s]{3}\s+\d{1,2}\s+\d{1,2}:\d{1,2}:\d{1,2})\s+([^\s]+)\s+(.*)$/),
entryRegexp : null,
isLoggerChecked: false,
entriesHandler : null,
logger : null,
getLogHash : null,
// logd
logdHandler(strArray, lineNum) {
return [
lineNum, // # (Number)
strArray[1], // Timestamp (String)
null, // Host (String)
strArray[2], // Facility (String)
strArray[3], // Level (String)
this.htmlEntities(strArray[4]) || ' ', // Message (String)
];
},
// syslog-ng
syslog_ngHandler(strArray, lineNum) {
if(!(strArray[2] in this.logHosts)) {
this.logHosts[strArray[2]] = this.makeLogHostsDropdownItem(strArray[2]);
};
return [
lineNum, // # (Number)
strArray[1], // Timestamp (String)
strArray[2], // Host (String)
null, // Facility (String)
null, // Level (String)
this.htmlEntities(strArray[3]) || ' ', // Message (String)
];
},
checkLogread() {
return Promise.all([
L.resolveDefault(fs.stat('/sbin/logread'), null),
L.resolveDefault(fs.stat('/usr/sbin/logread'), null),
]).then(stat => {
let logger = (stat[0]) ? stat[0].path : (stat[1]) ? stat[1].path : null;
if(logger) {
this.logger = logger;
} else {
throw new Error(_('Logread not found'));
};
});
},
async getLogData(tail, extraTstamp=false) {
if(!this.logger) {
await this.checkLogread();
};
let loggerArgs = [];
loggerArgs.push('-e', tools.appName + ':');
if(extraTstamp) {
loggerArgs.push('-t');
};
return fs.exec_direct(this.logger, loggerArgs, 'text').catch(err => {
throw new Error(_('Unable to load log data:') + ' ' + err.message);
});
},
parseLogData(logdata, tail) {
if(!logdata) {
return [];
};
let unsupportedLog = false;
let strings = logdata.trim().split(/\n/);
if(tail && tail > 0 && strings) {
strings = strings.slice(-tail);
};
this.totalLogLines = strings.length;
let entriesArray = strings.map((e, i) => {
if(!this.isLoggerChecked) {
if(this.logdRegexp.test(e)) {
this.entryRegexp = this.logdRegexp;
this.isFacilities = true;
this.isLevels = true;
this.logHosts = {};
this.entriesHandler = this.logdHandler;
}
else if(this.syslog_ngRegexp.test(e)) {
this.entryRegexp = this.syslog_ngRegexp;
this.isHosts = true;
this.logFacilities = {};
this.logLevels = {};
this.entriesHandler = this.syslog_ngHandler;
} else {
unsupportedLog = true;
return;
};
this.isLoggerChecked = true;
};
let strArray = e.match(this.entryRegexp);
if(strArray) {
return this.entriesHandler(strArray, i + 1);
} else {
unsupportedLog = true;
return;
};
});
if(unsupportedLog) {
throw new Error(_('Unable to load log data:') + ' ' + _('Unsupported log format'));
} else {
if(this.logSortingValue === 'desc') {
entriesArray.reverse();
};
return entriesArray;
};
},
viewName : 'ruantiblock',
title : _('Ruantiblock') + ' - ' + _('Log'),
autoRefresh: false,
appPattern : tools.appName + ':',
});