« Home Page

 

»

Main » Wiki Sandbox

a page to play with wiki formatting


You don't have permission to edit this page.


Categories:Uncategorized

  1. <?php
  2. /**
  3. * debug functions that can be used in any php application
  4. * Copyright (C) 2011 Tamara Temple
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
  19. *
  20. * @author Tamara Temple <tamara@tamaratemple.com>
  21. * @version 2011-06-19
  22. * @copyright Tamara Temple Development, 19 June, 2011
  23. * @license http://opensource.org/licenses/gpl-license.php GNU Public License
  24. * @package commoncode
  25. **/
  26.  
  27.  
  28.  
  29.  
  30.  
  31.  
  32. /**
  33. * Debug - set up a class for debugging statements. Simple, but in OO!
  34. *
  35. * @author Tamara Temple <tamara@tamaratemple.com>
  36. * @package commoncode
  37. */
  38. class Debug
  39. {
  40.     /**
  41.      * flag to indicate whether we're in a debugging session or not
  42.      *
  43.      * This variable holds the state of whether we are in a debugging session or not. The Debug object
  44.      * can set this on instantiation, or later through the on() pseudo-method.
  45.      *
  46.      * @var string
  47.      **/
  48.     private $debugflag = TRUE;
  49.    
  50.     /**
  51.      * flag to indicate whether the print method should also emit HTML tags around the various parts.
  52.      *
  53.      * @var string
  54.      **/
  55.     private $nohtml = FALSE;
  56.    
  57.     /**
  58.     * prefix to emit before debugging data
  59.     *
  60.     * This variable is to be used to customize whatever code is needed to set up the debug data environment.
  61.     * Typical possibilities include div or p opening with appropriat class specifications:
  62.     *   '<p class="debug">' for example.
  63.     *
  64.     * If the nohtml flag is set to TRUE, the prefix and suffix will not be emitted.
  65.     *
  66.     * @var string
  67.     **/
  68. private $prefix = '<p class="debug">';
  69.  
  70. /**
  71. * suffix to emit after debugging data
  72. *
  73. * This is the companion variable to $prefix above. Set it to close whatever you've opened in the
  74. * prefix, such as '</p>' in the example above. If this is empty or not set, nothing will be emitted.
  75. *
  76. * @var string
  77. **/
  78. private $suffix = '</p>';
  79.  
  80. /**
  81. * log file for sending debug output to, as well as stdout
  82. *
  83. * Should be a full path from /
  84. * If this is empty or not set, the error will be logged to the standard php logging facility.
  85. * If this is set to -1, no logging is performed.
  86. *
  87. * @var string
  88. **/
  89. private $errorlog = '';
  90.  
  91. /**
  92.  * flag to indicate whether debug output should be saved instead of printed.
  93.  *
  94.  * @var boolean
  95.  **/
  96. private $hold = FALSE;
  97.  
  98. /**
  99.  * array holding debug output for later retrieval. Used if hold is true
  100.  *
  101.  * @var array
  102.  **/
  103. private $heldmessages = array();
  104.  
  105. /**
  106. * contructor - initialize object when called with $dbg = new Debug('prefix','suffix','errorlog')
  107. *
  108. * @param bool (optional) debugflag - controls whether debugging is turned on or off. If omitted, set to true
  109. * @param bool (optional) nohtml - flag determining whether debug should emit html with it's messages, or just plain text.
  110. * @param string (optional) errorlog - full path name to error log file to receive debugging log messages
  111. * @param string (optional) prefix - the prefix to emit before the debugging data
  112. * @param string (optional) suffix - the suffix to emit after debugging data
  113. * @return object - returns the new Debug object
  114. * @author Tamara Temple
  115. */
  116. function __construct($debugflag=TRUE,$nohtml=FALSE,$errorlog=NULL,$hold=FALSE,$prefix=NULL,$suffix=NULL)
  117. {
  118.     $this->debugflag = isset($debugflag)?$debugflag:TRUE;
  119.     $this->nohtml = isset($nohtml)?$nohtml:FALSE;
  120.     $this->prefix = isset($prefix)?$prefix:'<p class="debug">';
  121.     $this->suffix = isset($suffix)?$suffix:'</p>';
  122.     $this->errorlog = isset($errorlog)&&
  123.         ((is_dir(dirname($errorlog))&&is_writable($errorlog)) ||
  124.         ($errorlog = "-1"))?$errorlog:NULL;
  125.     $this->hold = isset($hold)?$hold:FALSE;
  126. }
  127.  
  128.  
  129. /**
  130.  * anonymous calling function to enable simple pseudo-methods
  131.  *
  132.  * @method void on() - turns debugging on
  133.  * @method void off() - turns debugging off
  134.  * @method bool is_on() - returns state of debugflag
  135.  * @method void setsurrounds(prefix,suffix) - sets the value of prefix and suffix
  136.  * @method void setlog(errorlog) - sets the value of error log. Must be fully-qualified, writeable file, empty string to set logging to php's logger, or -1 to turn logging off
  137.  * @method void nohtml(bool) - sets the nohtml flag to TRUE or FALSE
  138.  * @method void hold(bool) - sets the internal hold flag to TRUE or FALSE
  139.  * @method array getMessages() - returns the set of debug messages that have been held in queue
  140.  * @return mixed
  141.  * @author Tamara Temple
  142.  **/
  143. public function __call($method, $params)
  144. {
  145.     switch ($method) {
  146.         case 'on':
  147.             $this->debugflag=TRUE;
  148.             break;
  149.         case 'off':
  150.             $this->debugflag=FALSE;
  151.             break;
  152.         case 'is_on':
  153.             return $this->debugflag;
  154.             break;
  155.         case 'setsurroundings':
  156.             if (count($params) != 2)    throw new Exception(self::$errormessages[1001], 1001);
  157.             if (is_string($params[0])) $this->prefix=$params[0];
  158.             if (is_string($params[1])) $this->suffix=$params[1];
  159.             break;
  160.         case 'setlog':
  161.             if (isset($params)) {
  162.                 if (is_string($params[0]) && is_dir(dirname($params[0])) && is_writable($params[0])) $this->errorlog = $params[0];
  163.                 if ($params[0] == "-1") $this->errorlog = $params;
  164.                 if ($params[0] == '') $this->errorlog = '';
  165.             }
  166.             break;
  167.         case 'nohtml':
  168.             if (empty($params)) return $this->nohtml;
  169.             if (isset($params[0]) && is_bool($params[0])) $this->nohtml = $params[0];
  170.             break;
  171.         case 'hold':
  172.             if (empty($params)) return $this->hold;
  173.             if (isset($params[0]) && is_bool($params[0])) $this->hold = $params[0];
  174.             break;
  175.         case 'debug':
  176.         case 'debug_var':
  177.             /* depricated functions, use p instead */
  178.             error_log(self::$errormessages[1002]);
  179.             call_user_func_array(array($this,'p'), $params);
  180.             break;
  181.         case 'getMessages':
  182.             return $this->heldmessages;
  183.             break;
  184.         default:
  185.             # code...
  186.             break;
  187.     }
  188. }
  189.  
  190.  
  191.  
  192. /**
  193. * print function - print a message if DEBUG === TRUE
  194. *
  195. * @param string msg - the debugging message to emit
  196. * @param mixed (optional) var - a variable to show the contents of. If not a scalar, the output will be dumped from print_r instead
  197. * @param string file - name of file debug message comes from. This can easily be set with the magic constant __FILE__
  198. * @param string line - line of the file where the debug message is sent from. This can easily be set with the magic constant __LINE__
  199. * @return void
  200. * @author Tamara Temple <tamara@tamaratemple.com>
  201. *
  202. * CHANGED: made function a bit more useful by being the ONLY debug function to call, and to allow $var, $file, and $line to be optional parameters. If $var is an array, it will print out via print_r, otherwise it's just echoed as is. $file and $line are used to supply __FILE__ and __LINE__ parameters specifically. This is possibly prettier than making every debug call look like >> debug(basename(__FILE__).'@'.__LINE__.' '."message"); << as it will now read as: >> debug("message",'',__FILE__,__LINE__); << instead.
  203. * CHANGED name is now p instead of debug
  204. *
  205. *
  206. **/
  207. public function p($msg,$var='',$file='',$line='')
  208. {
  209.     if (!$this->debugflag) return;
  210.     if (!empty($file)) $file = basename($file);
  211.     $out = 'DEBUG: ';
  212.     if (!empty($file)) $out .= $file;
  213.     if (!empty($line)) $out .= '@'.$line;
  214.     $out .= $msg.PHP_EOL;
  215.     if (!empty($var)) {
  216.         if (is_array($var)) {
  217.             if ($this->nohtml) {
  218.                 $out .= print_r($var,true);
  219.             } else {
  220.                 $out .= '<pre>' . htmlspecialchars(print_r($var,true)) . '</pre>';             
  221.             }
  222.         } else {
  223.             $out .= htmlspecialchars($var);
  224.         }
  225.         $out .= PHP_EOL;
  226.     }
  227.     if ($this->hold) {
  228.         $this->debugmessages[] = $out; // hold message until later
  229.     } else {
  230.         if ($this->nohtml) {
  231.             echo $out;
  232.         } else {
  233.             echo $this->prefix.PHP_EOL;
  234.             echo $out;
  235.             echo $this->suffix.PHP_EOL;
  236.         }
  237.     }
  238.  
  239.     if ($this->errorlog != "-1") {
  240.         if (is_string($this->errorlog)){
  241.             error_log($out,3,$this->errorlog);
  242.         } else {
  243.             error_log($out);
  244.         }
  245.     }
  246. }
  247.  
  248. /* set these up in the future hope of i18n */
  249. private static $errormessages = array(
  250.     1001=>'setsurroundings must be called with two parameters',
  251.     1002=>'debug and debug_var are depricated functions, use p instead',
  252.     );
  253.  
  254. }


Categories: Uncategorized


Page last modified at November 09, 2011, at 09:10 AM by tamara. (History). (Talk)

Remove the above line, this line and closing line of the comment block to use these items. Categories: Uncategorized

Tags:
Portfolio

Copyright © 2011- Tamara Temple Web Development. Code is licensed GPLv3. Content is all rights reserved.
Built using PmWiki version pmwiki-2.2.35.