82c048e1c82ae28798e63a289c830717e04cf101
[friendica-addons.git/.git] / monolog / vendor / monolog / monolog / src / Monolog / Handler / StreamHandler.php
1 <?php declare(strict_types=1);
2
3 /*
4  * This file is part of the Monolog package.
5  *
6  * (c) Jordi Boggiano <j.boggiano@seld.be>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11
12 namespace Monolog\Handler;
13
14 use Monolog\Logger;
15 use Monolog\Utils;
16
17 /**
18  * Stores to any stream resource
19  *
20  * Can be used to store into php://stderr, remote and local files, etc.
21  *
22  * @author Jordi Boggiano <j.boggiano@seld.be>
23  *
24  * @phpstan-import-type FormattedRecord from AbstractProcessingHandler
25  */
26 class StreamHandler extends AbstractProcessingHandler
27 {
28     /** @const int */
29     protected const MAX_CHUNK_SIZE = 2147483647;
30     /** @const int 10MB */
31     protected const DEFAULT_CHUNK_SIZE = 10 * 1024 * 1024;
32     /** @var int */
33     protected $streamChunkSize;
34     /** @var resource|null */
35     protected $stream;
36     /** @var ?string */
37     protected $url = null;
38     /** @var ?string */
39     private $errorMessage = null;
40     /** @var ?int */
41     protected $filePermission;
42     /** @var bool */
43     protected $useLocking;
44     /** @var true|null */
45     private $dirCreated = null;
46
47     /**
48      * @param resource|string $stream         If a missing path can't be created, an UnexpectedValueException will be thrown on first write
49      * @param int|null        $filePermission Optional file permissions (default (0644) are only for owner read/write)
50      * @param bool            $useLocking     Try to lock log file before doing any writes
51      *
52      * @throws \InvalidArgumentException If stream is not a resource or string
53      */
54     public function __construct($stream, $level = Logger::DEBUG, bool $bubble = true, ?int $filePermission = null, bool $useLocking = false)
55     {
56         parent::__construct($level, $bubble);
57
58         if (($phpMemoryLimit = Utils::expandIniShorthandBytes(ini_get('memory_limit'))) !== false) {
59             if ($phpMemoryLimit > 0) {
60                 // use max 10% of allowed memory for the chunk size, and at least 100KB
61                 $this->streamChunkSize = min(static::MAX_CHUNK_SIZE, max((int) ($phpMemoryLimit / 10), 100 * 1024));
62             } else {
63                 // memory is unlimited, set to the default 10MB
64                 $this->streamChunkSize = static::DEFAULT_CHUNK_SIZE;
65             }
66         } else {
67             // no memory limit information, set to the default 10MB
68             $this->streamChunkSize = static::DEFAULT_CHUNK_SIZE;
69         }
70
71         if (is_resource($stream)) {
72             $this->stream = $stream;
73
74             stream_set_chunk_size($this->stream, $this->streamChunkSize);
75         } elseif (is_string($stream)) {
76             $this->url = Utils::canonicalizePath($stream);
77         } else {
78             throw new \InvalidArgumentException('A stream must either be a resource or a string.');
79         }
80
81         $this->filePermission = $filePermission;
82         $this->useLocking = $useLocking;
83     }
84
85     /**
86      * {@inheritDoc}
87      */
88     public function close(): void
89     {
90         if ($this->url && is_resource($this->stream)) {
91             fclose($this->stream);
92         }
93         $this->stream = null;
94         $this->dirCreated = null;
95     }
96
97     /**
98      * Return the currently active stream if it is open
99      *
100      * @return resource|null
101      */
102     public function getStream()
103     {
104         return $this->stream;
105     }
106
107     /**
108      * Return the stream URL if it was configured with a URL and not an active resource
109      *
110      * @return string|null
111      */
112     public function getUrl(): ?string
113     {
114         return $this->url;
115     }
116
117     /**
118      * @return int
119      */
120     public function getStreamChunkSize(): int
121     {
122         return $this->streamChunkSize;
123     }
124
125     /**
126      * {@inheritDoc}
127      */
128     protected function write(array $record): void
129     {
130         if (!is_resource($this->stream)) {
131             $url = $this->url;
132             if (null === $url || '' === $url) {
133                 throw new \LogicException('Missing stream url, the stream can not be opened. This may be caused by a premature call to close().' . Utils::getRecordMessageForException($record));
134             }
135             $this->createDir($url);
136             $this->errorMessage = null;
137             set_error_handler([$this, 'customErrorHandler']);
138             try {
139                 $stream = fopen($url, 'a');
140                 if ($this->filePermission !== null) {
141                     @chmod($url, $this->filePermission);
142                 }
143             } finally {
144                 restore_error_handler();
145             }
146             if (!is_resource($stream)) {
147                 $this->stream = null;
148
149                 throw new \UnexpectedValueException(sprintf('The stream or file "%s" could not be opened in append mode: '.$this->errorMessage, $url) . Utils::getRecordMessageForException($record));
150             }
151             stream_set_chunk_size($stream, $this->streamChunkSize);
152             $this->stream = $stream;
153         }
154
155         $stream = $this->stream;
156         if (!is_resource($stream)) {
157             throw new \LogicException('No stream was opened yet' . Utils::getRecordMessageForException($record));
158         }
159
160         if ($this->useLocking) {
161             // ignoring errors here, there's not much we can do about them
162             flock($stream, LOCK_EX);
163         }
164
165         $this->streamWrite($stream, $record);
166
167         if ($this->useLocking) {
168             flock($stream, LOCK_UN);
169         }
170     }
171
172     /**
173      * Write to stream
174      * @param resource $stream
175      * @param array    $record
176      *
177      * @phpstan-param FormattedRecord $record
178      */
179     protected function streamWrite($stream, array $record): void
180     {
181         fwrite($stream, (string) $record['formatted']);
182     }
183
184     private function customErrorHandler(int $code, string $msg): bool
185     {
186         $this->errorMessage = preg_replace('{^(fopen|mkdir)\(.*?\): }', '', $msg);
187
188         return true;
189     }
190
191     private function getDirFromStream(string $stream): ?string
192     {
193         $pos = strpos($stream, '://');
194         if ($pos === false) {
195             return dirname($stream);
196         }
197
198         if ('file://' === substr($stream, 0, 7)) {
199             return dirname(substr($stream, 7));
200         }
201
202         return null;
203     }
204
205     private function createDir(string $url): void
206     {
207         // Do not try to create dir if it has already been tried.
208         if ($this->dirCreated) {
209             return;
210         }
211
212         $dir = $this->getDirFromStream($url);
213         if (null !== $dir && !is_dir($dir)) {
214             $this->errorMessage = null;
215             set_error_handler([$this, 'customErrorHandler']);
216             $status = mkdir($dir, 0777, true);
217             restore_error_handler();
218             if (false === $status && !is_dir($dir) && strpos((string) $this->errorMessage, 'File exists') === false) {
219                 throw new \UnexpectedValueException(sprintf('There is no existing directory at "%s" and it could not be created: '.$this->errorMessage, $dir));
220             }
221         }
222         $this->dirCreated = true;
223     }
224 }