Passed
Push — master ( f10a02...88f473 )
by 世昌
01:41
created

Debug::assignAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 1
dl 0
loc 9
rs 10
c 0
b 0
f 0
1
<?php
2
namespace nebula\component\debug;
3
4
use nebula\component\debug\Caller;
5
use nebula\component\debug\ConfigTrait;
6
use nebula\component\debug\log\LogLevel;
7
use nebula\component\debug\ConfigInterface;
8
use nebula\component\debug\log\LoggerTrait;
9
use nebula\component\debug\attach\DumpTrait;
10
use nebula\component\debug\attach\AttachTrait;
11
use nebula\component\debug\log\LoggerInterface;
12
use nebula\component\debug\attach\DumpInterface;
13
use nebula\component\debug\log\LoggerAwareTrait;
14
use nebula\component\debug\attach\AttachInterface;
15
use nebula\component\debug\log\LoggerAwareInterface;
16
17
class Debug implements LoggerInterface, LoggerAwareInterface, DumpInterface, AttachInterface, ConfigInterface
18
{
19
    use LoggerTrait,LoggerAwareTrait,DumpTrait,AttachTrait,ConfigTrait;
20
21
    protected $times;
22
23
    public function log(string $level, string $message, array $context = [])
24
    {
25
        $attribute = [];
26
        $attribute['message'] = $this->strtr($message, $context);
27
        $attribute['level'] = $level;
28
        $caller = new Caller(debug_backtrace(), [__DIR__]);
29
        $trace = $caller->getCallerTrace();
30
        $attribute['file'] = $trace['file'];
31
        $attribute['line'] = $trace['line'];
32
        $attribute = $this->assignAttributes($attribute);
33
        $this->logger->log($level, $this->interpolate($this->getConfig('log-format'), $context, $attribute), []);
34
    }
35
36
    public function getDefaultConfig():array
37
    {
38
        return [
39
            'log-format' => '%time-format% - %memory-format% [%level%] %file%:%line% %message%',
40
            'start-time' => 0,
41
            'start-memory' => 0,
42
        ];
43
    }
44
45
    protected function strtr(string $message, array $context)
46
    {
47
        $replace = [];
48
        foreach ($context as $key => $val) {
49
            $replace['{' . $key . '}'] = $val;
50
        }
51
        return strtr($message, $replace);
52
    }
53
54
    protected function assignAttributes(array $attribute):array
55
    {
56
        $attribute['current-time'] = number_format(microtime(true), 4, '.', '');
57
        $time = microtime(true) - $this->getConfig('start-time');
58
        $memory = memory_get_usage() - $this->getConfig('start-memory');
59
        $attribute['time-format'] = number_format($time, 10, '.', '');
60
        $attribute['memory-format'] = $this->formatBytes($memory, 2);
61
        $attribute['memory'] = $memory;
62
        return $attribute;
63
    }
64
65
    protected static function formatBytes(int $bytes, int $precision=0)
66
    {
67
        $human= ['B', 'KB', 'MB', 'GB', 'TB'];
68
        $bytes = max($bytes, 0);
69
        $pow = floor(($bytes?log($bytes):0)/log(1024));
70
        $pos = min($pow, count($human)-1);
71
        $bytes /= (1 << (10* $pos));
72
        return round($bytes, $precision).' '.$human[$pos];
73
    }
74
75
    public function time(string $name, string $type= LogLevel::INFO)
76
    {
77
        $this->time[$name]=['time'=>microtime(true),'level'=>$type];
0 ignored issues
show
Bug Best Practice introduced by
The property time does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
78
    }
79
80
    public function timeEnd(string $name)
81
    {
82
        if (\array_key_exists($name, $this->time)) {
83
            $pass=microtime(true)-$this->time[$name]['time'];
84
            $this->log($this->time[$name]['level'], 'process cost '. $name.' '. number_format($pass, 5).'s');
85
            return true;
86
        }
87
        return false;
88
    }
89
}
90