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