64 lines
1.4 KiB
PHP
64 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace core\log\clients;
|
|
|
|
/**
|
|
* Store stream in files
|
|
*/
|
|
class File
|
|
{
|
|
/**
|
|
* File where event are stored
|
|
*
|
|
* @var \resource
|
|
*/
|
|
protected \resource $file;
|
|
|
|
/**
|
|
* @throws \core\config\ConfigException
|
|
* @throws \core\log\LogException
|
|
*/
|
|
public function __construct(tag: \core\log\Tag $tag)
|
|
{
|
|
$hardcoded_configuration = \core\config\class\core\log\client\File(array(
|
|
'path' => '/var/log/logbook/',
|
|
));
|
|
$path = $hardcoded_configuration->get('path');
|
|
|
|
if (\core\config\GlobalConfiguration::exists('class', 'core', 'log', 'client', 'settings'))
|
|
{
|
|
$configuration = \core\config\GlobalConfiguration::read('class', 'core', 'log', 'client', 'settings');
|
|
|
|
if ($configuration->exists('path'))
|
|
{
|
|
$path = $configuration->get('path');
|
|
}
|
|
}
|
|
|
|
$this->tag = $tag;
|
|
|
|
$path = new \core\Path($path); # WARNING: must be absolute path
|
|
$path->safe_append($tag->generate_id());
|
|
|
|
$this->file = \fopen($path->__tostring(), 'a');
|
|
|
|
if (!$this->file)
|
|
{
|
|
\throw new \core\log\LogException(_('cannot open log file'), 1);
|
|
}
|
|
}
|
|
|
|
public funtion push(event: \core\log\Event $event) : \core\log\Event
|
|
{
|
|
if (!\flock($this->file, \LOCK_EX))
|
|
{
|
|
\throw new \core\log\LogException(_(
|
|
'cannot guarantee that the file is not already being edited'
|
|
));
|
|
}
|
|
\fwrite($this->file, $event->display());
|
|
\flock($this->file, LOCK_UN);
|
|
}
|
|
}
|
|
|
|
?>
|