2008-08-22 14:17:14 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* XMPPHP: The PHP XMPP Library
|
|
|
|
* Copyright (C) 2008 Nathanael C. Fritz
|
|
|
|
* This file is part of SleekXMPP.
|
2019-04-21 01:23:50 +01:00
|
|
|
*
|
2008-08-22 14:17:14 +01:00
|
|
|
* 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.
|
2019-04-21 01:23:50 +01:00
|
|
|
*
|
2008-08-22 14:17:14 +01:00
|
|
|
* 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.
|
2019-04-21 01:23:50 +01:00
|
|
|
*
|
2008-08-22 14:17:14 +01:00
|
|
|
* 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
|
|
|
|
*
|
2019-04-21 01:23:50 +01:00
|
|
|
* @category xmpphp
|
|
|
|
* @package XMPPHP
|
|
|
|
* @author Nathanael C. Fritz <JID: fritzy@netflint.net>
|
|
|
|
* @author Stephan Wentz <JID: stephan@jabber.wentz.it>
|
|
|
|
* @author Michael Garvin <JID: gar@netflint.net>
|
|
|
|
* @author Alexander Birkner (https://github.com/BirknerAlex)
|
|
|
|
* @author zorn-v (https://github.com/zorn-v/xmpphp/)
|
|
|
|
* @author GNU social
|
2008-08-22 14:17:14 +01:00
|
|
|
* @copyright 2008 Nathanael C. Fritz
|
|
|
|
*/
|
|
|
|
|
2019-04-21 01:23:50 +01:00
|
|
|
namespace XMPPHP;
|
|
|
|
|
2008-08-22 14:17:14 +01:00
|
|
|
/**
|
|
|
|
* XMPPHP Log
|
2019-04-21 01:23:50 +01:00
|
|
|
*
|
|
|
|
* @package XMPPHP
|
|
|
|
* @author Nathanael C. Fritz <JID: fritzy@netflint.net>
|
|
|
|
* @author Stephan Wentz <JID: stephan@jabber.wentz.it>
|
|
|
|
* @author Michael Garvin <JID: gar@netflint.net>
|
|
|
|
* @copyright 2008 Nathanael C. Fritz
|
|
|
|
* @version $Id$
|
2008-08-22 14:17:14 +01:00
|
|
|
*/
|
2019-04-21 01:23:50 +01:00
|
|
|
class Log
|
|
|
|
{
|
|
|
|
const LEVEL_ERROR = 0;
|
|
|
|
const LEVEL_WARNING = 1;
|
|
|
|
const LEVEL_INFO = 2;
|
|
|
|
const LEVEL_DEBUG = 3;
|
|
|
|
const LEVEL_VERBOSE = 4;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2019-11-16 16:51:01 +00:00
|
|
|
protected $data = [];
|
2019-04-21 01:23:50 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array
|
|
|
|
*/
|
2019-11-16 16:51:01 +00:00
|
|
|
protected $names = ['ERROR', 'WARNING', 'INFO', 'DEBUG', 'VERBOSE'];
|
2008-08-22 14:17:14 +01:00
|
|
|
|
2019-04-21 01:23:50 +01:00
|
|
|
/**
|
2019-11-16 16:51:01 +00:00
|
|
|
* @var int
|
2019-04-21 01:23:50 +01:00
|
|
|
*/
|
|
|
|
protected $runlevel;
|
2008-08-22 14:17:14 +01:00
|
|
|
|
2019-04-21 01:23:50 +01:00
|
|
|
/**
|
2019-11-16 16:51:01 +00:00
|
|
|
* @var bool
|
2019-04-21 01:23:50 +01:00
|
|
|
*/
|
|
|
|
protected $printout;
|
2008-08-22 14:17:14 +01:00
|
|
|
|
2019-04-21 01:23:50 +01:00
|
|
|
/**
|
|
|
|
* Constructor
|
|
|
|
*
|
|
|
|
* @param boolean $printout
|
2019-11-16 16:51:01 +00:00
|
|
|
* @param int $runlevel (optional)
|
2019-04-21 01:23:50 +01:00
|
|
|
*/
|
2019-11-16 16:51:01 +00:00
|
|
|
public function __construct($printout = false, ?int $runlevel = self::LEVEL_INFO)
|
2019-04-21 01:23:50 +01:00
|
|
|
{
|
2019-11-16 16:51:01 +00:00
|
|
|
$this->printout = (bool) $printout;
|
|
|
|
$this->runlevel = (int) ($runlevel ?? 0);
|
2019-04-21 01:23:50 +01:00
|
|
|
}
|
2008-08-22 14:17:14 +01:00
|
|
|
|
2019-04-21 01:23:50 +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
|
2019-11-16 16:51:01 +00:00
|
|
|
* @param int $runlevel (optional)
|
2019-04-21 01:23:50 +01:00
|
|
|
*/
|
2019-11-16 16:51:01 +00:00
|
|
|
public function log($msg, $runlevel = self::LEVEL_INFO): void
|
2019-04-21 01:23:50 +01:00
|
|
|
{
|
|
|
|
$time = time();
|
2019-11-16 16:51:01 +00:00
|
|
|
//$this->data[] = [$this->runlevel, $msg, $time];
|
2019-04-21 01:23:50 +01:00
|
|
|
if ($this->printout and $runlevel <= $this->runlevel) {
|
|
|
|
$this->writeLine($msg, $runlevel, $time);
|
|
|
|
}
|
|
|
|
}
|
2008-08-22 14:17:14 +01:00
|
|
|
|
2019-11-16 16:51:01 +00:00
|
|
|
/**
|
|
|
|
* @param string $msg
|
|
|
|
* @param int $runlevel
|
|
|
|
* @param int $time
|
|
|
|
*/
|
|
|
|
protected function writeLine(string $msg, int $runlevel, int $time): void
|
2019-04-21 01:23:50 +01:00
|
|
|
{
|
|
|
|
//echo date('Y-m-d H:i:s', $time)." [".$this->names[$runlevel]."]: ".$msg."\n";
|
|
|
|
echo $time . " [" . $this->names[$runlevel] . "]: " . $msg . "\n";
|
|
|
|
flush();
|
|
|
|
}
|
2008-08-22 14:17:14 +01:00
|
|
|
|
2019-04-21 01:23:50 +01:00
|
|
|
/**
|
|
|
|
* Output the complete log.
|
|
|
|
* Log will be cleared if $clear = true
|
|
|
|
*
|
2019-11-16 16:51:01 +00:00
|
|
|
* @param bool $clear
|
|
|
|
* @param int $runlevel
|
2019-04-21 01:23:50 +01:00
|
|
|
*/
|
2019-11-16 16:51:01 +00:00
|
|
|
public function printout(bool $clear = true, int $runlevel = null): void
|
2019-04-21 01:23:50 +01:00
|
|
|
{
|
|
|
|
if ($runlevel === null) {
|
|
|
|
$runlevel = $this->runlevel;
|
|
|
|
}
|
|
|
|
foreach ($this->data as $data) {
|
|
|
|
if ($runlevel <= $data[0]) {
|
|
|
|
$this->writeLine($data[1], $runlevel, $data[2]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ($clear) {
|
2019-11-16 16:51:01 +00:00
|
|
|
$this->data = [];
|
2019-04-21 01:23:50 +01:00
|
|
|
}
|
|
|
|
}
|
2008-08-22 14:17:14 +01:00
|
|
|
}
|