|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Admin Page Framework |
|
4
|
|
|
* |
|
5
|
|
|
* http://en.michaeluno.jp/admin-page-framework/ |
|
6
|
|
|
* Copyright (c) 2013-2016 Michael Uno; Licensed MIT |
|
7
|
|
|
* |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* A base class of the debug class. |
|
12
|
|
|
* |
|
13
|
|
|
* @since 3.8.9 |
|
14
|
|
|
* @extends AdminPageFramework_Debug_Base |
|
15
|
|
|
* @package AdminPageFramework |
|
16
|
|
|
* @subpackage Common/Utility |
|
17
|
|
|
*/ |
|
18
|
|
|
class AdminPageFramework_Debug_Log extends AdminPageFramework_Debug_Base { |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Logs the given variable output to a file. |
|
22
|
|
|
* |
|
23
|
|
|
* @param mixed $mValue The value to log. |
|
24
|
|
|
* @param string $sFilePath The log file path. |
|
25
|
|
|
* @since 3.8.9 |
|
26
|
|
|
* @return void |
|
27
|
|
|
**/ |
|
28
|
|
|
static protected function _log( $mValue, $sFilePath=null ) { |
|
29
|
|
|
|
|
30
|
|
|
static $_fPreviousTimeStamp = 0; |
|
31
|
|
|
|
|
32
|
|
|
$_oCallerInfo = debug_backtrace(); |
|
33
|
|
|
$_sCallerFunction = self::_getCallerFunctionName( $_oCallerInfo ); |
|
34
|
|
|
$_sCallerClass = self::_getCallerClassName( $_oCallerInfo ); |
|
35
|
|
|
$_fCurrentTimeStamp = microtime( true ); |
|
36
|
|
|
|
|
37
|
|
|
file_put_contents( |
|
38
|
|
|
self::_getLogFilePath( $sFilePath, $_sCallerClass ), |
|
39
|
|
|
self::_getLogContents( $mValue, $_fCurrentTimeStamp, $_fPreviousTimeStamp, $_sCallerClass, $_sCallerFunction ), |
|
40
|
|
|
FILE_APPEND |
|
41
|
|
|
); |
|
42
|
|
|
|
|
43
|
|
|
$_fPreviousTimeStamp = $_fCurrentTimeStamp; |
|
44
|
|
|
|
|
45
|
|
|
} |
|
46
|
|
|
/** |
|
47
|
|
|
* @since 3.8.9 |
|
48
|
|
|
* @return string |
|
49
|
|
|
*/ |
|
50
|
|
|
static private function _getLogContents( $mValue, $_fCurrentTimeStamp, $_fPreviousTimeStamp, $_sCallerClass, $_sCallerFunction ) { |
|
51
|
|
|
return self::_getLogHeadingLine( |
|
52
|
|
|
$_fCurrentTimeStamp, |
|
53
|
|
|
round( $_fCurrentTimeStamp - $_fPreviousTimeStamp, 3 ), // elapsed time |
|
54
|
|
|
$_sCallerClass, |
|
55
|
|
|
$_sCallerFunction |
|
56
|
|
|
) . PHP_EOL |
|
57
|
|
|
. self::_getLegibleDetails( $mValue ) . PHP_EOL . PHP_EOL; |
|
58
|
|
|
} |
|
59
|
|
|
/** |
|
60
|
|
|
* @since 3.8.9 |
|
61
|
|
|
* @return string |
|
62
|
|
|
*/ |
|
63
|
|
|
static private function _getCallerFunctionName( $oCallerInfo ) { |
|
64
|
|
|
return self::getElement( |
|
65
|
|
|
$oCallerInfo, // subject array |
|
66
|
|
|
array( 1, 'function' ), // key |
|
67
|
|
|
'' // default |
|
68
|
|
|
); |
|
69
|
|
|
} |
|
70
|
|
|
/** |
|
71
|
|
|
* @since 3.8.9 |
|
72
|
|
|
* @return string |
|
73
|
|
|
*/ |
|
74
|
|
|
static private function _getCallerClassName( $oCallerInfo ) { |
|
75
|
|
|
return self::getElement( |
|
76
|
|
|
$oCallerInfo, // subject array |
|
77
|
|
|
array( 1, 'class' ), // key |
|
78
|
|
|
'' // default |
|
79
|
|
|
); |
|
80
|
|
|
} |
|
81
|
|
|
/** |
|
82
|
|
|
* Determines the log file path. |
|
83
|
|
|
* @since 3.5.3 |
|
84
|
|
|
* @internal |
|
85
|
|
|
* @return string The path of the file to log the contents. |
|
86
|
|
|
*/ |
|
87
|
|
|
static private function _getLogFilePath( $bsFilePath, $sCallerClass ) { |
|
88
|
|
|
|
|
89
|
|
|
$_bFileExists = self::_createFile( $bsFilePath ); |
|
90
|
|
|
if ( $_bFileExists ) { |
|
91
|
|
|
return $bsFilePath; |
|
92
|
|
|
} |
|
93
|
|
|
// Return a generated default log path. |
|
94
|
|
|
if ( true === $bsFilePath ) { |
|
95
|
|
|
return WP_CONTENT_DIR . DIRECTORY_SEPARATOR . basename( get_class() ) . '_' . date( "Ymd" ) . '.log'; |
|
96
|
|
|
} |
|
97
|
|
|
return WP_CONTENT_DIR . DIRECTORY_SEPARATOR . basename( get_class() ) . '_' . basename( $sCallerClass ) . '_' . date( "Ymd" ) . '.log'; |
|
98
|
|
|
|
|
99
|
|
|
} |
|
100
|
|
|
/** |
|
101
|
|
|
* Creates a file. |
|
102
|
|
|
* @return boolean |
|
103
|
|
|
* @internal |
|
104
|
|
|
*/ |
|
105
|
|
|
static private function _createFile( $sFilePath ) { |
|
106
|
|
|
if ( ! $sFilePath || true === $sFilePath ) { |
|
107
|
|
|
return false; |
|
108
|
|
|
} |
|
109
|
|
|
if ( file_exists( $sFilePath ) ) { |
|
110
|
|
|
return true; |
|
111
|
|
|
} |
|
112
|
|
|
// Otherwise, create a file. |
|
113
|
|
|
$_bhResrouce = fopen( $sFilePath, 'w' ); |
|
114
|
|
|
return ( boolean ) $_bhResrouce; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Returns the heading part of a log item. |
|
119
|
|
|
* @since 3.5.3 |
|
120
|
|
|
* @internal |
|
121
|
|
|
* @return string the heading part of a log item. |
|
122
|
|
|
*/ |
|
123
|
|
|
static private function _getLogHeadingLine( $fCurrentTimeStamp, $nElapsed, $sCallerClass, $sCallerFunction ) { |
|
124
|
|
|
|
|
125
|
|
|
$_nNow = $fCurrentTimeStamp + ( self::_getSiteGMTOffset() * 60 * 60 ); |
|
126
|
|
|
$_nMicroseconds = str_pad( round( ( $_nNow - floor( $_nNow ) ) * 10000 ), 4, '0' ); |
|
127
|
|
|
$_aOutput = array( |
|
128
|
|
|
date( "Y/m/d H:i:s", $_nNow ) . '.' . $_nMicroseconds, |
|
129
|
|
|
self::_getFormattedElapsedTime( $nElapsed ), |
|
130
|
|
|
self::_getPageLoadID(), |
|
131
|
|
|
self::getFrameworkVersion(), |
|
132
|
|
|
$sCallerClass . '::' . $sCallerFunction, |
|
133
|
|
|
current_filter(), |
|
134
|
|
|
self::getCurrentURL(), |
|
135
|
|
|
); |
|
136
|
|
|
return implode( ' ', $_aOutput ); |
|
137
|
|
|
|
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Returns the GMT offset of the site. |
|
142
|
|
|
* |
|
143
|
|
|
* @return numeric |
|
144
|
|
|
*/ |
|
145
|
|
|
static private function _getSiteGMTOffset() { |
|
146
|
|
|
static $_nGMTOffset; |
|
147
|
|
|
$_nGMTOffset = isset( $_nGMTOffset ) |
|
148
|
|
|
? $_nGMTOffset |
|
149
|
|
|
: get_option( 'gmt_offset' ); |
|
150
|
|
|
return $_nGMTOffset; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* @return integer |
|
155
|
|
|
*/ |
|
156
|
|
|
static private function _getPageLoadID() { |
|
157
|
|
|
static $_iPageLoadID; |
|
158
|
|
|
$_iPageLoadID = $_iPageLoadID |
|
159
|
|
|
? $_iPageLoadID |
|
160
|
|
|
: uniqid(); |
|
161
|
|
|
return $_iPageLoadID; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Returns formatted elapsed time. |
|
166
|
|
|
* @since 3.5.3 |
|
167
|
|
|
* @internal |
|
168
|
|
|
* @return string Formatted elapsed time. |
|
169
|
|
|
*/ |
|
170
|
|
|
static private function _getFormattedElapsedTime( $nElapsed ) { |
|
171
|
|
|
|
|
172
|
|
|
$_aElapsedParts = explode( ".", ( string ) $nElapsed ); |
|
173
|
|
|
$_sElapsedFloat = str_pad( |
|
174
|
|
|
self::getElement( |
|
175
|
|
|
$_aElapsedParts, // subject array |
|
176
|
|
|
1, // key |
|
177
|
|
|
0 // default |
|
178
|
|
|
), |
|
179
|
|
|
3, |
|
180
|
|
|
'0' |
|
181
|
|
|
); |
|
182
|
|
|
$_sElapsed = self::getElement( |
|
183
|
|
|
$_aElapsedParts, // subject array |
|
184
|
|
|
0, // key |
|
185
|
|
|
0 // default |
|
186
|
|
|
); |
|
187
|
|
|
$_sElapsed = strlen( $_sElapsed ) > 1 |
|
188
|
|
|
? '+' . substr( $_sElapsed, -1, 2 ) |
|
189
|
|
|
: ' ' . $_sElapsed; |
|
190
|
|
|
return $_sElapsed . '.' . $_sElapsedFloat; |
|
191
|
|
|
|
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
} |
|
195
|
|
|
|