cvs: pear /Log_Parser Parser.php
| From: | Xavier Noguer Gallego | Date: | Wed, 24 Sep 2003 15:34:02 +0000 |
| Subject: | cvs: pear /Log_Parser Parser.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-13063@lists.php.net to get a copy of this message | ||
xnoguer Wed Sep 24 11:34:02 2003 EDT
Modified files:
/pear/Log_Parser Parser.php
Log:
committing for toby
Index: pear/Log_Parser/Parser.php
diff -u pear/Log_Parser/Parser.php:1.6 pear/Log_Parser/Parser.php:1.7
--- pear/Log_Parser/Parser.php:1.6 Thu Jul 17 10:26:34 2003
+++ pear/Log_Parser/Parser.php Wed Sep 24 11:34:01 2003
@@ -16,10 +16,12 @@
// | Authors: |
// +----------------------------------------------------------------------+
//
-// $Id: Parser.php,v 1.6 2003/07/17 14:26:34 xnoguer Exp $
+// $Id: Parser.php,v 1.7 2003/09/24 15:34:01 xnoguer Exp $
require_once('PEAR.php');
-require_once('Parser/Config.php'); // change this to proper dir structure
+require_once('Log_Parser/Parser/Config.php'); // change this to proper dir structure
+require_once('Log_Parser/Parser/Filter_Engine.php'); // change this to proper dir
structure
+
/**
* This class allows parsing log files
@@ -32,12 +34,14 @@
* The current line number inside the log
* @var integer
*/
+
var $_line_number;
/**
* File name for the log
* @var string
*/
+
var $_filename;
/**
@@ -45,27 +49,38 @@
*
* @var array
*/
+
var $regex;
/**
* The (string) indexes for tokens
* @var array
*/
+
var $_indexes;
/**
* The array of parsed lines indexed by token name
* @var array
*/
+
var $parsed_lines;
/**
* The total amount of lines parsed
* @var integer
*/
+
var $total_lines;
/**
+ * The filter engine to filter log lines
+ * @var object Log_Parser_Filter_Engine
+ */
+
+ var $_filterEngine;
+
+ /**
* The constructor
*
* @param string $filename The log file name
@@ -75,6 +90,7 @@
function Log_Parser($filename, &$config)
{
+
// check if $config is a Log_Parser_Config object (or an array maybe...)
if (is_object($config) and (get_class($config) == 'log_parser_config'))
{
@@ -82,6 +98,7 @@
$config_array = $config->getConfigArray();
// beginning and end for perl-compatible regex patterns
+
$prepend = '/^';
$append = '$/';
@@ -89,9 +106,9 @@
foreach ($config_array as $lineName => $lineDef) {
// workout complete regex foreach line
$this->regex[$lineName] = $prepend;
- for ($i = 1; $i <= count($lineDef); $i++) {
- $this->_indexes[$lineName][$i] =
$config_array[$lineName][$i]['name'];
- $this->regex[$lineName] .=
$config_array[$lineName][$i]['pattern'];
+ for ($i = 1; $i <= (count($lineDef)); $i++) {
+ @$this->_indexes[$lineName][$i] =
@$config_array[$lineName][$i]['name'];
+ @$this->regex[$lineName] .=
@$config_array[$lineName][$i]['pattern'];
}
$this->regex[$lineName] = trim($this->regex[$lineName]).$append;
}
@@ -113,9 +130,11 @@
* @param string $line The line to parse
* @return mixed Array containing the tokens parsed from the line, or false on failure
*/
+
function _parseLine($line, &$res)
{
$line = trim($line);
+ $got_line = false;
// parses now for all regex (multiline-suport)
foreach ($this->regex as $linename => $regex) {
if (preg_match($regex, $line, $res)) {
@@ -124,9 +143,14 @@
break;
}
}
- if (isset($got_line)) {
+ if ($got_line) {
for ($i = 1; $i <= count($this->_indexes[$got_line]); $i++) {
- $tmp_line[$this->_indexes[$got_line][$i]] = $res[$i];
+ $tmp_line[$this->_indexes[$got_line][$i]] = $res[$i];
+ }
+ if (isset($this->_filter)) {
+ if (!$this->_filter->filter($linename, $tmp_line)) {
+ return null;
+ }
}
return $tmp_line;
}
@@ -142,26 +166,32 @@
* @access public
* @return mixed true on success, PEAR_Error on failure.
*/
+
function parse($start = null, $end = null)
{
- $lines = file($this->_filename);
- $total_lines = count($lines);
- if ($lines === false) {
- $this->raiseError("Error reading file {$this->_filename}");
- }
+ $lines = array();
// range checking
if (!isset($start)) {
$start = 0;
}
+ if ((isset($end)) and ($end < $start)) {
+ $this->raiseError("Invalid range given: $end < $start");
+ }
if (!isset($end)) {
- $end = $total_lines;
+ $end = -1;
}
- if ($end > $total_lines) {
- $end = $total_lines; // silently correct ending line
+ $fd = @fopen($this->_filename, "r");
+ if ($fd === false) {
+ $this->raiseError("Error reading file {$this->_filename}");
}
- if ($end < $start) {
- $this->raiseError("Invalid range given: $end < $start");
+ for ($i = 0; !feof($fd); $i++) {
+ $lines[] = fgets($fd);
+ if ($i == $end) {
+ break;
+ }
}
+ fclose($fd);
+ $end = $i; // assign the real end
$this->total_lines = $end - $start + 1;
// check if it actually matches the config
@@ -181,6 +211,7 @@
*
* @access public
*/
+
function currentLine()
{
return $this->parsed_lines[$this->_line_number];
@@ -193,6 +224,7 @@
* @access public
* @return mixed array of tokens or false if there are no more lines left
*/
+
function nextLine()
{
if ($this->_line_number >= $this->total_lines) {
@@ -208,6 +240,7 @@
* @param integer $line_number
* @access public
*/
+
function jumpLine($line_number)
{
$this->_line_number = $line_number;
@@ -218,6 +251,7 @@
*
* @access public
*/
+
function whichLine()
{
return $this->_line_number;
@@ -228,10 +262,30 @@
*
* @access public
*/
+
function resetLog()
{
$this->_line_number = 0;
}
+ /**
+ * Add a filter to your Log_Parser
+ * This adds a filter to your Log_Parser object. The method expects a valid
+ * object of a class inheriting Log_Parser_Filter_Static or Log_Parser_Filter_Dynamic.
+ *
+ * @access public
+ * @param object An object as described above
+ * @return object PEAR::Error on error, otherwise true
+ */
+
+ function addFilter ( &$filterObject ) {
+ $this->_filter =& new Log_Parser_Filter_Engine($filterObject);
+ if ($this->_filter->hasError()) {
+ $error =& $this->_fileter->getError();
+ $this->_filter = null;
+ return $error;
+ }
+ return true;
+ }
}
?>