Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.
   1  <?php
   2  /**
   3   * Copyright 2012-2017 Horde LLC (http://www.horde.org/)
   4   *
   5   * See the enclosed file LICENSE for license information (LGPL). If you
   6   * did not receive this file, see http://www.horde.org/licenses/lgpl21.
   7   *
   8   * @category  Horde
   9   * @copyright 2012-2017 Horde LLC
  10   * @license   http://www.horde.org/licenses/lgpl21 LGPL 2.1
  11   * @package   Imap_Client
  12   */
  13  
  14  /**
  15   * An object allowing management of debugging output within a
  16   * Horde_Imap_Client_Base object.
  17   *
  18   * NOTE: This class is NOT intended to be accessed outside of a Base object.
  19   * There is NO guarantees that the API of this class will not change across
  20   * versions.
  21   *
  22   * @author    Michael Slusarz <slusarz@horde.org>
  23   * @category  Horde
  24   * @copyright 2012-2017 Horde LLC
  25   * @internal
  26   * @license   http://www.horde.org/licenses/lgpl21 LGPL 2.1
  27   * @package   Imap_Client
  28   */
  29  class Horde_Imap_Client_Base_Debug
  30  {
  31      /** Time, in seconds, to be labeled a slow command. */
  32      const SLOW_CMD = 5;
  33  
  34      /**
  35       * Is debugging active?
  36       *
  37       * @var boolean
  38       */
  39      public $debug = true;
  40  
  41      /**
  42       * The debug stream.
  43       *
  44       * @var resource
  45       */
  46      protected $_stream;
  47  
  48      /**
  49       * Timestamp of last command.
  50       *
  51       * @var integer
  52       */
  53      protected $_time = null;
  54  
  55      /**
  56       * Constructor.
  57       *
  58       * @param mixed $debug  The debug target.
  59       */
  60      public function __construct($debug)
  61      {
  62          $this->_stream = is_resource($debug)
  63              ? $debug
  64              : @fopen($debug, 'a');
  65          register_shutdown_function(array($this, 'shutdown'));
  66      }
  67  
  68      /**
  69       * Shutdown function.
  70       */
  71      public function shutdown()
  72      {
  73          if (is_resource($this->_stream)) {
  74              fflush($this->_stream);
  75              fclose($this->_stream);
  76              $this->_stream = null;
  77          }
  78      }
  79  
  80      /**
  81       * Write client output to debug log.
  82       *
  83       * @param string $msg  Debug message.
  84       */
  85      public function client($msg)
  86      {
  87          $this->_write($msg . "\n", 'C: ');
  88      }
  89  
  90      /**
  91       * Write informational message to debug log.
  92       *
  93       * @param string $msg  Debug message.
  94       */
  95      public function info($msg)
  96      {
  97          $this->_write($msg . "\n", '>> ');
  98      }
  99  
 100      /**
 101       * Write server output to debug log.
 102       *
 103       * @param string $msg  Debug message.
 104       */
 105      public function raw($msg)
 106      {
 107          $this->_write($msg);
 108      }
 109  
 110      /**
 111       * Write server output to debug log.
 112       *
 113       * @param string $msg  Debug message.
 114       */
 115      public function server($msg)
 116      {
 117          $this->_write($msg . "\n", 'S: ');
 118      }
 119  
 120      /**
 121       * Write debug information to the output stream.
 122       *
 123       * @param string $msg  Debug data.
 124       */
 125      protected function _write($msg, $pre = null)
 126      {
 127          if (!$this->debug || !$this->_stream) {
 128              return;
 129          }
 130  
 131          if (!is_null($pre)) {
 132              $new_time = microtime(true);
 133  
 134              if (is_null($this->_time)) {
 135                  fwrite(
 136                      $this->_stream,
 137                      str_repeat('-', 30) . "\n" . '>> ' . date('r') . "\n"
 138                  );
 139              } elseif (($diff = ($new_time - $this->_time)) > self::SLOW_CMD) {
 140                  fwrite(
 141                      $this->_stream,
 142                      '>> Slow Command: ' . round($diff, 3) . " seconds\n"
 143                  );
 144              }
 145  
 146              $this->_time = $new_time;
 147          }
 148  
 149          fwrite($this->_stream, $pre . $msg);
 150      }
 151  
 152  }