forked from spipu/html2pdf
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDebug.php
More file actions
143 lines (123 loc) · 3.04 KB
/
Copy pathDebug.php
File metadata and controls
143 lines (123 loc) · 3.04 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
133
134
135
136
137
138
139
140
141
142
143
<?php
/**
* Html2Pdf Library
*
* HTML => PDF converter
* distributed under the OSL-3.0 License
*
* @package Html2pdf
* @author Laurent MINGUET <webmaster@html2pdf.fr>
* @copyright 2017 Laurent MINGUET
*/
namespace Spipu\Html2Pdf\Debug;
/**
* Class Debug
*/
class Debug implements DebugInterface
{
/**
* @var float
*/
protected $startTime;
/**
* @var float
*/
protected $lastTime;
/**
* @var int
*/
protected $level = 0;
/**
* @var bool
*/
protected $htmlOutput;
/**
* Debug constructor
*
* @param bool $htmlOutput
*/
public function __construct($htmlOutput = true)
{
$this->htmlOutput = $htmlOutput;
}
/**
* display a debug line
*
* @param string $name
* @param string $timeTotal
* @param string $timeStep
* @param string $memoryUsage
* @param string $memoryPeak
*
* @return void
*/
protected function displayLine($name, $timeTotal, $timeStep, $memoryUsage, $memoryPeak)
{
$output =
str_pad($name, 30, ' ', STR_PAD_RIGHT).
str_pad($timeTotal, 12, ' ', STR_PAD_LEFT).
str_pad($timeStep, 12, ' ', STR_PAD_LEFT).
str_pad($memoryUsage, 15, ' ', STR_PAD_LEFT).
str_pad($memoryPeak, 15, ' ', STR_PAD_LEFT);
if ($this->htmlOutput) {
$output = '<pre style="padding:0; margin:0">'.$output.'</pre>';
}
echo $output."\n";
}
/**
* Start the debugger
*
* @return Debug
*/
public function start()
{
$time = microtime(true);
$this->startTime = $time;
$this->lastTime = $time;
$this->displayLine('step', 'time', 'delta', 'memory', 'peak');
$this->addStep('Init debug');
return $this;
}
/**
* stop the debugger
*
* @return Debug
*/
public function stop()
{
$this->addStep('Before output');
return $this;
}
/**
* add a debug step
*
* @param string $name step name
* @param boolean $level (true=up, false=down, null=nothing to do)
*
* @return Debug
*/
public function addStep($name, $level = null)
{
// if true : UP
if ($level === true) {
$this->level++;
}
$time = microtime(true);
$usage = memory_get_usage();
$peak = memory_get_peak_usage();
$type = ($level === true ? ' Begin' : ($level === false ? ' End' : ''));
$this->displayLine(
str_repeat(' ', $this->level) . $name . $type,
number_format(($time - $this->startTime)*1000, 1, '.', ' ').' ms',
number_format(($time - $this->lastTime)*1000, 1, '.', ' ').' ms',
number_format($usage/1024, 1, '.', ' ').' Ko',
number_format($peak/1024, 1, '.', ' ').' Ko'
);
$this->lastTime = $time;
// it false : DOWN
if ($level === false) {
$this->level--;
}
return $this;
}
}