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 class that represents a recorder. This is the listener to the
22
 * build process.
23
 *
24
 * @author  Siad Ardroumli <siad.ardroumli@gmail.com>
25
 * @package phing.tasks.system
26
 */
27
class RecorderEntry implements BuildLogger, SubBuildListener
28
{
29
    /**
30
     * The name of the file associated with this recorder entry.
31
     *
32
     * @var string $filename
33
     */
34
    private $filename = null;
35

36
    /**
37
     * The state of the recorder (recorder on or off).
38
     *
39
     * @var bool $record
40
     */
41
    private $record = true;
42

43
    /**
44
     * The current verbosity level to record at.
45
     */
46
    private $loglevel;
47

48
    /**
49
     * The output OutputStream to record to.
50
     *
51
     * @var OutputStream $out
52
     */
53
    private $out = null;
54

55
    /**
56
     * The start time of the last know target.
57
     */
58
    private $targetStartTime;
59

60
    /**
61
     * Strip task banners if true.
62
     */
63
    private $emacsMode = false;
64

65
    /**
66
     * project instance the recorder is associated with
67
     *
68
     * @var Project $project
69
     */
70
    private $project;
71

72
    /**
73
     * @param string $name The name of this recorder (used as the filename).
74
     */
75 1
    public function __construct($name)
76
    {
77 1
        $this->targetStartTime = Phing::currentTimeMillis();
78 1
        $this->filename = $name;
79 1
        $this->loglevel = Project::MSG_INFO;
80
    }
81

82
    /**
83
     * @return string the name of the file the output is sent to.
84
     */
85 0
    public function getFilename()
86
    {
87 0
        return $this->filename;
88
    }
89

90
    /**
91
     * Turns off or on this recorder.
92
     *
93
     * @param bool|null state true for on, false for off, null for no change.
94
     */
95 1
    public function setRecordState($state)
96
    {
97 1
        if ($state != null) {
98 1
            $this->flush();
99 1
            $this->record = StringHelper::booleanValue($state);
100
        }
101
    }
102

103
    /**
104
     * {@inheritDoc}.
105
     */
106 0
    public function buildStarted(BuildEvent $event)
107
    {
108 0
        $this->log("> BUILD STARTED", Project::MSG_DEBUG);
109
    }
110

111
    /**
112
     * {@inheritDoc}.
113
     */
114 0
    public function buildFinished(BuildEvent $event)
115
    {
116 0
        $this->log("< BUILD FINISHED", Project::MSG_DEBUG);
117

118 0
        if ($this->record && $this->out != null) {
119 0
            $error = $event->getException();
120

121 0
            if ($error == null) {
122 0
                $this->out->write(Phing::getProperty('line.separator') . "BUILD SUCCESSFUL" . PHP_EOL);
123
            } else {
124 0
                $this->out->write(
125 0
                    Phing::getProperty('line.separator') . "BUILD FAILED"
126 0
                    . Phing::getProperty('line.separator') . PHP_EOL
127
                );
128 0
                $this->out->write($error->getTraceAsString());
129
            }
130
        }
131 0
        $this->cleanup();
132
    }
133

134
    /**
135
     * Cleans up any resources held by this recorder entry at the end
136
     * of a subbuild if it has been created for the subbuild's project
137
     * instance.
138
     *
139
     * @param BuildEvent $event the buildFinished event
140
     */
141 0
    public function subBuildFinished(BuildEvent $event)
142
    {
143 0
        if ($event->getProject() == $this->project) {
144 0
            $this->cleanup();
145
        }
146
    }
147

148
    /**
149
     * Empty implementation to satisfy the BuildListener interface.
150
     *
151
     * @param BuildEvent $event the buildStarted event
152
     */
153 0
    public function subBuildStarted(BuildEvent $event)
154
    {
155
    }
156

157
    /**
158
     * {@inheritDoc}.
159
     */
160 1
    public function targetStarted(BuildEvent $event)
161
    {
162 1
        $this->log(">> TARGET STARTED -- " . $event->getTarget()->getName(), Project::MSG_DEBUG);
163 1
        $this->log(
164 1
            Phing::getProperty('line.separator') . $event->getTarget()->getName() . ":",
165 1
            Project::MSG_INFO
166
        );
167 1
        $this->targetStartTime = Phing::currentTimeMillis();
168
    }
169

170
    /**
171
     * {@inheritDoc}.
172
     */
173 1
    public function targetFinished(BuildEvent $event)
174
    {
175 1
        $this->log("<< TARGET FINISHED -- " . $event->getTarget()->getName(), Project::MSG_DEBUG);
176

177 1
        $time = DefaultLogger::formatTime(Phing::currentTimeMillis() - $this->targetStartTime);
178

179 1
        $this->log($event->getTarget()->getName() . ":  duration " . $time, Project::MSG_VERBOSE);
180 1
        flush();
181
    }
182

183
    /**
184
     * {@inheritDoc}.
185
     */
186 1
    public function taskStarted(BuildEvent $event)
187
    {
188 1
        $this->log(">>> TASK STARTED -- " . $event->getTask()->getTaskName(), Project::MSG_DEBUG);
189
    }
190

191
    /**
192
     * {@inheritDoc}.
193
     */
194 1
    public function taskFinished(BuildEvent $event)
195
    {
196 1
        $this->log("<<< TASK FINISHED -- " . $event->getTask()->getTaskName(), Project::MSG_DEBUG);
197 1
        $this->flush();
198
    }
199

200
    /**
201
     * {@inheritDoc}.
202
     */
203 1
    public function messageLogged(BuildEvent $event)
204
    {
205 1
        $this->log("--- MESSAGE LOGGED", Project::MSG_DEBUG);
206

207 1
        $buf = '';
208

209 1
        if ($event->getTask() != null) {
210 1
            $name = $event->getTask()->getTaskName();
211

212 1
            if (!$this->emacsMode) {
213 1
                $label = "[" . $name . "] ";
214 1
                $size = DefaultLogger::LEFT_COLUMN_SIZE - strlen($label);
215

216 1
                for ($i = 0; $i < $size; $i++) {
217 1
                    $buf .= " ";
218
                }
219 1
                $buf .= $label;
220
            }
221
        }
222 1
        $buf .= $event->getMessage();
223

224 1
        $this->log($buf, $event->getPriority());
225
    }
226

227

228
    /**
229
     * The thing that actually sends the information to the output.
230
     *
231
     * @param string $mesg The message to log.
232
     * @param int $level The verbosity level of the message.
233
     */
234 1
    private function log($mesg, $level)
235
    {
236 1
        if ($this->record && ($level <= $this->loglevel) && $this->out != null) {
237 1
            $this->out->write($mesg . PHP_EOL);
238
        }
239
    }
240

241 1
    private function flush()
242
    {
243 1
        if ($this->record && $this->out != null) {
244 1
            $this->out->flush();
245
        }
246
    }
247

248
    /**
249
     * {@inheritDoc}.
250
     */
251 1
    public function setMessageOutputLevel($level)
252
    {
253 1
        if ($level >= Project::MSG_ERR && $level <= Project::MSG_DEBUG) {
254 0
            $this->loglevel = $level;
255
        }
256
    }
257

258
    /**
259
     * {@inheritDoc}.
260
     */
261 0
    public function setOutputStream(OutputStream $output)
262
    {
263 0
        $this->closeFile();
264 0
        $this->out = $output;
265
    }
266

267
    /**
268
     * {@inheritDoc}.
269
     */
270 1
    public function setEmacsMode($emacsMode)
271
    {
272 1
        $this->emacsMode = $emacsMode;
273
    }
274

275
    /**
276
     * {@inheritDoc}.
277
     */
278 0
    public function setErrorStream(OutputStream $err)
279
    {
280 0
        $this->setOutputStream($err);
281
    }
282

283
    /**
284
     * Set the project associated with this recorder entry.
285
     *
286
     * @param Project $project the project instance
287
     */
288 1
    public function setProject(Project $project)
289
    {
290 1
        $this->project = $project;
291 1
        if ($this->project != null) {
292 1
            $this->project->addBuildListener($this);
293
        }
294
    }
295

296
    /**
297
     * Get the project associated with this recorder entry.
298
     */
299 0
    public function getProject()
300
    {
301 0
        return $this->project;
302
    }
303

304 0
    public function cleanup()
305
    {
306 0
        $this->closeFile();
307 0
        if ($this->project != null) {
308 0
            $this->project->removeBuildListener($this);
309
        }
310 0
        $this->project = null;
311
    }
312

313
    /**
314
     * Initially opens the file associated with this recorder.
315
     * Used by Recorder.
316
     *
317
     * @param  bool $append Indicates if output must be appended to the logfile or that
318
     * the logfile should be overwritten.
319
     * @throws BuildException
320
     */
321 1
    public function openFile($append)
322
    {
323 1
        $this->openFileImpl($append);
324
    }
325

326
    /**
327
     * Closes the file associated with this recorder.
328
     * Used by Recorder.
329
     */
330 0
    public function closeFile()
331
    {
332 0
        if ($this->out != null) {
333 0
            $this->out->close();
334 0
            $this->out = null;
335
        }
336
    }
337

338
    /**
339
     * Re-opens the file associated with this recorder.
340
     * Used by Recorder.
341
     *
342
     * @throws BuildException
343
     */
344 1
    public function reopenFile()
345
    {
346 1
        $this->openFileImpl(true);
347
    }
348

349 1
    private function openFileImpl($append)
350
    {
351 1
        if ($this->out == null) {
352
            try {
353 1
                $this->out = new FileOutputStream($this->filename, $append);
354 0
            } catch (IOException $ioe) {
355 0
                throw new BuildException("Problems opening file using a recorder entry", $ioe);
356
            }
357
        }
358
    }
359
}

Read our documentation on viewing source code .

Loading