PageRenderTime 49ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/include/debug.class.php

http://zoneideas.googlecode.com/
PHP | 106 lines | 57 code | 28 blank | 21 comment | 7 complexity | 44c4fec675fa00783f3145902405f021 MD5 | raw file
  1. <?php
  2. /**
  3. * @package zoneideas
  4. * @subpackage debug
  5. * @author Serg Podtynnyi <serg.podtynnyi@gmail.com>
  6. */
  7. if (basename($_SERVER["SCRIPT_NAME"])==basename(__FILE__))die();
  8. /**
  9. * Some defines for DEBUG
  10. *
  11. *
  12. *
  13. */
  14. define("DEBUG_FILE_PATH","./debug.log");
  15. define("NOTICE" ,"NOTICE");
  16. define("WARNING","WARNING");
  17. define("ERROR" ,"ERROR");
  18. /**
  19. *
  20. * @package zoneideas
  21. * @final
  22. * @static
  23. */
  24. final class Debug
  25. {
  26. private static $instance;
  27. private static $last_debug_type = "screen";
  28. private $log_file = false;
  29. private $log_screen = false;
  30. public static function Init ($debug_type = "")
  31. {
  32. self::$instance = new Debug();
  33. if (empty($debug_type))
  34. {
  35. $debug_type = self::$last_debug_type;
  36. }
  37. if (stristr($debug_type,"file"))
  38. {
  39. self::$instance->log_file = true;
  40. }
  41. elseif (stristr($debug_type,"screen"))
  42. {
  43. self::$instance->log_screen = true;
  44. }
  45. self::$last_debug_type = $debug_type;
  46. }
  47. public static function Log ($message,$level=NOTICE)
  48. {
  49. $data[] = $level;
  50. $data[] = $message;
  51. self::WriteLine(self::PrepareLine($data));
  52. }
  53. private static function PrepareLine ($data)
  54. {
  55. $line = date("c");
  56. $line.= " ";
  57. // $debug_calls = debug_backtrace();
  58. // $file_name = basename($debug_calls[sizeof($debug_calls)-1]["file"]);
  59. // $file_line = $debug_calls[sizeof($debug_calls)-1]["line"] ;
  60. // $line.= sprintf("%-8s%'04d,%s %s ",$data[0],$file_line,$file_name,$data[1]);
  61. $line.= sprintf("%-8s %s ",$data[0],$data[1]);
  62. return $line;
  63. }
  64. private static function WriteLine ($data)
  65. {
  66. if (!DEBUG) return 0;
  67. if (empty(self::$instance))
  68. self::Init();
  69. if (self::$instance->log_file)
  70. file_put_contents(DEBUG_FILE_PATH,$data."\r\n",FILE_APPEND);
  71. if (self::$instance->log_screen)
  72. echo "<pre>".$data."</pre>";
  73. }
  74. public static function GetType()
  75. {
  76. return empty(self::$instance)?"none":self::$last_debug_type;
  77. }
  78. }