Menu="UNRAID-OS"
Title="System Log"
Icon="icon-log"
Tag="list"
---
<?PHP
/* Copyright 2005-2022, Lime Technology
 * Copyright 2012-2022, Bergware International.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License version 2,
 * as published by the Free Software Foundation.
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 */
?>
<?
$zip    = htmlspecialchars(str_replace(' ','_',strtolower($var['NAME'])));
$log    = '/var/log/syslog';
$cfg    = '/boot/config/rsyslog.cfg';
$max    = 5000;
$select = [];
if (file_exists($cfg)) {
  $syslog = parse_ini_file($cfg);
  if ($syslog['local_server'] && $logs = glob($syslog['server_folder'].'/syslog-*.log',GLOB_NOSORT)) {
    natsort($logs);
    $select[] = "<select onchange='showLog(this.value)'>";
    $select[] = mk_option(1,$log,'syslog');
    foreach ($logs as $file) $select[] = mk_option(1,$file,basename($file));
    $select[] = "</select>";
  }
}
$select = implode($select);
?>
<style>
input#max{border:none;width:60px;margin:0;padding:0}
</style>
<script>
var logfile = "<?=$log?>";

function zipfile(){
  var d = new Date();
  return "<?=$zip?>-"+logfile.split('/').reverse()[0].replace('.log','')+'-'+d.toISOString().substr(0,16).replace(/[-:]/g,'').replace('T','-')+'.zip';
}
function cleanUp(file) {
  if (document.hasFocus()) {
    $('input#download').val("_(Download)_").prop('disabled',false);
    $.post('/webGui/include/Download.php',{cmd:'delete',file:file});
  } else {
    setTimeout(function(){cleanUp(file);},2000);
  }
}
function syslog(file) {
  $('input#download').val("_(Downloading)_...").prop('disabled',true);
  $.post('/webGui/include/Download.php',{cmd:'save',source:logfile,file:file},function(zip) {
    location = zip;
    setTimeout(function(){cleanUp(file);},4000);
  });
}
function highlight(checked,line) {
  var o = checked ? '-' : '';
  var n = ($('span.text').css('display')=='none' && !checked) ? 'none' : '';
  switch (line) {
    case 'E': $('span.'+o+'error').css('display',n); $('span.error'+o).toggleClass('error -error error-'); break;
    case 'W': $('span.'+o+'warn').css('display',n); $('span.warn'+o).toggleClass('warn -warn warn-'); break;
    case 'S': $('span.'+o+'system').css('display',n); $('span.system'+o).toggleClass('system -system system-'); break;
    case 'A': $('span.'+o+'array').css('display',n); $('span.array'+o).toggleClass('array -array array-'); break;
    case 'L': $('span.'+o+'login').css('display',n); $('span.login'+o).toggleClass('login -login login-'); break;
    case 'N': $('span.text,span[class^="-"]').css('display',checked ? 'none':''); break;
  }
  $('span.label').show();
}
function toggle(checked) {
  highlight(checked,'E');
  highlight(checked,'W');
  highlight(checked,'S');
  highlight(checked,'A');
  highlight(checked,'L');
  $('span.label input[type=checkbox]').not('.ctrl').prop('checked',checked);
}
function resize() {
  $('pre.up').height(Math.max(window.innerHeight-320,330));
}
function showLog(log,init) {
  logfile = log;
  $('span.label input[type=checkbox]').prop('checked',true);
  $('span.label').each(function(){
    var type = $(this).attr('class').replace('label','').replace(/-/g,'');
    $(this).removeClass().addClass(type+' label');
  });
  timers.syslog = setTimeout(function(){$('div.spinner.fixed').show('slow');},500);
  $.post('/webGui/include/Syslog.php',{log:log,max:$('#max').val()||<?=$max?>},function(data){
    clearTimeout(timers.syslog);
    $('pre.up').html(data);
<?if ($display['resize']):?>
    resize();
    if (init) $(window).bind('resize',function(){resize();});
<?endif;?>
    $('div.spinner.fixed').hide('slow');
  });
}
$(function() {
  $('input#max').on('keydown',function(e) {
    if (e.keyCode === 13) {
      e.preventDefault();
      e.stopImmediatePropagation();
      showLog(logfile);
    }
  });
  showLog(logfile,true);
});
$('.tabs').append("<span class='status'><span class='lite label'>_(Log size)_:&nbsp;&nbsp;<input type='number' id='max' value='' placeholder='<?=$max?>'></span><?=$select?><span class='lite label'><label>_(Text)_<input type='checkbox' class='ctrl' onclick='highlight(!this.checked,\"N\")' checked></label></span><span class='error label'><label>_(Error)_<input type='checkbox' onclick='highlight(this.checked,\"E\")' checked></label></span><span class='warn label'><label>_(Warning)_<input type='checkbox' onclick='highlight(this.checked,\"W\")' checked></label></span><span class='system label'><label>_(System)_<input type='checkbox' onclick='highlight(this.checked,\"S\")' checked></label></span><span class='array label'><label>_(Array)_<input type='checkbox' onclick='highlight(this.checked,\"A\")' checked></label></span><span class='login label'><label>_(Login)_<input type='checkbox' onclick='highlight(this.checked,\"L\")' checked></label></span><span class='lite label'><input type='checkbox' class='ctrl' onclick='toggle(this.checked)' checked></span></span>");
</script>
<pre class='up'></pre>
<input type="button" id="download" value="_(Download)_" onclick="syslog(zipfile())"><input type="button" value="_(Refresh)_" onclick="showLog(logfile)"><input type="button" value="_(Done)_" onclick="done()">
