log.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. 'use strict';
  2. 'require dom';
  3. 'require fs';
  4. 'require poll';
  5. 'require view';
  6. var css = ' \
  7. #log_textarea { \
  8. padding: 10px; \
  9. text-align: left; \
  10. } \
  11. #log_textarea pre { \
  12. padding: .5rem; \
  13. word-break: break-all; \
  14. margin: 0; \
  15. } \
  16. .description { \
  17. background-color: #33ccff; \
  18. }';
  19. function pollLog(e) {
  20. return Promise.all([
  21. fs.exec_direct('/usr/libexec/aria2-call', [ 'tail' ]).then(function(res) {
  22. return res.trim().split(/\n/).reverse().join('\n')
  23. }),
  24. fs.exec_direct('/sbin/logread', [ '-e', 'aria2' ]).then(function(res) {
  25. return res.trim().split(/\n/).reverse().slice(0, 50).join('\n')
  26. })
  27. ]).then(function(data) {
  28. var t = E('pre', { 'wrap': 'pre' }, [
  29. E('div', { 'class': 'description' }, _('Last 50 lines of log file:')),
  30. E('br'),
  31. data[0] || _('No log data.'),
  32. E('br'),
  33. E('br'),
  34. E('div', { 'class': 'description' }, _('Last 50 lines of syslog:')),
  35. E('br'),
  36. data[1] || _('No log data.')
  37. ]);
  38. dom.content(e, t);
  39. });
  40. };
  41. return view.extend({
  42. render: function() {
  43. var log_textarea = E('div', { 'id': 'log_textarea' },
  44. E('img', {
  45. 'src': L.resource(['icons/loading.gif']),
  46. 'alt': _('Loading'),
  47. 'style': 'vertical-align:middle'
  48. }, _('Collecting data...'))
  49. );
  50. poll.add(pollLog.bind(this, log_textarea));
  51. return E([
  52. E('style', [ css ]),
  53. E('div', {'class': 'cbi-map'}, [
  54. E('h2', {'name': 'content'}, '%s - %s'.format(_('Aria2'), _('Log Data'))),
  55. E('div', {'class': 'cbi-section'}, [
  56. log_textarea,
  57. E('div', {'style': 'text-align:right'},
  58. E('small', {}, _('Refresh every %s seconds.').format(L.env.pollinterval))
  59. )
  60. ])
  61. ])
  62. ]);
  63. },
  64. handleSave: null,
  65. handleSaveApply: null,
  66. handleReset: null
  67. });