-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dsndebug.php
175 lines (132 loc) · 4.79 KB
/
Dsndebug.php
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
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
* The DSNDebug Library
* @package LIBRARIES \ DSNDEBUG
* @category Labrary
*
* @copyright 2015-2017 3SN.IT
* @author Johnny DRIESEN <info.@3sn.be>
* @version 1.0.0
* @since 1.0.0 Initial Model File
*
* @todo clean up code
*/
class DSNDebug {
private $fp;
private $on_screen = TRUE;
private $fp_isopen = FALSE;
private static $forceLoggingInProduction = FALSE;
private static $debugFolder = './debug/';
private static $debugFileExt = '.dbg.txt';
// If you want to prefix the log info with a DateTime stamp, set to TRUE ...
private $dateIncluded = FALSE;
private $dta_format = 'Y-m-d H:i:s';
public function __construct($onScreen = TRUE, $toFile = FALSE, $fName = ''){
// Extra test ... NEVER allow Debug Screen writes in Production !!
$this->on_screen = (self::inDevMode() && $onScreen);
if ($toFile) { $this->fp = self::open($fName); }
}
function __destruct() {
$res = self::close();
}
// Force a DIE ...
public function DSNDIE() {
$trace = debug_backtrace();
$caller = $trace[1];
$caller_function = (isset($caller['function'])) ? $caller['function'] : 'no_function';
$caller_class = (isset($caller['class'])) ? $caller['class'] : 'no_class';
$s = ' BY DSNDebug IN FUNCTION ' . $caller_function . ' IN CLASS ' . $caller_class;
self::screen('KILLED', $s);
self::swrite("\n" . '== Closed by DSNDIE == ');
$res = self::close();
die;
}
// Used to force screen output in a LIVE environment...
// Don't use this function when not really needed ...
public function forceScreen($bForce = FALSE) {
$this->on_screen = $bForce;
}
// Shows the debug info on the screen ...
public function screen($objName = '', $objValue = NULL) {
if($this->on_screen) {
$k = self::the_date() . $objName;
$v = (!is_null($objValue)) ? ' = ' . var_export($objValue,TRUE) : '';
$s = "\n" ;
$s .= "<pre>" ;
$s .= "<b>" . $k . "</b>" . $v;
$s .= "</pre>";
echo $s;
}
}
// Shows the debug info on the screen ...
public function screen_success($objName = '', $objValue = NULL) {
$objName = "<font color='green'>" . $objName . "</font>";
self::screen($objName, $objValue);
}
public function screen_error($objName = '', $objValue = NULL) {
$objName = "<font color='red'>" . $objName . "</font>";
self::screen($objName, $objValue);
}
public function screen_warning($objName = '', $objValue = NULL) {
$objName = "<font color='orange'>" . $objName . "</font>";
self::screen($objName, $objValue);
}
public function screen_info($objName = '', $objValue = NULL) {
$objName = "<font color='blue'>" . $objName . "</font>";
self::screen($objName, $objValue);
}
// Writes the debug to a file ...
public function write($objName = '', $objValue = 'dsndummyvalue') {
$v = (!($objValue === 'dsndummyvalue')) ? ' = ' . var_export($objValue,TRUE) : '';
$s = $objName . $v;
self::swrite($s);
}
// **** The PRIVATE Functions used in this class ...
private function getDebugFolder() {
return self::$debugFolder;
}
private function the_date() {
$ret = ($this->dateIncluded) ? '[' . date_format(date_create(), $this->dta_format) . '] ' : '';
return $ret;
}
private static function inDevMode() {
if (self::$forceLoggingInProduction) {
$ret = self::$forceLoggingInProduction;
} else {
$ret = (strtoupper(getenv('CI_ENV')) == strtoupper("development"));
}
return $ret;
}
private function open($filename = '') {
// Only allow writing Debug files when in Development mode !!
if(self::inDevMode()) {
if ($filename === '') {
$trace = debug_backtrace();
// 2 step back... cause it's coming from the constructor of THIS class
$caller = $trace[2];
$caller_function = (isset($caller['function'])) ? $caller['function'] : 'no_function';
$caller_class = (isset($caller['class'])) ? $caller['class'] : 'no_class';
$filename = $caller_class . '_' . $caller_function . self::$debugFileExt;
}
$this->fp = fopen(self::$debugFolder . $filename, 'a+');
$this->fp_isopen = TRUE;
self::swrite('== Open Debugging == ');
$sfunc = debug_backtrace()[1]['function'];
$sclass = debug_backtrace()[1]['class'];
return $this->fp;
}
}
private function close() {
if(($this->fp) && ($this->fp_isopen)) {
self::swrite('== Close Debugging == ' . "\n");
fclose($this->fp);
$this->fp_isopen = FALSE;
}
return $this->fp_isopen;
}
private function swrite($sname = '', $svalue = '') {
if($this->fp) {
fwrite($this->fp, self::the_date() . $sname . ' ' . $svalue . "\n");
}
}
}