on-demand release 3.10dev+
[moodle.git] / lib / jabber / XMPP / Log.php
CommitLineData
838a8eb1 1<?php
2/**
3 * XMPPHP: The PHP XMPP Library
4 * Copyright (C) 2008 Nathanael C. Fritz
5 * This file is part of SleekXMPP.
6 *
7 * XMPPHP is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * XMPPHP is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with XMPPHP; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 *
21 * @category xmpphp
22 * @package XMPPHP
23 * @author Nathanael C. Fritz <JID: fritzy@netflint.net>
24 * @author Stephan Wentz <JID: stephan@jabber.wentz.it>
d804f9e2 25 * @author Michael Garvin <JID: gar@netflint.net>
838a8eb1 26 * @copyright 2008 Nathanael C. Fritz
27 */
28
29/**
30 * XMPPHP Log
31 *
32 * @package XMPPHP
33 * @author Nathanael C. Fritz <JID: fritzy@netflint.net>
34 * @author Stephan Wentz <JID: stephan@jabber.wentz.it>
d804f9e2 35 * @author Michael Garvin <JID: gar@netflint.net>
838a8eb1 36 * @copyright 2008 Nathanael C. Fritz
37 * @version $Id$
38 */
39class XMPPHP_Log {
40
41 const LEVEL_ERROR = 0;
42 const LEVEL_WARNING = 1;
43 const LEVEL_INFO = 2;
44 const LEVEL_DEBUG = 3;
45 const LEVEL_VERBOSE = 4;
46
47 /**
48 * @var array
49 */
50 protected $data = array();
51
52 /**
53 * @var array
54 */
55 protected $names = array('ERROR', 'WARNING', 'INFO', 'DEBUG', 'VERBOSE');
56
57 /**
58 * @var integer
59 */
60 protected $runlevel;
61
62 /**
63 * @var boolean
64 */
65 protected $printout;
66
67 /**
68 * Constructor
69 *
70 * @param boolean $printout
71 * @param string $runlevel
72 */
73 public function __construct($printout = false, $runlevel = self::LEVEL_INFO) {
74 $this->printout = (boolean)$printout;
75 $this->runlevel = (int)$runlevel;
76 }
77
78 /**
79 * Add a message to the log data array
80 * If printout in this instance is set to true, directly output the message
81 *
82 * @param string $msg
83 * @param integer $runlevel
84 */
85 public function log($msg, $runlevel = self::LEVEL_INFO) {
86 $time = time();
d804f9e2 87 #$this->data[] = array($this->runlevel, $msg, $time);
838a8eb1 88 if($this->printout and $runlevel <= $this->runlevel) {
89 $this->writeLine($msg, $runlevel, $time);
90 }
91 }
92
93 /**
94 * Output the complete log.
95 * Log will be cleared if $clear = true
96 *
97 * @param boolean $clear
98 * @param integer $runlevel
99 */
100 public function printout($clear = true, $runlevel = null) {
101 if($runlevel === null) {
102 $runlevel = $this->runlevel;
103 }
104 foreach($this->data as $data) {
105 if($runlevel <= $data[0]) {
106 $this->writeLine($data[1], $runlevel, $data[2]);
107 }
108 }
109 if($clear) {
110 $this->data = array();
111 }
112 }
113
114 protected function writeLine($msg, $runlevel, $time) {
115 //echo date('Y-m-d H:i:s', $time)." [".$this->names[$runlevel]."]: ".$msg."\n";
116 echo $time." [".$this->names[$runlevel]."]: ".$msg."\n";
d804f9e2 117 flush();
838a8eb1 118 }
119}