[ Index ]

PHP Cross Reference of phool

title

Body

[close]

/Phool/ -> Display.php (source)

   1  <?php
   2  //============================================================================
   3  // This program is free software: you can redistribute it and/or modify
   4  // it under the terms of the GNU Lesser General Public License (LGPL) as
   5  // published by the Free Software Foundation, either version 3 of the License,
   6  // or (at your option) any later version.
   7  //
   8  // This program is distributed in the hope that it will be useful,
   9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11  // GNU Lesser General Public License for more details.
  12  //
  13  // You should have received a copy of the GNU Lesser General Public License
  14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
  15  //============================================================================
  16  /**
  17  * @copyright Francois Laupretre <phool@tekwire.net>
  18  * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, V 2.0
  19  * @category phool
  20  * @package phool
  21  */
  22  //============================================================================
  23  
  24  namespace Phool;
  25  
  26  //============================================================================
  27  /**
  28  * Static functions used to display messages (normal, trace, debug...)
  29  */
  30  
  31  class Display
  32  {
  33  const MAX_VERBOSE_LEVEL=2;    // Highest message level
  34  const MIN_VERBOSE_LEVEL=-3; // Lowest message level -1
  35  
  36  // Note: the minimal verbose level allows to hide every messages
  37  
  38  private static $prefix=array(
  39       2 => '>> ',            // Debug
  40       1 => '> ',                // Trace
  41       0 => '',                // Info
  42      -1 => '*Warning* ',     // Warning
  43      -2 => "\n***Error*** "    // Error
  44      );
  45  
  46  /** @var integer Verbose level, default=0 */
  47  
  48  private static $verbose_level=0;
  49  
  50  /** @var integer Array containing the error msgs since the beginning */
  51  
  52  private static $errors=array();
  53  
  54  //----------------------------------------------------------------------------
  55  /**
  56  * Increment verbose level
  57  *
  58  * @return void
  59  */
  60  
  61  public static function incVerbose()
  62  {
  63  if (self::$verbose_level < self::MAX_VERBOSE_LEVEL) self::$verbose_level++;
  64  }
  65  
  66  //----------------------------------------------------------------------------
  67  /**
  68  * Decrement verbose level
  69  *
  70  * @return void
  71  */
  72  
  73  public static function decVerbose()
  74  {
  75  if (self::$verbose_level > self::MIN_VERBOSE_LEVEL) self::$verbose_level--;
  76  }
  77  
  78  //----------------------------------------------------------------------------
  79  /**
  80  * Set verbose level
  81  *
  82  * @param integer $level integer
  83  * @return void
  84  */
  85  
  86  public static function setVerbose($level)
  87  {
  88  self::$verbose_level=$level;
  89  }
  90  
  91  //----------------------------------------------------------------------------
  92  /**
  93  * Conditionnally display a string to stderr
  94  *
  95  * Display the string if the message level is less or equal to the verbose level
  96  *
  97  * @param string $msg The message
  98  * @param integer $level The message level
  99  * @return void
 100  */
 101  
 102  private static function _display($msg,$level)
 103  {
 104  if ($level <= self::$verbose_level)
 105      {
 106      $msg=self::$prefix[$level].$msg."\n";
 107      if (defined('STDERR')) fprintf(STDERR,"%s",$msg);
 108      else echo $msg;
 109      }
 110  }
 111  
 112  //----------------------------------------------------------------------------
 113  /**
 114  * Display an error message
 115  *
 116  * @param string $msg The message
 117  * @return void
 118  */
 119  
 120  public static function error($msg)
 121  {
 122  self::_display($msg,-2);
 123  self::$errors[]=$msg;
 124  }
 125  
 126  //----------------------------------------------------------------------------
 127  /**
 128  * Display a warning message
 129  *
 130  * @param string $msg The message
 131  * @return void
 132  */
 133  
 134  public static function warning($msg)
 135  {
 136  self::_display($msg,-1);
 137  }
 138  
 139  //----------------------------------------------------------------------------
 140  /**
 141  * Return the current error count
 142  *
 143  * @return int
 144  */
 145  
 146  public static function errorCount()
 147  {
 148  return count(self::$errors);
 149  
 150  }
 151  
 152  //----------------------------------------------------------------------------
 153  /**
 154  * Return the error array
 155  *
 156  * @return array
 157  */
 158  
 159  public static function getErrors()
 160  {
 161  return self::$errors;
 162  }
 163  
 164  //----------------------------------------------------------------------------
 165  /**
 166  * Display a level 0 message
 167  *
 168  * @param string $msg The message
 169  * @return void
 170  */
 171  
 172  public static function msg($msg)
 173  {
 174  self::_display($msg,0);
 175  }
 176  
 177  //----------------------------------------------------------------------------
 178  /**
 179  * Display an info message
 180  *
 181  * @param string $msg The message
 182  * @return void
 183  */
 184  
 185  public static function info($msg)
 186  {
 187  self::_display($msg,0);
 188  }
 189  
 190  //----------------------------------------------------------------------------
 191  /**
 192  * Display a trace message
 193  *
 194  * @param string $msg The message
 195  * @return void
 196  */
 197  
 198  public static function trace($msg)
 199  {
 200  self::_display($msg,1);
 201  }
 202  
 203  //----------------------------------------------------------------------------
 204  /**
 205  * Display a debug message
 206  *
 207  * @param string $msg The message
 208  * @return void
 209  */
 210  
 211  public static function debug($msg)
 212  {
 213  self::_display($msg,2);
 214  }
 215  
 216  //----------------------------------------------------------------------------
 217  /**
 218  * Convert a boolean to a displayable string
 219  *
 220  * @param bool $val The boolean value to convert
 221  * @return string The string to display
 222  */
 223  
 224  public static function boolStr($val)
 225  {
 226  return ($val ? 'yes' : 'no');
 227  }
 228  
 229  //----------------------------------------------------------------------------
 230  /**
 231  * Converts a variable through var_dump()
 232  *
 233  * @param any $var The value to convert
 234  * @return string The dumped value
 235  */
 236  
 237  public static function vdump($var)
 238  {
 239  ob_start();
 240  var_dump($var);
 241  return ob_get_clean();
 242  }
 243  
 244  //----------------------------------------------------------------------------
 245  /**
 246  * Display current stack trace
 247  *
 248  * @return void
 249  */
 250  
 251  public static function showTrace()
 252  {
 253  $e=new \Exception();
 254  print_r($e->getTrace());
 255  }
 256  
 257  //----------------------------------------------------------------------------
 258  /**
 259  * Return displayable type of a variable
 260  *
 261  * @param any $var
 262  * @return string
 263  */
 264  
 265  public static function varType($var)
 266  {
 267  return is_object($var) ? 'object '.get_class($var) : gettype($var);
 268  }
 269  
 270  //----------------------------------------------------------------------------
 271  /**
 272  * Convert a boolean to a displayable string
 273  *
 274  * @param any $var
 275  * @return string
 276  */
 277  
 278  public static function bool2str($var)
 279  {
 280  return $var ? 'Yes' : 'No';
 281  }
 282  
 283  //----------------------------------------------------------------------------
 284  } // End of class Display
 285  ?>


Generated: Thu Jun 4 19:17:11 2015 Cross-referenced by PHPXref 0.7.1