logger: shorter syntax

This commit is contained in:
Andrew Dolgov 2021-02-25 15:49:30 +03:00
parent 59c14e9c00
commit dcf0135285
13 changed files with 45 additions and 25 deletions

View File

@ -115,6 +115,10 @@ class Config {
return self::$instance; return self::$instance;
} }
private function __clone() {
//
}
function __construct() { function __construct() {
$ref = new ReflectionClass(get_class($this)); $ref = new ReflectionClass(get_class($this));

View File

@ -369,7 +369,7 @@ class Handler_Public extends Handler {
$_SESSION["language"] = get_pref(Prefs::USER_LANGUAGE, $_SESSION["uid"]); $_SESSION["language"] = get_pref(Prefs::USER_LANGUAGE, $_SESSION["uid"]);
} }
$_SESSION["ref_schema_version"] = get_schema_version(true); $_SESSION["ref_schema_version"] = get_schema_version();
$_SESSION["bw_limit"] = !!clean($_POST["bw_limit"] ?? false); $_SESSION["bw_limit"] = !!clean($_POST["bw_limit"] ?? false);
$_SESSION["safe_mode"] = $safe_mode; $_SESSION["safe_mode"] = $safe_mode;

View File

@ -3,7 +3,7 @@ class Logger {
private static $instance; private static $instance;
private $adapter; private $adapter;
public static $errornames = array( const ERROR_NAMES = [
1 => 'E_ERROR', 1 => 'E_ERROR',
2 => 'E_WARNING', 2 => 'E_WARNING',
4 => 'E_PARSE', 4 => 'E_PARSE',
@ -19,10 +19,14 @@ class Logger {
4096 => 'E_RECOVERABLE_ERROR', 4096 => 'E_RECOVERABLE_ERROR',
8192 => 'E_DEPRECATED', 8192 => 'E_DEPRECATED',
16384 => 'E_USER_DEPRECATED', 16384 => 'E_USER_DEPRECATED',
32767 => 'E_ALL'); 32767 => 'E_ALL'];
function log_error($errno, $errstr, $file, $line, $context) { static function log_error(int $errno, string $errstr, string $file, int $line, $context) {
if ($errno == E_NOTICE) return false; return self::get_instance()->_log_error($errno, $errstr, $file, $line, $context);
}
private function _log_error($errno, $errstr, $file, $line, $context) {
//if ($errno == E_NOTICE) return false;
if ($this->adapter) if ($this->adapter)
return $this->adapter->log_error($errno, $errstr, $file, $line, $context); return $this->adapter->log_error($errno, $errstr, $file, $line, $context);
@ -30,7 +34,11 @@ class Logger {
return false; return false;
} }
function log($errno, $errstr, $context = "") { static function log(int $errno, string $errstr, $context = "") {
return self::get_instance()->_log($errno, $errstr, $context);
}
private function _log($errno, $errstr, $context = "") {
if ($this->adapter) if ($this->adapter)
return $this->adapter->log_error($errno, $errstr, '', 0, $context); return $this->adapter->log_error($errno, $errstr, '', 0, $context);
else else
@ -57,11 +65,15 @@ class Logger {
} }
} }
public static function get() : Logger { private static function get_instance() : Logger {
if (self::$instance == null) if (self::$instance == null)
self::$instance = new self(); self::$instance = new self();
return self::$instance; return self::$instance;
} }
static function get() : Logger {
user_error("Please don't use Logger::get(), call Logger::log(...) instead.", E_USER_DEPRECATED);
return self::get_instance();
}
} }

View File

@ -21,7 +21,7 @@ class Logger_Stdout {
$priority = LOG_INFO; $priority = LOG_INFO;
} }
$errname = Logger::$errornames[$errno] . " ($errno)"; $errname = Logger::ERROR_NAMES[$errno] . " ($errno)";
print "[EEE] $priority $errname ($file:$line) $errstr\n"; print "[EEE] $priority $errname ($file:$line) $errstr\n";

View File

@ -21,7 +21,7 @@ class Logger_Syslog {
$priority = LOG_INFO; $priority = LOG_INFO;
} }
$errname = Logger::$errornames[$errno] . " ($errno)"; $errname = Logger::ERROR_NAMES[$errno] . " ($errno)";
syslog($priority, "[tt-rss] $errname ($file:$line) $errstr"); syslog($priority, "[tt-rss] $errname ($file:$line) $errstr");

View File

