This repository has been archived by the owner on May 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Logging.php
83 lines (62 loc) · 2.17 KB
/
Logging.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
<?php
namespace Cyantree\Grout;
class Logging
{
private $_tracks = array();
public $file = 'log.txt';
public $maxFilesize = 1000000;
private $_measurements = array();
private $_measurementCounter = 0;
private $_startTime;
private $_id;
public function startMeasurement($id = null)
{
if ($id === null) {
$id = $this->_measurementCounter++;
}
$this->_measurements[$id] = microtime(true);
return $id;
}
public function stopMeasurement($id, $log)
{
self::log($log, microtime(true) - $this->_measurements[$id]);
unset($this->_measurements[$id]);
}
public function start($id = '_START_', $startTime = null)
{
$this->_id = mt_rand(1000, 9999);
$this->_startTime = $startTime ? $startTime : microtime(true);
self::log($id);
}
public function log($text, $duration = null, $time = null)
{
array_push($this->_tracks, $time ? $time : microtime(true), $text, $duration);
}
public function stop($text = '_STOP_')
{
self::log($text);
$start = $this->_startTime;
$count = (count($this->_tracks) / 3) >> 0;
$i = 0;
if ($this->maxFilesize && file_exists($this->file) && filesize($this->file) > $this->maxFilesize) {
$f = fopen($this->file, 'w');
} else {
$f = fopen($this->file, 'a');
}
fwrite($f, $this->_id . ': ' . date('Y-m-d H:i:s', $this->_startTime) . ' - ' . $this->_tracks[1] . chr(10));
while ($i++ < $count - 1) {
$duration = $this->_tracks[3 * $i + 2];
if (!$duration) {
$duration = $this->_tracks[3 * $i] - $this->_tracks[3 * ($i - 1)];
$durationFlag = 'm';
} else $durationFlag = 'c';
fwrite($f, $this->_id . ': ' . self::_formatTime($duration) . $durationFlag . ': ' . self::_formatTime($this->_tracks[3 * $i] - $start) . ': ' . $this->_tracks[3 * $i + 1] . chr(10));
}
fwrite($f, chr(10));
fclose($f);
}
private function _formatTime($time)
{
return str_pad(((($time * 10000) >> 0) / 10000), 7, '0');
}
}