logbook/class/core/log/client/file.class.php

48 lines
1,004 B
PHP

<?php
namespace core\log\client;
/**
* 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(config: \core\config\Configuration $configuration, tag: \core\log\Tag $tag)
{
$this->configuration = $configuration;
$this->tag = $tag;
$path = $configuration.get('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);
}
}
?>