MDL-16500 Removed the old Jabber class from the module and moved to a new one called...
[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>
25 * @copyright 2008 Nathanael C. Fritz
26 */
27
28/**
29 * XMPPHP Log
30 *
31 * @package XMPPHP
32 * @author Nathanael C. Fritz <JID: fritzy@netflint.net>
33 * @author Stephan Wentz <JID: stephan@jabber.wentz.it>
34 * @copyright 2008 Nathanael C. Fritz
35 * @version $Id$
36 */
37class XMPPHP_Log {
38
39 const LEVEL_ERROR = 0;
40 const LEVEL_WARNING = 1;
41 const LEVEL_INFO = 2;
42 const LEVEL_DEBUG = 3;
43 const LEVEL_VERBOSE = 4;
44
45 /**
46 * @var array
47 */
48 protected $data = array();
49
50 /**
51 * @var array
52 */
53 protected $names = array('ERROR', 'WARNING', 'INFO', 'DEBUG', 'VERBOSE');
54
55 /**
56 * @var integer
57 */
58 protected $runlevel;
59
60 /**
61 * @var boolean
62 */
63 protected $printout;
64
65 /**
66 * Constructor
67 *
68 * @param boolean $printout
69 * @param string $runlevel
70 */
71 public function __construct($printout = false, $runlevel = self::LEVEL_INFO) {
72 $this->printout = (boolean)$printout;
73 $this->runlevel = (int)$runlevel;
74 }
75
76 /**
77 * Add a message to the log data array
78 * If printout in this instance is set to true, directly output the message
79 *
80 * @param string $msg
81 * @param integer $runlevel
82 */
83 public function log($msg, $runlevel = self::LEVEL_INFO) {
84 $time = time();
85 $this->data[] = array($this->runlevel, $msg, $time);
86 if($this->printout and $runlevel <= $this->runlevel) {
87 $this->writeLine($msg, $runlevel, $time);
88 }
89 }
90
91 /**
92 * Output the complete log.
93 * Log will be cleared if $clear = true
94 *
95 * @param boolean $clear
96 * @param integer $runlevel
97 */
98 public function printout($clear = true, $runlevel = null) {
99 if($runlevel === null) {
100 $runlevel = $this->runlevel;
101 }
102 foreach($this->data as $data) {
103 if($runlevel <= $data[0]) {
104 $this->writeLine($data[1], $runlevel, $data[2]);
105 }
106 }
107 if($clear) {
108 $this->data = array();
109 }
110 }
111
112 protected function writeLine($msg, $runlevel, $time) {
113 //echo date('Y-m-d H:i:s', $time)." [".$this->names[$runlevel]."]: ".$msg."\n";
114 echo $time." [".$this->names[$runlevel]."]: ".$msg."\n";
115 }
116}