rosavox/lib/alveolata/log/functions.php
2025-05-23 07:33:29 +00:00

245 lines
4.6 KiB
PHP

<?php
namespace alveolata\log;
// require_once(DIR_ALVEOLATA . '/definitions.php');
require_once(DIR_ALVEOLATA . '/report/functions.php');
require_once(DIR_ALVEOLATA . '/log/base.php');
require_once(DIR_ALVEOLATA . '/log/output-interface.php');
require_once(DIR_ALVEOLATA . '/log/output-implementation-restricted.php');
require_once(DIR_ALVEOLATA . '/log/output-implementation-console.php');
require_once(DIR_ALVEOLATA . '/log/output-implementation-file.php');
require_once(DIR_ALVEOLATA . '/log/output-implementation-email.php');
require_once(DIR_ALVEOLATA . '/log/output-implementation-libnotify.php');
/**
* @var array list of interface_output
* @author Christian Fraß <frass@greenscale.de>
*/
class _state
{
public static $outputs = [];
}
/**
* @author Christian Fraß <frass@greenscale.de>
*/
function add_output(
interface_output $output
) : void
{
array_push(_state::$outputs, $output);
}
/**
* @author Christian Fraß <frass@greenscale.de>
*/
function _submit(
int $level,
\alveolata\report\struct_report $report
) : void
{
foreach (_state::$outputs as $output) {
$output->process($level, $report);
}
}
/**
* @param \alveolata\report\type $report
* @author Christian Fraß <frass@greenscale.de>
*/
function error_(
\alveolata\report\struct_report $report
) : void
{
_submit(enum_level::error, $report);
}
/**
* @param string $incident
* @param map<string,any> [$details]
* @author Christian Fraß <frass@greenscale.de>
*/
function error(
string $incident,
array $details = []
) : void
{
error_(\alveolata\report\make($incident, $details));
}
/**
* @param \alveolata\report\type $report
* @author Christian Fraß <frass@greenscale.de>
*/
function warning_(
\alveolata\report\struct_report $report
) : void
{
_submit(enum_level::warning, $report);
}
/**
* @param string $incident
* @param map<string,any> [$details]
* @author Christian Fraß <frass@greenscale.de>
*/
function warning(
string $incident,
array $details = []
) : void
{
warning_(\alveolata\report\make($incident, $details));
}
/**
* @param \alveolata\report\type $report
* @author Christian Fraß <frass@greenscale.de>
*/
function notice_(
\alveolata\report\struct_report $report
) : void
{
_submit(enum_level::notice, $report);
}
/**
* @param string $incident
* @param map<string,any> [$details]
* @author Christian Fraß <frass@greenscale.de>
*/
function notice(
string $incident,
array $details = []
) : void
{
notice_(\alveolata\report\make($incident, $details));
}
/**
* @param \alveolata\report\type $report
* @author Christian Fraß <frass@greenscale.de>
*/
function info_(
\alveolata\report\struct_report $report
) : void
{
_submit(enum_level::info, $report);
}
/**
* @param string $incident
* @param map<string,any> [$details]
* @author Christian Fraß <frass@greenscale.de>
*/
function info(
string $incident,
array $details = []
) : void
{
info_(\alveolata\report\make($incident, $details));
}
/**
* @param \alveolata\report\type $report
* @author Christian Fraß <frass@greenscale.de>
*/
function debug_(
\alveolata\report\struct_report $report
) : void
{
_submit(enum_level::debug, $report);
}
/**
* @param string $incident
* @param map<string,any> [$details]
* @author Christian Fraß <frass@greenscale.de>
*/
function debug(
string $incident,
array $details = []
) : void
{
debug_(\alveolata\report\make($incident, $details));
}
/**
* @author Christian Fraß <frass@greenscale.de>
*/
function make_output(
string $kind,
array $parameters
) : interface_output
{
switch ($kind) {
case 'console': {
return (
new implementation_restricted(
new implementation_console(),
level_decode($parameters['level_threshold'] ?? 'notice')
)
);
break;
}
case 'file': {
return (
new implementation_restricted(
new implementation_file(
$parameters['path'],
[
'human_readable' => ($parameters['human_readable'] ?? false),
]
),
level_decode($parameters['level_threshold'] ?? 'notice')
)
);
break;
}
case 'email': {
return (
new implementation_restricted(
new implementation_email(
$parameters['auth'],
$parameters['receivers'],
$parameters['sender'],
$parameters['tags'],
$parameters['implementation']
),
level_decode($parameters['level_threshold'] ?? 'notice')
)
);
break;
}
case 'libnotify': {
return (
new implementation_restricted(
new implementation_libnotify(),
level_decode($parameters['level_threshold'] ?? 'notice')
)
);
break;
}
default: {
throw (new \Exception(sprintf('invalid logoutput kind "%s"', $kind)));
break;
}
}
}
?>