[347] | 1 | <?php
|
---|
| 2 | /**
|
---|
| 3 | * Html2Pdf Library
|
---|
| 4 | *
|
---|
| 5 | * HTML => PDF converter
|
---|
| 6 | * distributed under the OSL-3.0 License
|
---|
| 7 | *
|
---|
| 8 | * @package Html2pdf
|
---|
| 9 | * @author Laurent MINGUET <webmaster@html2pdf.fr>
|
---|
| 10 | * @copyright 2017 Laurent MINGUET
|
---|
| 11 | */
|
---|
| 12 |
|
---|
| 13 | namespace Spipu\Html2Pdf\Debug;
|
---|
| 14 |
|
---|
| 15 | /**
|
---|
| 16 | * Class Debug
|
---|
| 17 | */
|
---|
| 18 | class Debug implements DebugInterface
|
---|
| 19 | {
|
---|
| 20 | /**
|
---|
| 21 | * @var float
|
---|
| 22 | */
|
---|
| 23 | protected $startTime;
|
---|
| 24 |
|
---|
| 25 | /**
|
---|
| 26 | * @var float
|
---|
| 27 | */
|
---|
| 28 | protected $lastTime;
|
---|
| 29 |
|
---|
| 30 | /**
|
---|
| 31 | * @var int
|
---|
| 32 | */
|
---|
| 33 | protected $level = 0;
|
---|
| 34 |
|
---|
| 35 | /**
|
---|
| 36 | * @var bool
|
---|
| 37 | */
|
---|
| 38 | protected $htmlOutput;
|
---|
| 39 |
|
---|
| 40 | /**
|
---|
| 41 | * Debug constructor
|
---|
| 42 | *
|
---|
| 43 | * @param bool $htmlOutput
|
---|
| 44 | */
|
---|
| 45 | public function __construct($htmlOutput = true)
|
---|
| 46 | {
|
---|
| 47 | $this->htmlOutput = $htmlOutput;
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | /**
|
---|
| 51 | * display a debug line
|
---|
| 52 | *
|
---|
| 53 | * @param string $name
|
---|
| 54 | * @param string $timeTotal
|
---|
| 55 | * @param string $timeStep
|
---|
| 56 | * @param string $memoryUsage
|
---|
| 57 | * @param string $memoryPeak
|
---|
| 58 | *
|
---|
| 59 | * @return void
|
---|
| 60 | */
|
---|
| 61 | protected function displayLine($name, $timeTotal, $timeStep, $memoryUsage, $memoryPeak)
|
---|
| 62 | {
|
---|
| 63 | $output =
|
---|
| 64 | str_pad($name, 30, ' ', STR_PAD_RIGHT).
|
---|
| 65 | str_pad($timeTotal, 12, ' ', STR_PAD_LEFT).
|
---|
| 66 | str_pad($timeStep, 12, ' ', STR_PAD_LEFT).
|
---|
| 67 | str_pad($memoryUsage, 15, ' ', STR_PAD_LEFT).
|
---|
| 68 | str_pad($memoryPeak, 15, ' ', STR_PAD_LEFT);
|
---|
| 69 |
|
---|
| 70 | if ($this->htmlOutput) {
|
---|
| 71 | $output = '<pre style="padding:0; margin:0">'.$output.'</pre>';
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | echo $output."\n";
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | /**
|
---|
| 78 | * Start the debugger
|
---|
| 79 | *
|
---|
| 80 | * @return Debug
|
---|
| 81 | */
|
---|
| 82 | public function start()
|
---|
| 83 | {
|
---|
| 84 | $time = microtime(true);
|
---|
| 85 |
|
---|
| 86 | $this->startTime = $time;
|
---|
| 87 | $this->lastTime = $time;
|
---|
| 88 |
|
---|
| 89 | $this->displayLine('step', 'time', 'delta', 'memory', 'peak');
|
---|
| 90 | $this->addStep('Init debug');
|
---|
| 91 |
|
---|
| 92 | return $this;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | /**
|
---|
| 96 | * stop the debugger
|
---|
| 97 | *
|
---|
| 98 | * @return Debug
|
---|
| 99 | */
|
---|
| 100 | public function stop()
|
---|
| 101 | {
|
---|
| 102 | $this->addStep('Before output');
|
---|
| 103 | return $this;
|
---|
| 104 | }
|
---|
| 105 |
|
---|
| 106 | /**
|
---|
| 107 | * add a debug step
|
---|
| 108 | *
|
---|
| 109 | * @param string $name step name
|
---|
| 110 | * @param boolean $level (true=up, false=down, null=nothing to do)
|
---|
| 111 | *
|
---|
| 112 | * @return Debug
|
---|
| 113 | */
|
---|
| 114 | public function addStep($name, $level = null)
|
---|
| 115 | {
|
---|
| 116 | // if true : UP
|
---|
| 117 | if ($level === true) {
|
---|
| 118 | $this->level++;
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | $time = microtime(true);
|
---|
| 122 | $usage = memory_get_usage();
|
---|
| 123 | $peak = memory_get_peak_usage();
|
---|
| 124 | $type = ($level === true ? ' Begin' : ($level === false ? ' End' : ''));
|
---|
| 125 |
|
---|
| 126 | $this->displayLine(
|
---|
| 127 | str_repeat(' ', $this->level) . $name . $type,
|
---|
| 128 | number_format(($time - $this->startTime)*1000, 1, '.', ' ').' ms',
|
---|
| 129 | number_format(($time - $this->lastTime)*1000, 1, '.', ' ').' ms',
|
---|
| 130 | number_format($usage/1024, 1, '.', ' ').' Ko',
|
---|
| 131 | number_format($peak/1024, 1, '.', ' ').' Ko'
|
---|
| 132 | );
|
---|
| 133 |
|
---|
| 134 | $this->lastTime = $time;
|
---|
| 135 |
|
---|
| 136 | // it false : DOWN
|
---|
| 137 | if ($level === false) {
|
---|
| 138 | $this->level--;
|
---|
| 139 | }
|
---|
| 140 |
|
---|
| 141 | return $this;
|
---|
| 142 | }
|
---|
| 143 | }
|
---|