Plop
A simple logging library for PHP
WatchedFile.php
1 <?php
2 /*
3  This file is part of Plop, a simple logging library for PHP.
4 
5  Copyright © 2010-2014 François Poirotte
6 
7  Plop is free software: you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation, either version 3 of the License, or
10  (at your option) any later version.
11 
12  Plop is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with Plop. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 namespace Plop\Handler;
22 
30 {
32  protected $dev;
33 
35  protected $ino;
36 
38  public function __construct(
39  $filename,
40  $mode = 'at',
41  $delay = false
42  ) {
43  parent::__construct($filename, $mode, $delay);
44  if (!file_exists($filename)) {
45  $this->dev = $this->ino = -1;
46  } else {
47  $stats = stat($filename);
48  $this->dev = $stats['dev'];
49  $this->ino = $stats['ino'];
50  }
51  }
52 
54  protected function emit(\Plop\RecordInterface $record)
55  {
56  if (!file_exists($this->baseFilename)) {
57  $stats = null;
58  $changed = true;
59  } else {
60  $stats = stat($this->baseFilename);
61  $changed = (
62  ($stat['dev'] != $this->dev) ||
63  ($stat['ino'] != $this->ino)
64  );
65  }
66 
67  if ($changed && $this->stream !== false) {
68  if (is_resource($this->stream)) {
69  fflush($this->stream);
70  fclose($this->stream);
71  }
72  $this->open();
73  if (!$stats) {
74  $stats = stat($this->baseFilename);
75  }
76  $this->dev = $stats['dev'];
77  $this->ino = $stats['ino'];
78  }
79  parent::emit($record);
80  }
81 }
emit(\Plop\RecordInterface $record)
Definition: WatchedFile.php:54
$mode
Opening mode for the log file.
Definition: File.php:34
Interface for a log record.
An handler that writes log messages to a file.
Definition: File.php:28
$ino
Inode number of the watched file.
Definition: WatchedFile.php:35
An handler that logs to a file and automatically reopens that file when it changes (eg...
Definition: WatchedFile.php:29
$dev
Device the watched file resides on.
Definition: WatchedFile.php:32
__construct($filename, $mode= 'at', $delay=false)
Definition: WatchedFile.php:38