1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
|
'use strict';
'require poll';
'require rpc';
'require uci';
'require ui';
'require view';
/* Note that any view implementing this log reader requires the log read
acl permission */
const callLogRead = rpc.declare({
object: 'log',
method: 'read',
params: [ 'lines', 'stream', 'oneshot' ],
expect: { log: [] }
});
var CBILogreadBox = function(logtag, name) {
return L.view.extend({
logFacilityFilter: 'any',
invertLogFacilitySearch: false,
logSeverityFilter: 'any',
invertLogSeveritySearch: false,
logTextFilter: '',
invertLogTextSearch: false,
logTagFilter: logtag ? logtag : '',
logName: name ? name : _('System Log'),
fetchMaxRows: 1000,
facilities: [
['any', 'any', _('Any')],
['0', 'kern', _('Kernel')],
['1', 'user', _('User')],
['2', 'mail', _('Mail')],
['3', 'daemon', _('Daemon')],
['4', 'auth', _('Auth')],
['5', 'syslog', _('Syslog')],
['6', 'lpr', _('LPR')],
['7', 'news', _('News')],
['8', 'uucp', _('UUCP')],
['9', 'cron', _('Cron')],
['10', 'authpriv', _('Auth Priv')],
['11', 'ftp', _('FTP')],
['12', 'ntp', _('NTP')],
['13', 'security', _('Log audit')],
['14', 'console', _('Log alert')],
['15', 'cron', _('Scheduling daemon')],
['16', 'local0', _('Local 0')],
['17', 'local1', _('Local 1')],
['18', 'local2', _('Local 2')],
['19', 'local3', _('Local 3')],
['20', 'local4', _('Local 4')],
['21', 'local5', _('Local 5')],
['22', 'local6', _('Local 6')],
['23', 'local7', _('Local 7')]
],
severity: [
['any','any', _('Any')],
['0', 'emerg', _('Emergency')],
['1', 'alert', _('Alert')],
['2', 'crit', _('Critical')],
['3', 'err', _('Error')],
['4', 'warn', _('Warning')],
['5', 'notice', _('Notice')],
['6', 'info', _('Info')],
['7', 'debug', _('Debug')]
],
async retrieveLog() {
try {
const tz = uci.get('system', '@system[0]', 'zonename')?.replaceAll(' ', '_');
const ts = uci.get('system', '@system[0]', 'clock_timestyle') || 0;
const hc = uci.get('system', '@system[0]', 'clock_hourcycle') || 0;
const logEntries = await callLogRead(this.fetchMaxRows, false, true);
const dateObj = new Intl.DateTimeFormat(undefined, {
dateStyle: 'medium',
timeStyle: (ts == 0) ? 'long' : 'full',
hourCycle: (hc == 0) ? undefined : hc,
timeZone: tz
});
let loglines = logEntries.map(entry => {
const time = new Date(entry?.time);
const datestr = dateObj.format(time);
/* remember to add one since the 'any' entry occupies 1st position i.e. [0] */
const facility = this.facilities[Math.floor(entry?.priority / 8) + 1][1] ?? 'unknown';
const severity = this.severity[(entry?.priority % 8) + 1][1] ?? 'unknown';
return `[${datestr}] ${facility}.${severity}: ${entry?.msg}`;
});
loglines = loglines.filter(line => {
const sevMatch = this.logSeverityFilter === 'any' || line.includes(`.${this.logSeverityFilter}`);
const facMatch = this.logFacilityFilter === 'any' || line.includes(`${this.logFacilityFilter}.`);
return (this.invertLogSeveritySearch != sevMatch)
&& (this.invertLogFacilitySearch != facMatch);
});
loglines = loglines.filter(line => {
return line.toLowerCase().includes(this.logTagFilter?.toLowerCase());
});
loglines = loglines.filter(line => {
const match = line.includes(this.logTextFilter);
return this.invertLogTextSearch ? !match : match;
});
return {
value: loglines?.join('\n'),
rows: loglines?.length + 1
};
}
catch (err) {
ui.addNotification(null, E('p', {}, _('Unable to load log data: ' + err.message)));
return {
value: '',
rows: 0
};
}
},
async pollLog() {
const element = document.getElementById('syslog');
if (element) {
const log = await this.retrieveLog();
element.value = log?.value;
element.rows = log?.rows;
}
},
async load() {
poll.add(this.pollLog.bind(this));
return Promise.all([
uci.load('system'),
]).then(() => this.retrieveLog());
},
render(loglines) {
const scrollDownButton = E('button', {
'id': 'scrollDownButton',
'class': 'cbi-button cbi-button-neutral'
}, _('Scroll to tail', 'scroll to bottom (the tail) of the log file')
);
scrollDownButton.addEventListener('click', () => {
scrollUpButton.scrollIntoView();
scrollDownButton.blur();
});
const scrollUpButton = E('button', {
'id' : 'scrollUpButton',
'class': 'cbi-button cbi-button-neutral'
}, _('Scroll to head', 'scroll to top (the head) of the log file')
);
scrollUpButton.addEventListener('click', () => {
scrollDownButton.scrollIntoView();
scrollUpButton.blur();
});
const self = this;
// Create facility invert checkbox
const facilityInvert = E('input', {
'id': 'invertLogFacilitySearch',
'type': 'checkbox',
'class': 'cbi-input-checkbox',
});
// Create facility select-dropdown from facilities map
const facilitySelect = E('select', {
'id': 'logFacilitySelect',
'class': 'cbi-input-select',
'style': 'margin-bottom:10px',
},
this.facilities.map(([ , val, label]) =>
(val == 'any') ? E('option', { value: val, selected: '' }, label) : E('option', { value: val }, label)
));
// Create severity invert checkbox
const severityInvert = E('input', {
'id': 'invertLogSeveritySearch',
'type': 'checkbox',
'class': 'cbi-input-checkbox',
});
// Create severity select-dropdown from facilities map
const severitySelect = E('select', {
'id': 'logSeveritySelect',
'class': 'cbi-input-select',
},
this.severity.map(([ , val, label]) =>
(val == 'any') ? E('option', { value: val, selected: '' }, label) : E('option', { value: val }, label)
));
// Create raw text search invert checkbox
const filterTextInvert = E('input', {
'id': 'invertLogTextSearch',
'type': 'checkbox',
'class': 'cbi-input-checkbox',
});
// Create raw text search text input
const filterTextInput = E('input', {
'id': 'logTextFilter',
'class': 'cbi-input-text',
});
// Create max rows input
const filterMaxRows = E('input', {
'id': 'logMaxRows',
'type': 'number',
'class': 'cbi-input',
});
/**
* Update the form when log filter parameters change
*/
function handleLogFilterChange() {
self.logFacilityFilter = facilitySelect.value;
self.invertLogFacilitySearch = facilityInvert.checked;
self.logSeverityFilter = severitySelect.value;
self.invertLogSeveritySearch = severityInvert.checked;
self.logTextFilter = filterTextInput.value;
self.invertLogTextSearch = filterTextInvert.checked;
self.fetchMaxRows = Number.parseInt(filterMaxRows.value);
self.pollLog();
}
facilitySelect.addEventListener('change', handleLogFilterChange);
facilityInvert.addEventListener('change', handleLogFilterChange);
severitySelect.addEventListener('change', handleLogFilterChange);
severityInvert.addEventListener('change', handleLogFilterChange);
filterTextInput.addEventListener('input', handleLogFilterChange);
filterTextInvert.addEventListener('change', handleLogFilterChange);
filterMaxRows.addEventListener('change', handleLogFilterChange);
return E([], [
E('h2', {}, [ this.logName ]),
E('div', { 'id': 'content_syslog' }, [
E('div', { class: 'cbi-section-descr' }, this.logTagFilter ? _('The syslog output, pre-filtered for messages related to: ' + this.logTagFilter) : '') ,
E('div', { 'style': 'margin-bottom:10px' }, [
E('label', { 'for': 'invertLogFacilitySearch', 'style': 'margin-right:5px' }, _('Not')),
facilityInvert,
E('label', { 'for': 'logFacilitySelect', 'style': 'margin: 0 5px' }, _('facility:')),
facilitySelect,
E('label', { 'for': 'invertLogSeveritySearch', 'style': 'margin: 0 5px' }, _('Not')),
severityInvert,
E('label', { 'for': 'logSeveritySelect', 'style': 'margin: 0 5px' }, _('severity:')),
severitySelect,
]),
E('div', { 'style': 'margin-bottom:10px' }, [
E('label', { 'for': 'invertLogTextSearch', 'style': 'margin-right:5px' }, _('Not')),
filterTextInvert,
E('label', { 'for': 'logTextFilter', 'style': 'margin: 0 5px' }, _('including:')),
filterTextInput,
E('label', { 'for': 'logMaxRows', 'style': 'margin: 0 5px' }, _('Max rows:')),
filterMaxRows,
]),
E('div', {'style': 'padding-bottom: 20px'}, [scrollDownButton]),
E('textarea', {
'id': 'syslog',
'style': 'font-size:12px',
'readonly': 'readonly',
'wrap': 'off',
'rows': loglines?.rows,
}, [ loglines?.value ]),
E('div', {'style': 'padding-bottom: 20px'}, [scrollUpButton])
])
]);
},
handleSaveApply: null,
handleSave: null,
handleReset: null
});
};
return L.Class.extend({
LogreadBox: CBILogreadBox,
});
|