Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

luci-app-v2raya: update log display #325

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,67 +9,125 @@
'require poll';
'require uci';
'require view';
'require form';

return view.extend({
render: function() {
/* Thanks to luci-app-aria2 */
var css = ' \
#log_textarea { \
padding: 10px; \
text-align: left; \
} \
#log_textarea pre { \
padding: .5rem; \
word-break: break-all; \
margin: 0; \
} \
.description { \
background-color: #33ccff; \
}';
render: function () {
var css = `
#log_textarea {
margin-top: 10px;
}
#log_textarea pre {
background-color: #f7f7f7;
color: #333;
padding: 10px;
border: 1px solid #ccc;
border-radius: 4px;
font-family: Consolas, Menlo, Monaco, monospace;
font-size: 14px;
line-height: 1.5;
white-space: pre-wrap;
word-wrap: break-word;
overflow-y: auto;
max-height: 800px;
}
#.description {
background-color: #33ccff;
}
.cbi-button-danger {
background-color: #fff;
color: #f00;
border: 1px solid #f00;
border-radius: 4px;
padding: 4px 8px;
font-size: 14px;
cursor: pointer;
margin-top: 10px;
}
.cbi-button-danger:hover {
background-color: #f00;
color: #fff;
}
.cbi-section small {
margin-left: 10px;
}
.cbi-section .cbi-section-actions {
margin-top: 10px;
}
.cbi-section .cbi-section-actions-right {
text-align: right;
}
`;


var log_textarea = E('div', { 'id': 'log_textarea' },
E('img', {
'src': L.resource(['icons/loading.gif']),
'alt': _('Loading...'),
'style': 'vertical-align:middle'
}, _('Collecting data...'))
}, _('数据收集中...'))
);

var log_path = uci.get('v2raya', 'config', 'log_file') || '/var/log/v2raya/v2raya.log';

poll.add(L.bind(function() {
var clear_log_button = E('div', {}, [
E('button', {
'class': 'cbi-button cbi-button-danger',
'click': function (ev) {
ev.preventDefault();
var button = ev.target;
button.disabled = true;
button.textContent = _('清除日志...');
fs.exec_direct('/usr/libexec/v2raya-call', ['clear_log'])
.then(function () {
button.textContent = _('日志清除成功!');
setTimeout(function () {
button.disabled = false;
button.textContent = _('清除日志');
}, 5000);
// 立即刷新日志显示框
var log = E('pre', { 'wrap': 'pre' }, [_('Log is clean.')]);
dom.content(log_textarea, log);
})
.catch(function () {
button.textContent = _('Failed to clear log.');
setTimeout(function () {
button.disabled = false;
button.textContent = _('清除日志');
}, 5000);
});
}
}, _('清除日志'))
]);


poll.add(L.bind(function () {
return fs.read_direct(log_path, 'text')
.then(function(res) {
var log = E('pre', { 'wrap': 'pre' }, [
res.trim() || _('Log is clean.')
]);
.then(function (res) {
var log = E('pre', { 'wrap': 'pre' }, [res.trim() || _('Log is clean.')]);

dom.content(log_textarea, log);
}).catch(function(err) {
var log;
dom.content(log_textarea, log);
log.scrollTop = log.scrollHeight;
}).catch(function (err) {
var log;

if (err.toString().includes('NotFoundError'))
log = E('pre', { 'wrap': 'pre' }, [
_('Log file does not exist.')
]);
else
log = E('pre', { 'wrap': 'pre' }, [
_('Unknown error: %s').format(err)
]);
if (err.toString().includes('NotFoundError')) {
log = E('pre', { 'wrap': 'pre' }, [_('Log file does not exist.')]);
} else {
log = E('pre', { 'wrap': 'pre' }, [_('Unknown error: %s').format(err)]);
}

dom.content(log_textarea, log);
});
dom.content(log_textarea, log);
});
}));

return E([
E('style', [ css ]),
E('div', {'class': 'cbi-map'}, [
E('div', {'class': 'cbi-section'}, [
log_textarea,
E('div', {'style': 'text-align:right'},
E('small', {}, _('Refresh every %s seconds.').format(L.env.pollinterval))
)
])
return E('div', { 'class': 'cbi-map' }, [
E('style', [css]),
E('div', { 'class': 'cbi-section' }, [
clear_log_button,
log_textarea,
E('small', {}, _('每 5 秒刷新一次 ').format(L.env.pollinterval)),
E('div', { 'class': 'cbi-section-actions cbi-section-actions-right' })
])
]);
},
Expand Down
8 changes: 8 additions & 0 deletions applications/luci-app-v2raya/root/usr/libexec/v2raya-call
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/sh

logfile="$(uci -q get v2raya.config.log_file)" || "/var/log/v2raya/v2raya.log"

if [ "$1" == "clear_log" ]; then
# 清空 v2raya.log 文件
> $logfile
fi
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
"description": "Grant access to v2rayA configuration",
"read": {
"file": {
"/etc/init.d/v2raya": [ "exec" ],
"/bin/mkdir -p /etc/v2raya/": [ "exec" ],
"/var/log/v2raya/v2raya.log": [ "read" ]
"/var/log/v2raya/v2raya.log": [ "read" ],
"/usr/bin/v2raya": [ "exec" ],
"/usr/libexec/v2raya-call": [ "exec" ]
},
"ubus": {
"service": [ "list" ]
Expand Down