1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
<?php
/*
** Zabbix
** Copyright (C) 2001-2014 Zabbix SIA
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**/
function sdb($return = false) {
$backtrace = debug_backtrace();
array_shift($backtrace);
$result = 'DEBUG BACKTRACE: <br/>';
foreach ($backtrace as $n => $bt) {
$result .= ' --['.$n.']-- '.$bt['file'].' : '.$bt['line'].'<br/>';
$result .= " <b>".(isset($bt['class']) ? $bt['class'].$bt['type'].$bt['function'] : $bt['function']).'</b>';
$args = array();
foreach ($bt['args'] as $arg) {
$args[] = is_array($arg) ? print_r($arg, true) : $arg;
}
$result .= '( '.implode(', ', $args).' ) <br/>';
}
if ($return) {
return $result;
}
else {
echo $result;
}
}
function sdi($msg = 'SDI') {
echo 'DEBUG INFO: ';
var_dump($msg);
echo SBR;
}
function sdii($msg = 'SDII', $for = '', $showInvisible = true) {
if ($showInvisible) {
if ($msg === null) {
$msg = 'NULL';
}
elseif ($msg === false) {
$msg = 'FALSE';
}
elseif ($msg === TRUE) {
$msg = 'TRUE';
}
}
echo 'DEBUG INFO: '.$for;
echo '<pre>'.print_r($msg, true).'</pre>';
echo SBR;
}
function vdp($var, $msg = null) {
echo 'DEBUG DUMP: ';
if (isset($msg)) {
echo '"'.$msg.'"'.SPACE;
}
var_dump($var);
echo SBR;
}
function todo($msg) {
echo 'TODO: '.$msg.SBR;
}
/**
* Writes data in given file. Rewrites file on each PHP execution.
*
* @staticvar resource $fileStream resource of opened file
* @param mixed $data data to write in file
* @param boolean $persist persist file content on multiple script runs
* @param string $fileName file where output will be stored
*/
function sdFile($data, $persist = false, $fileName = 'debug.txt') {
static $fileStream;
if ($persist || $fileStream) {
$fileStream = fopen($fileName, 'a');
}
else {
$fileStream = fopen($fileName, 'w');
}
fwrite($fileStream, var_export($data, true)."\n\n");
fclose($fileStream);
}
function sdff($msg, $fileName = '/tmp/zabbix.log') {
$fileStreem = @fopen($fileName, 'a');
if (is_array($msg)) {
$toImplode = array();
foreach ($msg as $key => $value) {
$toImplode[] = var_export($key, true).'=>'.var_export($value, true);
}
@fwrite($fileStreem, 'array('.implode(',', $toImplode).')'."\n\n");
} else {
@fwrite($fileStreem, var_export($msg, true)."\n\n");
}
@fclose($fileStreem);
}
function sdf(&$var) {
$value = $var;
$var = $new = null;
$varname = false;
foreach ($GLOBALS as $key => $val) {
if ($val === $new) {
$varname = $key;
}
}
echo '$'.$varname.'=';
if (is_array($value) || is_object($value)) {
echo '<pre>'.print_r($value, true).'</pre>';
}
else {
echo $value;
}
echo SBR;
}
/**
* Infinite loop breaker, can be called inside loop that can be infinite. If number of
* calls exceeds defined limit then backtrace is printed and script is terminated.
*
* @param $limit number of function calls after which script should be terminated.
*/
function ilb($limit = 100) {
static $counter = 0;
$counter++;
if ($counter == $limit) {
// just calling sdb is forbidden by pre-commit hook :/
call_user_func('sdb');
exit;
}
}
function timer($timer = null) {
static $timers = array();
if ($timer === null) {
$timer = '_general_';
}
$mtime = microtime(true);
if (isset($timers[$timer])) {
echo $timer.': '.round($mtime - $timers[$timer]['start'], 4).' ('.round($mtime - $timers[$timer]['last'], 4).')'.'<br>';
$timers[$timer]['last'] = $mtime;
}
else {
echo $timer.' started.'.'<br>';
$timers[$timer]['start'] = $mtime;
$timers[$timer]['last'] = $mtime;
}
}
/**
* Shorthand for throwing exception
*
* @param string $ex exception text
*/
function sdex($ex = 'My exception') {
throw new APIException(ZBX_API_ERROR_INTERNAL, $ex);
}
|