Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 39 and 311]

   1  <?php
   2  
   3  namespace 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  	  */
  32  
  33  /**
  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>
  41   * @copyright  2008 Nathanael C. Fritz
  42   * @version    $Id$
  43   */
  44  class Log {
  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();
  92  	 	 #$this->data[] = array($this->runlevel, $msg, $time);
  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";
 122  	 	 flush();
 123  	 }
 124  }