1: <?php
2: /*
3: You may not change or alter any portion of this comment or credits
4: of supporting developers from this source code or any supporting source code
5: which is considered copyrighted (c) material of the original comment or credit authors.
6:
7: This program is distributed in the hope that it will be useful,
8: but WITHOUT ANY WARRANTY; without even the implied warranty of
9: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10: */
11:
12: namespace Xmf;
13:
14: /**
15: * Debugging tools for developers
16: *
17: * @category Xmf\Module\Debug
18: * @package Xmf
19: * @author trabis <lusopoemas@gmail.com>
20: * @author Richard Griffith <richard@geekwright.com>
21: * @copyright 2011-2015 XOOPS Project (http://xoops.org)
22: * @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
23: * @version Release: 1.0
24: * @link http://xoops.org
25: * @since 1.0
26: */
27: class Debug
28: {
29: /**
30: * associative array of timers
31: *
32: * @var float[]
33: */
34:
35: private static $times = array();
36:
37: /**
38: * indexed array of timer data in form
39: * array('label' => string, 'start' => float, 'elapsed' => float)
40: *
41: * @var array
42: */
43:
44: private static $timerQueue = array();
45:
46: /**
47: * associative array of timer labels
48: *
49: * @var string[]
50: */
51:
52: private static $timerLabels = array();
53:
54:
55: /**
56: * Dump a variable
57: *
58: * @param mixed $var variable which will be dumped
59: * @param bool $inline force inline display if true, otherwise will attempt to
60: * use debug.log event
61: *
62: * @return void
63: */
64: public static function dump($var, $inline = false)
65: {
66: $events = \Xoops::getInstance()->events();
67: $eventName = 'debug.log';
68: if (!$inline && $events->hasListeners($eventName)) {
69: $events->triggerEvent($eventName, $var);
70: //\Kint::dump(func_get_arg(0));
71: } else {
72: $config = array(
73: 'skin' => array('selected' => 'modern'),
74: 'css' => array('url' => \XoopsBaseConfig::get('url') . '/modules/xmf/css/krumo/'),
75: 'display' => array(
76: 'show_version' => false,
77: 'show_call_info' => false,
78: 'sort_arrays' => false,
79: ),
80: );
81: \krumo::setConfig($config);
82: $msg = \krumo::dump($var);
83: echo $msg;
84: }
85: }
86:
87: /**
88: * Display debug backtrace
89: *
90: * @param boolean $inline force inline display if true, otherwise will attempt to
91: * use debug.log event
92: *
93: * @return mixed|string
94: */
95: public static function backtrace($inline = false)
96: {
97: $events = \Xoops::getInstance()->events();
98: $eventName = 'debug.log';
99: if (!$inline && $events->hasListeners($eventName)) {
100: $events->triggerEvent($eventName, debug_backtrace());
101: } else {
102: return self::dump(debug_backtrace(), $inline);
103: }
104: }
105:
106: /**
107: * Start a timer
108: *
109: * @param string $name unique name for timer
110: * @param string|null $label optional label for this timer
111: *
112: * @return void
113: */
114: public static function startTimer($name, $label = null)
115: {
116: $events = \Xoops::getInstance()->events();
117: $var = array($name);
118: $var[] = empty($label) ? $name : $label;
119: $eventName = 'debug.timer.start';
120: if ($events->hasListeners($eventName)) {
121: $events->triggerEvent($eventName, $var);
122: } else {
123: self::$times[$name] = microtime(true);
124: }
125: }
126:
127: /**
128: * Stop a timer
129: *
130: * @param string $name unique name for timer
131: *
132: * @return void
133: */
134: public static function stopTimer($name)
135: {
136: $events = \Xoops::getInstance()->events();
137: $eventName = 'debug.timer.stop';
138: if ($events->hasListeners($eventName)) {
139: $events->triggerEvent($eventName, $name);
140: } else {
141: echo $name . ' - ' . (int)(microtime(true) - self::$times[$name]) . " \n";
142: }
143: }
144:
145: /**
146: * Start a queued timer. Queued timers are stored and only dumped by request.
147: * This makes them useful in recording timing when immediate output is not
148: * possible practical, such as early system startup activities. Timers can be
149: * queued at any point once the Xmf\Debug class can be loaded then dumped
150: * when system facilities are available.
151: *
152: * @param string $name unique name for timer
153: * @param string|null $label optional label for this timer
154: *
155: * @return void
156: */
157: public static function startQueuedTimer($name, $label = null)
158: {
159: self::$times[$name] = microtime(true);
160: self::$timerLabels[$name] = empty($label) ? $name : $label;
161: }
162:
163: /**
164: * Stop a queued timer
165: *
166: * @param string $name unique name for timer
167: *
168: * @return void
169: */
170: public static function stopQueuedTimer($name)
171: {
172: if (isset(self::$timerLabels[$name]) && isset(self::$times[$name])) {
173: $queueItem = array(
174: 'label' => self::$timerLabels[$name],
175: 'start' => self::$times[$name],
176: 'elapsed' => microtime(true) - self::$times[$name],
177: );
178: self::$timerQueue[] = $queueItem;
179: }
180: }
181:
182: /**
183: * dump and queued timer data and reset the queue
184: *
185: * Note: The DebugBar logger will add any unprocessed queue data to its
186: * timeline automatically, if you use queued timers and don't call this.
187: *
188: * @param boolean $returnOnly if true do not dump queue, only return it
189: *
190: * @return array of time data see \Xmf\Debug::$timerQueue
191: */
192: public static function dumpQueuedTimers($returnOnly = false)
193: {
194: $queue = self::$timerQueue;
195: self::$timerQueue = array();
196: if (!$returnOnly) {
197: self::dump($queue);
198: }
199:
200: return $queue;
201: }
202:
203: /**
204: * start_trace - turn on xdebug trace
205: *
206: * Requires xdebug extension
207: *
208: * @param type $tracefile file name for trace file
209: * @param type $collect_params argument for ini_set('xdebug.collect_params',?)
210: * Controls display of parameters in trace output
211: * @param type $collect_return argument for ini_set('xdebug.collect_return',?)
212: * Controls display of function return value in trace
213: *
214: * @return void
215: */
216: public static function startTrace($tracefile = '', $collect_params = '3', $collect_return = 'On')
217: {
218: if (function_exists('xdebug_start_trace')) {
219: ini_set('xdebug.collect_params', $collect_params);
220: ini_set('xdebug.collect_return', $collect_return);
221: if ($tracefile == '') {
222: $tracefile = \XoopsBaseConfig::get('var-path') . '/logs/php_trace';
223: }
224: xdebug_start_trace($tracefile);
225: }
226: }
227:
228: /**
229: * stop_trace - turn off xdebug trace
230: *
231: * Requires xdebug extension
232: *
233: * @return void
234: */
235: public static function stopTrace()
236: {
237: if (function_exists('xdebug_stop_trace')) {
238: xdebug_stop_trace();
239: }
240: }
241: }
242: