Completed
Push — master ( 526d6e...2376dc )
by Akihito
03:04
created

Log::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
namespace Ackintosh\Snidel;
3
4
class Log
5
{
6
    /** @var int */
7
    private $ownerPid;
8
9
    /** @var resource */
10
    private $destination;
11
12
    /**
13
     * @param   int     $ownerPid
14
     */
15
    public function __construct($ownerPid)
16
    {
17
        $this->ownerPid = $ownerPid;
18
    }
19
20
    /**
21
     * sets the resource for the log.
22
     *
23
     * @param   resource    $resource
24
     * @return  void
25
     */
26
    public function setDestination($resource)
27
    {
28
        $this->destination = $resource;
29
    }
30
31
    /**
32
     * writes log
33
     *
34
     * @param   string  $type
35
     * @param   string  $message
36
     * @return  void
37
     */
38
    private function write($type, $message)
39
    {
40
        if ($this->destination === null) {
41
            return;
42
        }
43
        $pid = getmypid();
44
        fputs(
45
            $this->destination,
46
            sprintf(
47
                '[%s][%s][%d(%s)] %s',
48
                date('Y-m-d H:i:s'),
49
                $type,
50
                $pid,
51
                ($this->ownerPid === $pid) ? 'p' : 'c',
52
                $message . PHP_EOL
53
            )
54
        );
55
    }
56
57
    /**
58
     * writes log
59
     *
60
     * @param   string  $message
61
     * @return  void
62
     */
63
    public function info($message)
64
    {
65
        $this->write('info', $message);
66
    }
67
68
    /**
69
     * writes log
70
     *
71
     * @param   string  $message
72
     * @return  void
73
     */
74
    public function error($message)
75
    {
76
        $this->write('error', $message);
77
    }
78
}
79