@ -20,7 +20,7 @@ class Mailer {
$to_combined = $to_name ? "$to_name <$to_address>" : $to_address; $to_combined = $to_name ? "$to_name <$to_address>" : $to_address;
if (Config::get(Config::LOG_SENT_MAIL)) if (Config::get(Config::LOG_SENT_MAIL))
Logger::get()->log(E_USER_NOTICE, "Sending mail from $from_combined to $to_combined [$subject]: $message"); Logger::log(E_USER_NOTICE, "Sending mail from $from_combined to $to_combined [$subject]: $message");
// HOOK_SEND_MAIL plugin instructions: // HOOK_SEND_MAIL plugin instructions:
// 1. return 1 or true if mail is handled // 1. return 1 or true if mail is handled

View File

@ -129,7 +129,7 @@ class Pref_System extends Handler_Administrative {
?> ?>
<tr> <tr>
<td class='errno'> <td class='errno'>
<?= Logger::$errornames[$line["errno"]] . " (" . $line["errno"] . ")" ?> <?= Logger::ERROR_NAMES[$line["errno"]] . " (" . $line["errno"] . ")" ?>
</td> </td>
<td class='filename'><?= $line["filename"] . ":" . $line["lineno"] ?></td> <td class='filename'><?= $line["filename"] . ":" . $line["lineno"] ?></td>
<td class='errstr'><?= $line["errstr"] . "\n" . $line["context"] ?></td> <td class='errstr'><?= $line["errstr"] . "\n" . $line["context"] ?></td>

View File

@ -167,6 +167,10 @@ class Prefs {
}; };
} }
private function __clone() {
//
}
static function get_all(int $owner_uid, int $profile_id = null) { static function get_all(int $owner_uid, int $profile_id = null) {
return self::get_instance()->_get_all($owner_uid, $profile_id); return self::get_instance()->_get_all($owner_uid, $profile_id);
} }
@ -204,7 +208,7 @@ class Prefs {
} }
} }
if (get_schema_version(true) >= 141) { if (get_schema_version() >= 141) {
// fill in any overrides from the database // fill in any overrides from the database
$sth = $this->pdo->prepare("SELECT pref_name, value FROM ttrss_user_prefs2 $sth = $this->pdo->prepare("SELECT pref_name, value FROM ttrss_user_prefs2
WHERE owner_uid = :uid AND WHERE owner_uid = :uid AND
@ -228,7 +232,7 @@ class Prefs {
list ($def_val, $type_hint) = self::_DEFAULTS[$pref_name]; list ($def_val, $type_hint) = self::_DEFAULTS[$pref_name];
if (get_schema_version(true) < 141) { if (get_schema_version() < 141) {
return Config::cast_to($def_val, $type_hint); return Config::cast_to($def_val, $type_hint);
} }
@ -341,7 +345,7 @@ class Prefs {
} }
function migrate(int $owner_uid, int $profile_id = null) { function migrate(int $owner_uid, int $profile_id = null) {
if (get_schema_version(true) < 141) if (get_schema_version() < 141)
return; return;
if (!$profile_id) $profile_id = null; if (!$profile_id) $profile_id = null;

View File

@ -140,7 +140,7 @@ class RPC extends Handler_Protected {
$error = Errors::E_SUCCESS; $error = Errors::E_SUCCESS;
if (get_schema_version(true) != SCHEMA_VERSION) { if (get_schema_version() != SCHEMA_VERSION) {
$error = Errors::E_SCHEMA_MISMATCH; $error = Errors::E_SCHEMA_MISMATCH;
} }
@ -343,7 +343,7 @@ class RPC extends Handler_Protected {
$context = clean($_REQUEST['context']); $context = clean($_REQUEST['context']);
if ($msg) { if ($msg) {
Logger::get()->log_error(E_USER_WARNING, Logger::log_error(E_USER_WARNING,
$msg, 'client-js:' . $file, $line, $context); $msg, 'client-js:' . $file, $line, $context);
echo json_encode(array("message" => "HOST_ERROR_LOGGED")); echo json_encode(array("message" => "HOST_ERROR_LOGGED"));

View File

@ -216,7 +216,7 @@ class RSSUtils {
Debug::log("!! Last error: $error_message"); Debug::log("!! Last error: $error_message");
Logger::get()->log(E_USER_NOTICE, Logger::log(E_USER_NOTICE,
sprintf("Update process for feed %d (%s, owner UID: %d) failed with exit code: %d (%s).", sprintf("Update process for feed %d (%s, owner UID: %d) failed with exit code: %d (%s).",
$tline["id"], clean($tline["title"]), $tline["owner_uid"], $exit_code, clean($error_message))); $tline["id"], clean($tline["title"]), $tline["owner_uid"], $exit_code, clean($error_message)));
@ -233,7 +233,7 @@ class RSSUtils {
if (!self::update_rss_feed($tline["id"], true)) { if (!self::update_rss_feed($tline["id"], true)) {
global $fetch_last_error; global $fetch_last_error;
Logger::get()->log(E_USER_NOTICE, Logger::log(E_USER_NOTICE,
sprintf("Update request for feed %d (%s, owner UID: %d) failed: %s.", sprintf("Update request for feed %d (%s, owner UID: %d) failed: %s.",
$tline["id"], clean($tline["title"]), $tline["owner_uid"], clean($fetch_last_error))); $tline["id"], clean($tline["title"]), $tline["owner_uid"], clean($fetch_last_error)));
} }
@ -241,7 +241,7 @@ class RSSUtils {
Debug::log(sprintf("<= %.4f (sec) (not using a separate process)", microtime(true) - $fstarted)); Debug::log(sprintf("<= %.4f (sec) (not using a separate process)", microtime(true) - $fstarted));
} catch (PDOException $e) { } catch (PDOException $e) {
Logger::get()->log_error(E_USER_WARNING, $e->getMessage(), $e->getFile(), $e->getLine(), $e->getTraceAsString()); Logger::log_error(E_USER_WARNING, $e->getMessage(), $e->getFile(), $e->getLine(), $e->getTraceAsString());
try { try {
$pdo->rollback(); $pdo->rollback();
@ -1619,7 +1619,7 @@ class RSSUtils {
$sth->execute(); $sth->execute();
while ($row = $sth->fetch()) { while ($row = $sth->fetch()) {
Logger::get()->log(E_USER_NOTICE, Logger::log(E_USER_NOTICE,
sprintf("Auto disabling feed %d (%s, UID: %d) because it failed to update for %d days.", sprintf("Auto disabling feed %d (%s, UID: %d) because it failed to update for %d days.",
$row["id"], clean($row["title"]), $row["owner_uid"], Config::get(Config::DAEMON_UNSUCCESSFUL_DAYS_LIMIT))); $row["id"], clean($row["title"]), $row["owner_uid"], Config::get(Config::DAEMON_UNSUCCESSFUL_DAYS_LIMIT)));

View File

@ -47,7 +47,7 @@ class UserHelper {
} }
if (!$user_id) if (!$user_id)
Logger::get()->log(E_USER_WARNING, "Failed login attempt for $login (service: $service) from " . UserHelper::get_user_ip()); Logger::log(E_USER_WARNING, "Failed login attempt for $login (service: $service) from " . UserHelper::get_user_ip());
return false; return false;
@ -102,7 +102,7 @@ class UserHelper {
if (empty($_SESSION["uid"])) { if (empty($_SESSION["uid"])) {
if (Config::get(Config::AUTH_AUTO_LOGIN) && self::authenticate(null, null)) { if (Config::get(Config::AUTH_AUTO_LOGIN) && self::authenticate(null, null)) {
$_SESSION["ref_schema_version"] = get_schema_version(true); $_SESSION["ref_schema_version"] = get_schema_version();
} else { } else {
self::authenticate(null, null, true); self::authenticate(null, null, true);
} }

View File

@ -54,7 +54,7 @@ function ttrss_error_handler($errno, $errstr, $file, $line) {
$errstr = truncate_middle($errstr, 16384, " (...) "); $errstr = truncate_middle($errstr, 16384, " (...) ");
if (class_exists("Logger")) if (class_exists("Logger"))
return Logger::get()->log_error($errno, $errstr, $file, $line, $context); return Logger::log_error($errno, $errstr, $file, $line, $context);
} }
function ttrss_fatal_handler() { function ttrss_fatal_handler() {
@ -77,7 +77,7 @@ function ttrss_fatal_handler() {
if ($last_query) $errstr .= " [Last query: $last_query]"; if ($last_query) $errstr .= " [Last query: $last_query]";
if (class_exists("Logger")) if (class_exists("Logger"))
return Logger::get()->log_error($errno, $errstr, $file, $line, $context); return Logger::log_error($errno, $errstr, $file, $line, $context);
} }
return false; return false;

View File

@ -242,7 +242,7 @@
Debug::log(sprintf("Exception while updating feed %d: %s (%s:%d)", Debug::log(sprintf("Exception while updating feed %d: %s (%s:%d)",
$options["update-feed"], $e->getMessage(), $e->getFile(), $e->getLine())); $options["update-feed"], $e->getMessage(), $e->getFile(), $e->getLine()));
Logger::get()->log_error(E_USER_WARNING, $e->getMessage(), $e->getFile(), $e->getLine(), $e->getTraceAsString()); Logger::log_error(E_USER_WARNING, $e->getMessage(), $e->getFile(), $e->getLine(), $e->getTraceAsString());
exit(110); exit(110);
} }