forked from melvincarvalho/foafme
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Logger.php
68 lines (60 loc) · 1.95 KB
/
Logger.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
<?php
/**
* Description of Logger
*
* @author laczoka
*/
class Logger {
const DEBUG_MODE = 'debug_mode';
const DEBUG_OUT_DIR = 'debug_out_dir';
private $enabled;
private $directory_output;
private $logfile;
private $loghandle;
private $lastmsgts = 0;
private $messages = array();
private static $instance = NULL;
public function __construct($dir_out = '/tmp/', $enabled = false) {
$this->enabled = $enabled;
$this->directory_output = $dir_out;
$ts = time();
$logfilebase = $__SERVER['SERVER_NAME'] ? $__SERVER['SERVER_NAME'] : 'perflog';
$this->logfile = $this->directory_output.$logfilebase.'.'.$ts.'.log';
$this->loghandle = fopen($this->logfile, 'w');
}
public function logWithTs($message)
{
if ($this->enabled) {
$ts = microtime(true);
$time_elapsed = $this->lastmsgts ? ($ts - $this->lastmsgts) : 0 ;
$this->lastmsgts = $ts;
$this->messages[] = array(
'text'=> $message,
'ts' => $time_elapsed
);
}
}
public function __destruct() {
foreach ($this->messages as $msg) {
fwrite($this->loghandle, $msg['text']."\t\t".((float)$msg['ts']*1000)." msec\n");
}
fclose($this->loghandle);
}
public static function getLogger() {
if (NULL == self::$instance) {
self::$instance = new Logger(
$GLOBALS['config'][self::DEBUG_OUT_DIR],
$GLOBALS['config'][self::DEBUG_MODE]);
}
return self::$instance;
}
private function diff_microtime($mt_old,$mt_new)
{
list($old_usec, $old_sec) = explode(' ',$mt_old);
list($new_usec, $new_sec) = explode(' ',$mt_new);
$old_mt = ((float)$old_usec + (float)$old_sec);
$new_mt = ((float)$new_usec + (float)$new_sec);
return $new_mt - $old_mt;
}
}
?>