2023-02-16 20:24:56 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace Shimmie2;
|
|
|
|
|
|
|
|
class LogConsole extends Extension
|
|
|
|
{
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onInitExt(InitExtEvent $event): void
|
2023-02-16 20:24:56 +00:00
|
|
|
{
|
|
|
|
global $config;
|
|
|
|
$config->set_default_bool("log_console_access", true);
|
|
|
|
$config->set_default_bool("log_console_colour", true);
|
|
|
|
$config->set_default_int("log_console_level", SCORE_LOG_INFO);
|
|
|
|
}
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onPageRequest(PageRequestEvent $event): void
|
2023-02-16 20:24:56 +00:00
|
|
|
{
|
|
|
|
global $config, $page;
|
|
|
|
|
|
|
|
if (
|
|
|
|
$config->get_bool("log_console_access") &&
|
|
|
|
isset($_SERVER['REQUEST_URI'])
|
|
|
|
) {
|
|
|
|
$this->log(new LogEvent(
|
|
|
|
"access",
|
|
|
|
SCORE_LOG_INFO,
|
2024-02-11 11:34:09 +00:00
|
|
|
"{$_SERVER['REQUEST_METHOD']} {$_SERVER['REQUEST_URI']}"
|
2023-02-16 20:24:56 +00:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2023-02-16 20:28:27 +00:00
|
|
|
/*
|
2023-02-16 20:24:56 +00:00
|
|
|
if ($event->page_matches("log_test")) {
|
|
|
|
log_debug("log_console", "Hello debug!");
|
|
|
|
log_info("log_console", "Hello info!");
|
|
|
|
log_warning("log_console", "Hello warning!");
|
|
|
|
$page->set_mode(PageMode::DATA);
|
|
|
|
$page->set_data("You should see something in the log\n");
|
|
|
|
}
|
2023-02-16 20:28:27 +00:00
|
|
|
*/
|
2023-02-16 20:24:56 +00:00
|
|
|
}
|
|
|
|
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onLog(LogEvent $event): void
|
2023-02-16 20:24:56 +00:00
|
|
|
{
|
|
|
|
global $config;
|
|
|
|
if ($event->priority >= $config->get_int("log_console_level")) {
|
|
|
|
$this->log($event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-20 14:10:59 +00:00
|
|
|
private function log(LogEvent $event): void
|
2023-02-16 20:24:56 +00:00
|
|
|
{
|
|
|
|
global $config, $user;
|
|
|
|
// TODO: colour based on event->priority
|
|
|
|
$username = ($user && $user->name) ? $user->name : "Anonymous";
|
|
|
|
$str = join(" ", [
|
|
|
|
date(DATE_ISO8601),
|
|
|
|
get_real_ip(),
|
|
|
|
$event->section,
|
|
|
|
$username,
|
|
|
|
$event->message
|
|
|
|
]);
|
|
|
|
if ($config->get_bool("log_console_colour")) {
|
|
|
|
if ($event->priority >= SCORE_LOG_WARNING) {
|
|
|
|
// red
|
|
|
|
$COL = "\033[0;31m";
|
|
|
|
} elseif ($event->priority >= SCORE_LOG_INFO) {
|
|
|
|
// default
|
|
|
|
$COL = "";
|
|
|
|
} elseif ($event->priority >= SCORE_LOG_NOTSET) {
|
|
|
|
// blue
|
|
|
|
$COL = "\033[0;34m";
|
|
|
|
} else {
|
|
|
|
// priority < 0 ???
|
|
|
|
// magenta
|
|
|
|
$COL = "\033[0;35m";
|
|
|
|
}
|
|
|
|
$str = "$COL$str\033[0m\n";
|
|
|
|
} else {
|
|
|
|
$str = "$str\n";
|
|
|
|
}
|
2023-02-16 20:29:21 +00:00
|
|
|
if (!defined("UNITTEST") && PHP_SAPI !== 'cli' && PHP_SAPI !== 'phpdbg') {
|
2024-02-11 21:27:38 +00:00
|
|
|
$fp = @fopen("/dev/tty", "w");
|
2023-02-19 11:13:51 +00:00
|
|
|
if ($fp) {
|
2023-02-17 11:31:34 +00:00
|
|
|
fwrite($fp, $str);
|
|
|
|
fclose($fp);
|
|
|
|
}
|
2023-02-16 20:28:27 +00:00
|
|
|
}
|
2023-02-16 20:24:56 +00:00
|
|
|
}
|
|
|
|
}
|