]> git.wh0rd.org - tt-rss.git/commitdiff
experimental SQL-based error logger
authorAndrew Dolgov <fox@fakecake.org>
Tue, 16 Apr 2013 15:41:31 +0000 (19:41 +0400)
committerAndrew Dolgov <fox@fakecake.org>
Tue, 16 Apr 2013 15:41:31 +0000 (19:41 +0400)
classes/logger.php [new file with mode: 0644]
classes/logger/sql.php [new file with mode: 0644]
include/errorhandler.php [new file with mode: 0644]
include/functions.php
include/sessions.php
schema/ttrss_schema_mysql.sql
schema/ttrss_schema_pgsql.sql
schema/versions/mysql/118.sql [new file with mode: 0644]
schema/versions/pgsql/118.sql [new file with mode: 0644]

diff --git a/classes/logger.php b/classes/logger.php
new file mode 100644 (file)
index 0000000..6370e14
--- /dev/null
@@ -0,0 +1,24 @@
+<?php
+class Logger {
+
+       protected $errornames = array(
+               2                       => '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 (file)
index 0000000..7ee2284
--- /dev/null
@@ -0,0 +1,35 @@
+<?php
+class Logger_SQL {
+
+       private $link;
+
+       function __construct() {
+               $this->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 (file)
index 0000000..13eed0e
--- /dev/null
@@ -0,0 +1,50 @@
+<?php
+// TODO: make configurable
+require_once "classes/logger.php";
+require_once "classes/logger/sql.php";
+
+function ttrss_error_handler($errno, $errstr, $file, $line, $context) {
+       global $logger;
+
+       if (!$logger) $logger = new Logger_SQL();
+
+       $errfile = str_replace(dirname(dirname(__FILE__)), "", $errfile);
+
+       if ($logger) {
+               return $logger->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');
+?>
index 659950be0f327f04be092d6a73f05e2998576b2a..621357ea6e07935bc1d3f87c6f42e0ba1d8a140f 100644 (file)
@@ -1,6 +1,6 @@
 <?php
        define('EXPECTED_CONFIG_VERSION', 26);
-       define('SCHEMA_VERSION', 117);
+       define('SCHEMA_VERSION', 118);
 
        define('LABEL_BASE_INDEX', -1024);
        define('PLUGIN_FEED_BASE_INDEX', -128);
                return is_file(ICONS_DIR . "/$id.ico") && filesize(ICONS_DIR . "/$id.ico") > 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");
                                }
                        }
 
+                       return true;
+               }
+
+               return false;
+       }
+
+       function init_connection($link) {
+               if ($link) {
+                       init_connection_only($link);
+
                        global $pluginhost;
 
                        $pluginhost = new PluginHost($link);
index 402e8b8deca2c26922e75f8c6dd2b78c9cf5e64e..bc8b7cff1db94053d62eda507ee9ab197cb1c134 100644 (file)
@@ -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";
index 3773d9a4538bec49399a3181e883bc8ee0fe5856..0d0a8f460944dd6ad53e0bb9fd0176ede852e581 100644 (file)
@@ -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;
index 3fc58555d4b1029a8ba00a09e6d41a1a94493347..d81f65f2b936af33ae40db15501b0869c2326fb4 100644 (file)
@@ -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 (file)
index 0000000..add2b0c
--- /dev/null
@@ -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 (file)
index 0000000..161cf4e
--- /dev/null
@@ -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;