Jump Start - Simple log viewer

Possibly you have a simple logfile to made visible like this:

logfile

 
>> Machine this and that#
>> Version 0.14#
>> Ready#
<< Start#
>> OK#
viewlog: /home/www/fcon/lib/plugins/xsviewlog/testfiles/tst.log
0 Machine this and that#  
1 Version 0.14#  
2 Ready#  
3 Start#  
4 OK# 

The plugin creates a table with own styling defined by css-file based on the given classes at php-code for table components.

  • The table header contains the displayed log-file, given by syntax parameter.
  • First column output is just a line counter you may use as reference for related explanations.
  • Let us assume the log handles send and receive messages, which are differed by leading ”»” and ”«” characters per line. php-Code lines 94 and 100 test for such leading characters to handle the output accordingly. Then following lines will add the line number, related picture into second column and line content, coloured according the css definition for send/receive, into 3rd column.

The php-Code

The plugin does contain additionally css, images and a test file.

syntax.php
*/ if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/'); if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/'); require_once(DOKU_PLUGIN.'syntax.php'); /** * All DokuWiki plugins to extend the parser/rendering mechanism * need to inherit from this class */ class syntax_plugin_xsviewlog extends DokuWiki_Syntax_Plugin { /******************************************************************************/ /* return some info */ function getInfo(){ return confToHash(dirname(__FILE__).'/plugin.info.txt'); } function getType(){ return 'substition';} function getPType(){ return 'block';} function getSort(){ return 999;} /******************************************************************************/ /* Connect pattern to lexer */ function connectTo($mode){ $this->Lexer->addSpecialPattern('\{\(xsviewlog\>.*?\)\}',$mode,'plugin_xsviewlog'); } /******************************************************************************/ /* handle the match */ function handle($match, $state, $pos, &$handler) { global $ID; $match = substr($match,strlen('{(xsviewlog>'),-2); //strip markup from start and end //handle params $data = array(); /******************************************************************************/ /* parameters can be: {(xsviewlog>[file path],[from line],[to line],[type])} [file path] ... path to the file (either it is in DokuWiki media directory or windows fileshare, etc.) [from line] ... the first line, which should be displayed [to line] ... the last line, which should be displayed [type] ... the type of content to chose appropriate description ini file /******************************************************************************/ $params = explode(",",$match); // if you will have more parameters and choose ',' to delim them if ((!$params) || (count($params)<3)) { msg('Syntax of xsviewlog detected but to less parameters attached.', -1); } else { // Values $xsviewlog = array(); $xsviewlog['filepath'] = $params[0]; $xsviewlog['from'] = $params[1]; $xsviewlog['until'] = $params[2]; $xsviewlog['type'] = $params[3]; return $xsviewlog; } } /******************************************************************************/ /* render output * @author Taggic */ function render($mode, &$renderer, $xsviewlog) { global $ID; if($xsviewlog['filepath'] === '') $xsviewlog['filepath'] = DOKU_PLUGIN.'xsviewlog/testfiles/tst.log'; if(!$xsviewlog['type']) $xsviewlog['type'] = 'txt'; // 1. check if $xsviewlog['filepath'] exist, else error message if(!file_exists($xsviewlog['filepath'])) { msg('file '.$xsviewlog['filepath'].' not found',-1); return false; } // 2. retrieve file content $records = file($xsviewlog['filepath']); // 3. parse file content if(!$xsviewlog['until']) $xsviewlog['until']=count($records); foreach ($records as $line_num => $line) { if(($line_num>=$xsviewlog['from']-1) && ($line_num<=$xsviewlog['until']-1)) { // check if left 3 sign are matching '>> ' or '<< ' to differ between send and receive if(strpos($line, '>>')===0) { $log_block .= ''.$line_num.' '.substr($line,strlen(">>"),strlen($line)).'  '.NL; } elseif(strpos($line, '<<')===0) { $log_block .= ''.$line_num.' '.substr($line,strlen("<<"),strlen($line)).'  '.NL; } else { $log_block .= ''.$line_num.'   '.$line.'  '.NL; } } if ($line_num>$xsviewlog['until']) break; } // 4. append output to renderer $renderer->doc .= '
'. ''. $log_block.'
viewlog: '.$xsviewlog['filepath'].'
'; } }
  1. <?php
  2.  
  3. /**
  4.  * Plugin xsviewlog: simple plugin to view log files
  5.  *
  6.  * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
  7.  * @author     Taggic <taggic@t-online.de>
  8.  */
  9.  
  10. if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
  11. if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
  12. require_once(DOKU_PLUGIN.'syntax.php');  
  13.  
  14. /**
  15.  * All DokuWiki plugins to extend the parser/rendering mechanism
  16.  * need to inherit from this class
  17.  */
  18. class syntax_plugin_xsviewlog extends DokuWiki_Syntax_Plugin {
  19.  
  20. /******************************************************************************/
  21. /* return some info
  22. */
  23.     function getInfo(){
  24.         return confToHash(dirname(__FILE__).'/plugin.info.txt');
  25.     }
  26.  
  27.     function getType(){ return 'substition';}
  28.     function getPType(){ return 'block';}
  29.     function getSort(){ return 999;}
  30.    
  31. /******************************************************************************/
  32. /* Connect pattern to lexer
  33. */  
  34.     function connectTo($mode){
  35.         $this->Lexer->addSpecialPattern('\{\(xsviewlog\>.*?\)\}',$mode,'plugin_xsviewlog');
  36.     }                                  
  37.  
  38. /******************************************************************************/
  39. /* handle the match
  40. */  
  41.     function handle($match, $state, $pos, &$handler) {
  42.         global $ID;
  43.         $match = substr($match,strlen('{(xsviewlog>'),-2); //strip markup from start and end
  44.  
  45.         //handle params
  46.         $data = array();
  47.     /******************************************************************************/
  48.     /*      parameters can be:
  49.             {(xsviewlog>[file path],[from line],[to line],[type])}
  50.             [file path]   ... path to the file (either it is in DokuWiki media directory or windows fileshare, etc.)
  51.             [from line]   ... the first line, which should be displayed
  52.             [to line]     ... the last line, which should be displayed
  53.             [type]        ... the type of content to chose appropriate description ini file
  54.     /******************************************************************************/
  55.  
  56.         $params = explode(",",$match);  // if you will have more parameters and choose ',' to delim them
  57.        
  58.        
  59.         if ((!$params) || (count($params)<3)) {
  60.           msg('Syntax of xsviewlog detected but to less parameters attached.', -1);          
  61.         }
  62.         else {
  63.           // Values
  64.           $xsviewlog                = array();
  65.           $xsviewlog['filepath']    = $params[0];
  66.           $xsviewlog['from']        = $params[1];
  67.           $xsviewlog['until']       = $params[2];
  68.           $xsviewlog['type']        = $params[3];
  69.           return $xsviewlog;
  70.         }        
  71.      }
  72. /******************************************************************************/
  73. /* render output
  74. * @author Taggic <taggic@t-online.de>
  75. */  
  76.     function render($mode, &$renderer, $xsviewlog) {
  77.         global $ID;
  78.         if($xsviewlog['filepath'] === '') $xsviewlog['filepath'] = DOKU_PLUGIN.'xsviewlog/testfiles/tst.log';
  79.         if(!$xsviewlog['type'])     $xsviewlog['type'] = 'txt';
  80.  
  81.     // 1. check if $xsviewlog['filepath'] exist, else error message
  82.         if(!file_exists($xsviewlog['filepath'])) {
  83.           msg('file '.$xsviewlog['filepath'].' not found',-1);
  84.           return false;
  85.         }
  86.    
  87.     // 2. retrieve file content
  88.         $records    = file($xsviewlog['filepath']);
  89.  
  90.     // 3. parse file content
  91.         if(!$xsviewlog['until']) $xsviewlog['until']=count($records);
  92.         foreach ($records as $line_num => $line) {
  93.             if(($line_num>=$xsviewlog['from']-1) && ($line_num<=$xsviewlog['until']-1)) {
  94.                 // check if left 3 sign are matching '>> ' or '<< ' to differ between send and receive
  95.                 if(strpos($line, '>>')===0) {
  96.                     $log_block .= '<tr><td class="xsviewlog_col1" align="right">'.$line_num.'</td>
  97.                                       <td class="xsviewlog_col2"><img src="'.DOKU_BASE.'lib/plugins/xsviewlog/images/in.png" title="receive" /></td>
  98.                                       <td><span class="xsviewlog0">'.substr($line,strlen(">>"),strlen($line)).'&nbsp;</span></td>
  99.                                   </tr>'.NL;
  100.                 }
  101.                 elseif(strpos($line, '<<')===0) {
  102.                     $log_block .= '<tr><td class="xsviewlog_col1" align="right">'.$line_num.'</td>
  103.                                       <td class="xsviewlog_col2"><img src="'.DOKU_BASE.'lib/plugins/xsviewlog/images/out.png" title="send" /></td>
  104.                                       <td><span class="xsviewlog1">'.substr($line,strlen("<<"),strlen($line)).'&nbsp;</span></td>
  105.                                   </tr>'.NL;
  106.                 }
  107.                 else {
  108.                   $log_block .= '<tr><td class="xsviewlog_col1" align="right">'.$line_num.'</td>
  109.                                     <td class="xsviewlog_col2">&nbsp;</td>
  110.                                     <td><span class="xsviewlog2">'.$line.'&nbsp;</span></td>
  111.                                 </tr>'.NL;
  112.                 }
  113.             }
  114.             if ($line_num>$xsviewlog['until']) break;
  115.         }
  116.  
  117.     // 4. append output to renderer
  118.        $renderer->doc .= '<div class="xsviewlog"><table class="xsviewlog">'.
  119.                          '<tr><th><span class="viewlog_txt">viewlog:&nbsp;</span></th><th colspan="2">'.$xsviewlog['filepath'].'</th></tr>'.
  120.                           $log_block.'</table></div>';
  121.     }
  122.   }
  123.  

The Syntax

{(xsviewlog>[logfile path], [from line], [until line], [type])}

Remark

The type parameter currently not used within the plugin.




The Output

viewlog: /home/www/fcon/lib/plugins/xsviewlog/testfiles/tst.log
0 Machine this and that#  
1 Version 0.14#  
2 Ready#  
3 Start#  
4 OK# 

Download the Plugin

To get the complete plugin please download: xsviewlog.zip





see also

tips/log_viewer.txt · Last modified: 2014/04/30 09:36 (external edit)

This Wiki is hosted and ruled by Policies of Frister Consultancy Services.
By using this wiki you accept these rules. -> Impressum