From 889a5f9f195309df5842f142986b3166212d8a58 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Tue, 16 Apr 2013 19:41:31 +0400 Subject: [PATCH] experimental SQL-based error logger --- classes/logger.php | 24 +++++++++++++++++ classes/logger/sql.php | 35 ++++++++++++++++++++++++ include/errorhandler.php | 50 +++++++++++++++++++++++++++++++++++ include/functions.php | 15 ++++++++--- include/sessions.php | 1 + schema/ttrss_schema_mysql.sql | 13 ++++++++- schema/ttrss_schema_pgsql.sql | 13 ++++++++- schema/versions/mysql/118.sql | 16 +++++++++++ schema/versions/pgsql/118.sql | 15 +++++++++++ 9 files changed, 177 insertions(+), 5 deletions(-) create mode 100644 classes/logger.php create mode 100644 classes/logger/sql.php create mode 100644 include/errorhandler.php create mode 100644 schema/versions/mysql/118.sql create mode 100644 schema/versions/pgsql/118.sql diff --git a/classes/logger.php b/classes/logger.php new file mode 100644 index 00000000..6370e142 --- /dev/null +++ b/classes/logger.php @@ -0,0 +1,24 @@ + 'E_WARNING', + 8 => 'E_NOTICE', + 256 => 'E_USER_ERROR', + 512 => 'E_USER_WARNING', + 1024 => 'E_USER_NOTICE', + 2048 => 'E_STRICT', + 4096 => 'E_RECOVERABLE_ERROR', + 8192 => 'E_DEPRECATED', + 16384 => 'E_USER_DEPRECATED', + 32767 => 'E_ALL'); + + function log_error($errno, $errstr, $file, $line, $context) { + return false; + } + + function log($string) { + return false; + } +} +?> diff --git a/classes/logger/sql.php b/classes/logger/sql.php new file mode 100644 index 00000000..7ee22844 --- /dev/null +++ b/classes/logger/sql.php @@ -0,0 +1,35 @@ +link = db_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME); + } + + function log_error($errno, $errstr, $file, $line, $context) { + + if ($errno == E_NOTICE) return false; + + if ($this->link) { + $errno = db_escape_string($this->link, $errno); + $errstr = db_escape_string($this->link, $errstr); + $file = db_escape_string($this->link, $file); + $line = db_escape_string($this->link, $line); + $context = db_escape_string($this->link, json_encode($context)); + + $owner_uid = $_SESSION["uid"] ? $_SESSION["uid"] : "NULL"; + + $result = db_query($this->link, + "INSERT INTO ttrss_error_log + (errno, errstr, filename, lineno, context, owner_uid, created_at) VALUES + ($errno, '$errstr', '$file', '$line', '$context', $owner_uid, NOW())"); + + return db_affected_rows($this->link, $result) != 0; + + } + return false; + } + +} +?> diff --git a/include/errorhandler.php b/include/errorhandler.php new file mode 100644 index 00000000..13eed0e3 --- /dev/null +++ b/include/errorhandler.php @@ -0,0 +1,50 @@ +log_error($errno, $errstr, $file, $line, $context); + } + + return false; +} + +function ttrss_fatal_handler() { + global $logger; + + $file = "UNKNOWN FILE"; + $errstr = "UNKNOWN"; + $errno = E_CORE_ERROR; + $line = -1; + + $error = error_get_last(); + + if ($error !== NULL) { + $errno = $error["type"]; + $file = $error["file"]; + $line = $error["line"]; + $errstr = $error["message"]; + + $context = debug_backtrace(); + + $file = str_replace(dirname(dirname(__FILE__)) . "/", "", $file); + + if (!$logger) $logger = new Logger_SQL(); + + if ($logger) { + $logger->log_error($errno, $errstr, $file, $line, $context); + } + } +} + +register_shutdown_function('ttrss_fatal_handler'); +set_error_handler('ttrss_error_handler'); +?> diff --git a/include/functions.php b/include/functions.php index 659950be..621357ea 100644 --- a/include/functions.php +++ b/include/functions.php @@ -1,6 +1,6 @@ 0; } - function init_connection($link) { + function init_connection_only($link) { if ($link) { - if (DB_TYPE == "pgsql") { pg_query($link, "set client_encoding = 'UTF-8'"); pg_set_client_encoding("UNICODE"); @@ -3385,6 +3384,16 @@ } } + return true; + } + + return false; + } + + function init_connection($link) { + if ($link) { + init_connection_only($link); + global $pluginhost; $pluginhost = new PluginHost($link); diff --git a/include/sessions.php b/include/sessions.php index 402e8b8d..bc8b7cff 100644 --- a/include/sessions.php +++ b/include/sessions.php @@ -3,6 +3,7 @@ require_once "config.php"; require_once "db.php"; + require_once "errorhandler.php"; require_once "lib/accept-to-gettext.php"; require_once "lib/gettext/gettext.inc"; require_once "version.php"; diff --git a/schema/ttrss_schema_mysql.sql b/schema/ttrss_schema_mysql.sql index 3773d9a4..0d0a8f46 100644 --- a/schema/ttrss_schema_mysql.sql +++ b/schema/ttrss_schema_mysql.sql @@ -1,6 +1,7 @@ SET NAMES utf8; SET CHARACTER SET utf8; +drop table if exists ttrss_error_log; drop table if exists ttrss_plugin_storage; drop table if exists ttrss_linked_feeds; drop table if exists ttrss_linked_instances; @@ -299,7 +300,7 @@ create table ttrss_tags (id integer primary key auto_increment, create table ttrss_version (schema_version int not null) ENGINE=InnoDB DEFAULT CHARSET=UTF8; -insert into ttrss_version values (117); +insert into ttrss_version values (118); create table ttrss_enclosures (id integer primary key auto_increment, content_url text not null, @@ -478,5 +479,15 @@ create table ttrss_plugin_storage ( content longtext not null, foreign key (owner_uid) references ttrss_users(id) ON DELETE CASCADE) ENGINE=InnoDB DEFAULT CHARSET=UTF8; +create table ttrss_error_log( + id integer not null auto_increment primary key, + owner_uid integer, + errno integer not null, + errstr text not null, + filename text not null, + lineno integer not null, + context text not null, + created_at datetime not null, + foreign key (owner_uid) references ttrss_users(id) ON DELETE SET NULL) ENGINE=InnoDB DEFAULT CHARSET=UTF8; commit; diff --git a/schema/ttrss_schema_pgsql.sql b/schema/ttrss_schema_pgsql.sql index 3fc58555..d81f65f2 100644 --- a/schema/ttrss_schema_pgsql.sql +++ b/schema/ttrss_schema_pgsql.sql @@ -1,3 +1,4 @@ +drop table if exists ttrss_error_log; drop table if exists ttrss_plugin_storage; drop table if exists ttrss_linked_feeds; drop table if exists ttrss_linked_instances; @@ -257,7 +258,7 @@ create index ttrss_tags_post_int_id_idx on ttrss_tags(post_int_id); create table ttrss_version (schema_version int not null); -insert into ttrss_version values (117); +insert into ttrss_version values (118); create table ttrss_enclosures (id serial not null primary key, content_url text not null, @@ -421,4 +422,14 @@ create table ttrss_plugin_storage ( owner_uid integer not null references ttrss_users(id) ON DELETE CASCADE, content text not null); +create table ttrss_error_log( + id serial not null primary key, + owner_uid integer references ttrss_users(id) ON DELETE SET NULL, + errno integer not null, + errstr text not null, + filename text not null, + lineno integer not null, + context text not null, + created_at timestamp not null); + commit; diff --git a/schema/versions/mysql/118.sql b/schema/versions/mysql/118.sql new file mode 100644 index 00000000..add2b0c1 --- /dev/null +++ b/schema/versions/mysql/118.sql @@ -0,0 +1,16 @@ +begin; + +create table ttrss_error_log( + id integer not null auto_increment primary key, + owner_uid integer, + errno integer not null, + errstr text not null, + filename text not null, + lineno integer not null, + context text not null, + created_at datetime not null, + foreign key (owner_uid) references ttrss_users(id) ON DELETE SET NULL) ENGINE=InnoDB DEFAULT CHARSET=UTF8; + +update ttrss_version set schema_version = 118; + +commit; diff --git a/schema/versions/pgsql/118.sql b/schema/versions/pgsql/118.sql new file mode 100644 index 00000000..161cf4e3 --- /dev/null +++ b/schema/versions/pgsql/118.sql @@ -0,0 +1,15 @@ +begin; + +create table ttrss_error_log( + id serial not null primary key, + owner_uid integer references ttrss_users(id) ON DELETE SET NULL, + errno integer not null, + errstr text not null, + filename text not null, + lineno integer not null, + context text not null, + created_at timestamp not null); + +update ttrss_version set schema_version = 118; + +commit; -- 2.39.5