|
| 1 | +<?php |
| 2 | +// The data models. |
| 3 | +namespace ReThumbAdvanced\ShortPixelLogger; |
| 4 | + |
| 5 | + |
| 6 | +class DebugItem |
| 7 | +{ |
| 8 | + protected $time; |
| 9 | + protected $level; |
| 10 | + protected $message; |
| 11 | + protected $data = array(); |
| 12 | + protected $caller = false; // array when filled |
| 13 | + |
| 14 | + protected $model; |
| 15 | + |
| 16 | + const LEVEL_ERROR = 1; |
| 17 | + const LEVEL_WARN = 2; |
| 18 | + const LEVEL_INFO = 3; |
| 19 | + const LEVEL_DEBUG = 4; |
| 20 | + |
| 21 | + public function __construct($message, $args) |
| 22 | + { |
| 23 | + $this->level = $args['level']; |
| 24 | + $data = $args['data']; |
| 25 | + |
| 26 | + $this->message = $message; |
| 27 | + $this->time = microtime(true); |
| 28 | + |
| 29 | + $this->setCaller(); |
| 30 | + |
| 31 | + // Add message to data if it seems to be some debug variable. |
| 32 | + if (is_object($this->message) || is_array($this->message)) |
| 33 | + { |
| 34 | + $data[] = $this->message; |
| 35 | + $this->message = __('[Data]'); |
| 36 | + } |
| 37 | + if (is_array($data) && count($data) > 0) |
| 38 | + { |
| 39 | + $dataType = $this->getDataType($data); |
| 40 | + if ($dataType == 1) // singular |
| 41 | + { |
| 42 | + $this->data[] = print_r($data, true); |
| 43 | + } |
| 44 | + if ($dataType == 2) //array |
| 45 | + { |
| 46 | + foreach($data as $index => $item) |
| 47 | + { |
| 48 | + if (is_object($item) || is_array($item)) |
| 49 | + { |
| 50 | + $this->data[] = print_r($item, true); |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + } // if |
| 55 | + elseif (! is_array($data)) // this leaves out empty default arrays |
| 56 | + { |
| 57 | + $this->data[] = print_r($data, true); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + public function getData() |
| 62 | + { |
| 63 | + return array('time' => $this->time, 'level' => $this->level, 'message' => $this->message, 'data' => $this->data, 'caller' => $this->caller); |
| 64 | + } |
| 65 | + |
| 66 | + /** Test Data Array for possible values |
| 67 | + * |
| 68 | + * Data can be a collection of several debug vars, a single var, or just an normal array. Test if array has single types, |
| 69 | + * which is a sign the array is not a collection. |
| 70 | + */ |
| 71 | + protected function getDataType($data) |
| 72 | + { |
| 73 | + $single_type = array('integer', 'boolean', 'string'); |
| 74 | + if (in_array(gettype(reset($data)), $single_type)) |
| 75 | + { |
| 76 | + return 1; |
| 77 | + } |
| 78 | + else |
| 79 | + { |
| 80 | + return 2; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + public function getForFormat() |
| 85 | + { |
| 86 | + $data = $this->getData(); |
| 87 | + switch($this->level) |
| 88 | + { |
| 89 | + case self::LEVEL_ERROR: |
| 90 | + $level = 'ERR'; |
| 91 | + $color = "\033[31m"; |
| 92 | + break; |
| 93 | + case self::LEVEL_WARN: |
| 94 | + $level = 'WRN'; |
| 95 | + $color = "\033[33m"; |
| 96 | + break; |
| 97 | + case self::LEVEL_INFO: |
| 98 | + $level = 'INF'; |
| 99 | + $color = "\033[37m"; |
| 100 | + break; |
| 101 | + case self::LEVEL_DEBUG: |
| 102 | + $level = 'DBG'; |
| 103 | + $color = "\033[37m"; |
| 104 | + break; |
| 105 | + |
| 106 | + } |
| 107 | + $color_end = "\033[0m"; |
| 108 | + |
| 109 | + $data['color'] = $color; |
| 110 | + $data['color_end'] = $color_end; |
| 111 | + $data['level'] = $level; |
| 112 | + |
| 113 | + return $data; |
| 114 | + |
| 115 | + //return array('time' => $this->time, 'level' => $level, 'message' => $this->message, 'data' => $this->data, 'color' => $color, 'color_end' => $color_end, 'caller' => $this->caller); |
| 116 | + |
| 117 | + } |
| 118 | + |
| 119 | + protected function setCaller() |
| 120 | + { |
| 121 | + if(PHP_VERSION_ID < 50400) { |
| 122 | + $debug=debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); |
| 123 | + } else { |
| 124 | + $debug=debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS,5); |
| 125 | + } |
| 126 | + |
| 127 | + $i = 4; |
| 128 | + if (isset($debug[$i])) |
| 129 | + { |
| 130 | + $info = $debug[$i]; |
| 131 | + $line = isset($info['line']) ? $info['line'] : 'Line unknown'; |
| 132 | + $file = isset($info['file']) ? basename($info['file']) : 'File not set'; |
| 133 | + |
| 134 | + $this->caller = array('line' => $line, 'file' => $file, 'function' => $info['function']); |
| 135 | + } |
| 136 | + |
| 137 | + |
| 138 | + } |
| 139 | + |
| 140 | + |
| 141 | +} |
0 commit comments