2008-05-01 15:29:33 +01:00
|
|
|
<?php
|
2008-05-05 20:50:02 +01:00
|
|
|
/**
|
|
|
|
* XMPPHP: The PHP XMPP Library
|
|
|
|
* Copyright (C) 2008 Nathanael C. Fritz
|
|
|
|
* This file is part of SleekXMPP.
|
|
|
|
*
|
|
|
|
* XMPPHP is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* XMPPHP is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with XMPPHP; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*
|
2008-05-05 21:05:23 +01:00
|
|
|
* @category xmpphp
|
2008-07-07 03:33:57 +01:00
|
|
|
* @package XMPPHP
|
|
|
|
* @author Nathanael C. Fritz <JID: fritzy@netflint.net>
|
|
|
|
* @author Stephan Wentz <JID: stephan@jabber.wentz.it>
|
2008-05-05 21:08:45 +01:00
|
|
|
* @copyright 2008 Nathanael C. Fritz
|
2008-05-05 20:50:02 +01:00
|
|
|
*/
|
2008-05-01 15:29:33 +01:00
|
|
|
|
2008-05-05 20:50:02 +01:00
|
|
|
/**
|
|
|
|
* XMPPHP Log
|
|
|
|
*
|
2008-07-07 03:33:57 +01:00
|
|
|
* @package XMPPHP
|
|
|
|
* @author Nathanael C. Fritz <JID: fritzy@netflint.net>
|
|
|
|
* @author Stephan Wentz <JID: stephan@jabber.wentz.it>
|
2008-05-05 21:08:45 +01:00
|
|
|
* @copyright 2008 Nathanael C. Fritz
|
2008-07-07 03:33:57 +01:00
|
|
|
* @version $Id$
|
2008-05-05 20:50:02 +01:00
|
|
|
*/
|
2008-05-01 15:29:33 +01:00
|
|
|
class XMPPHP_Log {
|
2008-07-07 03:33:57 +01:00
|
|
|
|
|
|
|
const LEVEL_ERROR = 0;
|
|
|
|
const LEVEL_WARNING = 1;
|
|
|
|
const LEVEL_INFO = 2;
|
|
|
|
const LEVEL_DEBUG = 3;
|
|
|
|
const LEVEL_VERBOSE = 4;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $data = array();
|
2008-05-01 15:29:33 +01:00
|
|
|
|
2008-07-07 03:33:57 +01:00
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $names = array('ERROR', 'WARNING', 'INFO', 'DEBUG', 'VERBOSE');
|
2008-05-01 15:29:33 +01:00
|
|
|
|
2008-07-07 03:33:57 +01:00
|
|
|
/**
|
|
|
|
* @var integer
|
|
|
|
*/
|
|
|
|
protected $runlevel;
|
2008-05-01 15:29:33 +01:00
|
|
|
|
2008-07-07 03:33:57 +01:00
|
|
|
/**
|
|
|
|
* @var boolean
|
|
|
|
*/
|
|
|
|
protected $printout;
|
2008-05-01 15:29:33 +01:00
|
|
|
|
2008-07-07 03:33:57 +01:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* @param boolean $printout
|
|
|
|
* @param string $runlevel
|
|
|
|
*/
|
|
|
|
public function __construct($printout = false, $runlevel = self::LEVEL_INFO) {
|
|
|
|
$this->printout = (boolean)$printout;
|
|
|
|
$this->runlevel = (int)$runlevel;
|
|
|
|
}
|
2008-05-01 15:29:33 +01:00
|
|
|
|
2008-07-07 03:33:57 +01:00
|
|
|
/**
|
|
|
|
* Add a message to the log data array
|
|
|
|
* If printout in this instance is set to true, directly output the message
|
|
|
|
*
|
|
|
|
* @param string $msg
|
|
|
|
* @param integer $runlevel
|
|
|
|
*/
|
|
|
|
public function log($msg, $runlevel = self::LEVEL_INFO) {
|
|
|
|
$time = time();
|
|
|
|
$this->data[] = array($this->runlevel, $msg, $time);
|
|
|
|
if($this->printout and $runlevel <= $this->runlevel) {
|
|
|
|
$this->writeLine($msg, $runlevel, $time);
|
|
|
|
}
|
|
|
|
}
|
2008-05-01 15:29:33 +01:00
|
|
|
|
2008-07-07 03:33:57 +01:00
|
|
|
/**
|
|
|
|
* Output the complete log.
|
|
|
|
* Log will be cleared if $clear = true
|
|
|
|
*
|
|
|
|
* @param boolean $clear
|
|
|
|
* @param integer $runlevel
|
|
|
|
*/
|
|
|
|
public function printout($clear = true, $runlevel = null) {
|
|
|
|
if($runlevel === null) {
|
|
|
|
$runlevel = $this->runlevel;
|
|
|
|
}
|
|
|
|
foreach($this->data as $data) {
|
|
|
|
if($runlevel <= $data[0]) {
|
|
|
|
$this->writeLine($data[1], $runlevel, $data[2]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if($clear) {
|
|
|
|
$this->data = array();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function writeLine($msg, $runlevel, $time) {
|
|
|
|
//echo date('Y-m-d H:i:s', $time)." [".$this->names[$runlevel]."]: ".$msg."\n";
|
|
|
|
echo $time." [".$this->names[$runlevel]."]: ".$msg."\n";
|
2008-09-19 13:03:29 +01:00
|
|
|
flush();
|
2008-07-07 03:33:57 +01:00
|
|
|
}
|
2008-05-01 15:29:33 +01:00
|
|
|
}
|