1: <?php
2:
3: namespace PHPixie;
4:
5: class Debug
6: {
7: protected $builder;
8:
9: static protected $instance;
10:
11: public function __construct()
12: {
13: $this->builder = $this->buildBuilder();
14: static::$instance = $this;
15: }
16:
17: public function builder()
18: {
19: return $this->builder;
20: }
21:
22: public function logger()
23: {
24: return $this->builder->logger();
25: }
26:
27: public function registerHandlers($shutdownLog = false, $exception = true, $error = true)
28: {
29: $this->builder->handlers()->register($shutdownLog, $exception, $error);
30: }
31:
32: public function dumpLog($withTitle = true, $withTraceArguments = true, $shortValueDump = null, $echo = true)
33: {
34: $log = $this->builder->messages()->log($withTitle, $withTraceArguments, $shortValueDump);
35: if($echo) {
36: static::output($log);
37: }
38:
39: return $log;
40: }
41:
42: public function exceptionMessage($exception, $backtraceLimit = null, $neighboringLines = 5, $echo = true)
43: {
44: $message = $this->builder->messages()->exception($exception, $backtraceLimit, $neighboringLines);
45: if($echo) {
46: static::output($message);
47: }
48:
49: return $message;
50: }
51:
52: public function exceptionTrace($exception, $backtraceLimit = null)
53: {
54: return $this->builder->tracer()->exceptionTrace($exception, $backtraceLimit);
55: }
56:
57: protected function buildBuilder()
58: {
59: return new Debug\Builder;
60: }
61:
62: static public function log($value, $shortDump = false)
63: {
64: static::instanceBuilder()->logger()->log($value, $shortDump, 1);
65: }
66:
67: static public function logTrace($limit = null, $offset = 0)
68: {
69: static::instanceBuilder()->logger()->trace($limit, 1+$offset);
70: }
71:
72: static public function dump($value, $shortDump = false, $echo = true)
73: {
74: $dump = static::instanceBuilder()->dumper()->dump($value, $shortDump);
75: if($echo) {
76: static::output($dump);
77: }
78:
79: return $dump;
80: }
81:
82: static public function trace($limit = null, $offset = 0, $echo = true)
83: {
84: $trace = static::instanceBuilder()->tracer()->backtrace($limit, 1+$offset);
85: if($echo) {
86: static::output($trace);
87: }
88:
89: return $trace;
90: }
91:
92: static protected function output($string)
93: {
94: echo "\n$string\n";
95: }
96:
97: static protected function instanceBuilder()
98: {
99: if(static::$instance === null) {
100: throw new Debug\Exception("Debug library has not been initialized yet");
101: }
102:
103: return static::$instance->builder;
104: }
105:
106: }