class-debug.php
2.15 KB
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
<?php
class WPF_CRM_Debug {
/**
* Holds the current active CRM object
*/
private $crm;
/**
* Whether or not a session has started
*/
private $started;
/**
* Start time for the current session
*/
private $session_start_time;
public function __construct($crm) {
$this->crm = $crm;
$this->started = false;
add_action( 'shutdown', array( $this, 'shutdown' ), 20 );
}
/**
* Passes get requests to the base CRM class
*
* @access public
* @return void
*/
public function __get($name) {
return $this->crm->$name;
}
/**
* Calls CRM class methods and logs result
*
* @access public
* @return void
*/
public function __call( $method, $args ) {
if($this->started == false)
$this->session_start();
$start = microtime(true);
$result = call_user_func_array(array($this->crm, $method), $args);
$elapsed = microtime(true) - $start;
$this->write_log( 'Method: ' . $method . ' with args: ' . print_r($args, true) . 'Time elapsed: ' . $elapsed . '.' );
return $result;
}
/**
* Logs session start time
*
* @access private
* @return void
*/
private function session_start() {
$this->session_start_time = microtime(true);
$this->started = true;
$this->write_log('New session.');
}
/**
* Writes final messages to log on system shutdown
*
* @access public
* @return void
*/
public function shutdown() {
if($this->started == true) {
$elapsed = microtime(true) - $this->session_start_time;
$this->write_log('Session closed. Elapsed time ' . $elapsed);
}
}
/**
* Writes log messages
*
* @access private
* @return void
*/
private function write_log( $message ) {
$logfile = WP_CONTENT_DIR . '/wp-fusion.log';
// Get time of request
if( ($time = $_SERVER['REQUEST_TIME']) == '') {
$time = time();
}
// Get IP address
if( ($remote_addr = $_SERVER['REMOTE_ADDR']) == '') {
$remote_addr = "REMOTE_ADDR_UNKNOWN";
}
// Format the date and time
$date = date("Y-m-d H:i:s", $time);
// Append to the log file
if($fd = @fopen($logfile, "a")) {
$result = fputcsv($fd, array($date, $remote_addr, $message));
fclose($fd);
}
}
}