1
<?php
2
/**
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the LGPL. For more information please see
17
 * <http://phing.info>.
18
 */
19

20
/**
21
 * This is a special logger that is designed to profile builds.
22
 *
23
 * @author  Siad Ardroumli <siad.ardroumli@gmail.com>
24
 * @package phing.listener
25
 */
26
class ProfileLogger extends DefaultLogger
27
{
28
    private $profileData = [];
29

30
    protected static $dateFormat = DATE_RFC2822;
31

32
    /**
33
     * Logs a message to say that the target has started.
34
     *
35
     * @param BuildEvent $event
36
     *            An event with any relevant extra information. Must not be
37
     *            <code>null</code>.
38
     */
39 0
    public function targetStarted(BuildEvent $event)
40
    {
41 0
        if (@date_default_timezone_get() === 'UTC') {
42 0
            date_default_timezone_set('Europe/Berlin');
43
        }
44 0
        $now = Phing::currentTimeMillis();
45 0
        $name = "Target " . $event->getTarget()->getName();
46 0
        $this->logStart($event, $now, $name);
47 0
        $this->profileData[] = $now;
48
    }
49

50
    /**
51
     * Logs a message to say that the target has finished.
52
     *
53
     * @param BuildEvent $event
54
     *            An event with any relevant extra information. Must not be
55
     *            <code>null</code>.
56
     */
57 0
    public function targetFinished(BuildEvent $event)
58
    {
59 0
        $start = array_pop($this->profileData);
60

61 0
        $name = "Target " . $event->getTarget()->getName();
62 0
        $this->logFinish($event, $start, $name);
63
    }
64

65
    /**
66
     * Logs a message to say that the task has started.
67
     *
68
     * @param BuildEvent $event
69
     *            An event with any relevant extra information. Must not be
70
     *            <code>null</code>.
71
     */
72 0
    public function taskStarted(BuildEvent $event)
73
    {
74 0
        $name = $event->getTask()->getTaskName();
75 0
        $now = Phing::currentTimeMillis();
76 0
        $this->logStart($event, $now, $name);
77 0
        $this->profileData[] = $now;
78
    }
79

80
    /**
81
     * Logs a message to say that the task has finished.
82
     *
83
     * @param BuildEvent $event
84
     *            An event with any relevant extra information. Must not be
85
     *            <code>null</code>.
86
     */
87 0
    public function taskFinished(BuildEvent $event)
88
    {
89 0
        $start = array_pop($this->profileData);
90

91 0
        $name = $event->getTask()->getTaskName();
92 0
        $this->logFinish($event, $start, $name);
93
    }
94

95 0
    private function logFinish(BuildEvent $event, $start, $name)
96
    {
97 0
        $msg = null;
98 0
        if ($start != null) {
99 0
            $diff = self::formatTime(Phing::currentTimeMillis() - $start);
100 0
            $msg = Phing::getProperty("line.separator") . $name . ": finished "
101 0
                . date(self::$dateFormat, time()) . " ("
102 0
                . $diff
103 0
                . ")";
104 0
        } else {
105 0
            $msg = Phing::getProperty("line.separator") . $name . ": finished " . date(self::$dateFormat, time())
106 0
                . " (unknown duration, start not detected)";
107
        }
108 0
        $this->printMessage($msg, $this->out, $event->getPriority());
109
    }
110

111 0
    private function logStart(BuildEvent $event, $start, $name)
112
    {
113 0
        $msg = Phing::getProperty("line.separator") . $name . ": started " . date(self::$dateFormat, $start);
114 0
        $this->printMessage($msg, $this->out, $event->getPriority());
115
    }
116
}

Read our documentation on viewing source code .

Loading