From d6c65bec9d7970112fb02fc1c0d39763f8109a57 Mon Sep 17 00:00:00 2001 From: Frederic Guillot Date: Sat, 6 Jul 2013 14:29:45 -0400 Subject: [PATCH] Add support to dump debug output to a log file --- README.markdown | 2 ++ common.php | 2 ++ cronjob.php | 1 + index.php | 17 +++++++++----- model.php | 13 +++++++++++ vendor/PicoFeed/Logging.php | 13 +++++++++++ vendor/PicoFeed/Parser.php | 10 ++++++--- vendor/PicoFeed/Parsers/Atom.php | 2 +- vendor/PicoFeed/Parsers/Rss10.php | 2 +- vendor/PicoFeed/Parsers/Rss20.php | 2 +- vendor/PicoFeed/Reader.php | 13 +++++++++++ vendor/PicoFeed/RemoteResource.php | 36 ++++++++++++++++++++++++++++-- 12 files changed, 100 insertions(+), 13 deletions(-) create mode 100644 vendor/PicoFeed/Logging.php diff --git a/README.markdown b/README.markdown index bb00576..054a1fc 100644 --- a/README.markdown +++ b/README.markdown @@ -140,3 +140,5 @@ Actually, the following constants can be overrided: - `HTTP_TIMEOUT` => default value is 10 seconds - `APP_VERSION` => default value is master - `DB_FILENAME` => default value is `data/db.sqlite` +- `DEBUG` => default is false (enable logs dump of picoFeed) +- `DEBUG_DIRECTORY` => default is /tmp (place to store log files) \ No newline at end of file diff --git a/common.php b/common.php index fc85c59..3ef3f31 100644 --- a/common.php +++ b/common.php @@ -14,6 +14,8 @@ if (file_exists('config.php')) require 'config.php'; defined('APP_VERSION') or define('APP_VERSION', 'master'); defined('HTTP_TIMEOUT') or define('HTTP_TIMEOUT', 10); defined('DB_FILENAME') or define('DB_FILENAME', 'data/db.sqlite'); +defined('DEBUG') or define('DEBUG', false); +defined('DEBUG_DIRECTORY') or define('DEBUG_DIRECTORY', '/tmp'); PicoTools\container('db', function() { diff --git a/cronjob.php b/cronjob.php index 88baaba..1c1632a 100644 --- a/cronjob.php +++ b/cronjob.php @@ -26,3 +26,4 @@ if ($update_interval !== null && $call_interval !== null && $limit === Model\LIM } Model\update_feeds($limit); +Model\write_debug(); diff --git a/index.php b/index.php index 49203a9..ab1e565 100644 --- a/index.php +++ b/index.php @@ -284,7 +284,7 @@ Router\get_action('refresh-feed', function() { $id = Request\int_param('feed_id'); if ($id) Model\update_feed($id); - + Model\write_debug(); Response\redirect('?action=unread'); }); @@ -292,13 +292,15 @@ Router\get_action('refresh-feed', function() { // Ajax call to refresh one feed Router\post_action('refresh-feed', function() { - $id = Request\int_param('feed_id'); + $id = Request\int_param('feed_id', 0); if ($id) { - Response\json(array('feed_id' => $id, 'result' => Model\update_feed($id))); + + $result = Model\update_feed($id); + Model\write_debug(); } - Response\json(array('feed_id' => 0, 'result' => false)); + Response\json(array('feed_id' => $id, 'result' => $result)); }); @@ -332,6 +334,8 @@ Router\get_action('flush-history', function() { Router\get_action('refresh-all', function() { Model\update_feeds(); + Model\write_debug(); + Session\flash(t('Your subscriptions are updated')); Response\redirect('?action=unread'); }); @@ -364,7 +368,10 @@ Router\get_action('add', function() { // Add the feed Router\post_action('add', function() { - if (Model\import_feed($_POST['url'])) { + $result = Model\import_feed($_POST['url']); + Model\write_debug(); + + if ($result) { Session\flash(t('Subscription added successfully.')); Response\redirect('?action=feeds'); diff --git a/model.php b/model.php index 65928ad..f873fc6 100644 --- a/model.php +++ b/model.php @@ -61,6 +61,19 @@ function get_paging_options() } +function write_debug() +{ + if (DEBUG) { + + file_put_contents( + DEBUG_DIRECTORY.'/miniflux_'.date('YmdH').'.debug', + var_export(\PicoFeed\Logging::$messages, true).PHP_EOL, + FILE_APPEND | LOCK_EX + ); + } +} + + function encode_item_id($input) { return strtr(base64_encode($input), '+/=', '-_,'); diff --git a/vendor/PicoFeed/Logging.php b/vendor/PicoFeed/Logging.php new file mode 100644 index 0000000..4b53d19 --- /dev/null +++ b/vendor/PicoFeed/Logging.php @@ -0,0 +1,13 @@ +message, $error->line, $error->column, $error->code ); } + + return implode(', ', $errors); } diff --git a/vendor/PicoFeed/Parsers/Atom.php b/vendor/PicoFeed/Parsers/Atom.php index b873ca4..e05c127 100644 --- a/vendor/PicoFeed/Parsers/Atom.php +++ b/vendor/PicoFeed/Parsers/Atom.php @@ -11,7 +11,7 @@ class Atom extends \PicoFeed\Parser if ($xml === false) { - if ($this->debug) $this->displayXmlErrors(); + \PicoFeed\Logging::log($this->getXmlErrors()); return false; } diff --git a/vendor/PicoFeed/Parsers/Rss10.php b/vendor/PicoFeed/Parsers/Rss10.php index d386b78..8b88995 100644 --- a/vendor/PicoFeed/Parsers/Rss10.php +++ b/vendor/PicoFeed/Parsers/Rss10.php @@ -11,7 +11,7 @@ class Rss10 extends \PicoFeed\Parser if ($xml === false) { - if ($this->debug) $this->displayXmlErrors(); + \PicoFeed\Logging::log($this->getXmlErrors()); return false; } diff --git a/vendor/PicoFeed/Parsers/Rss20.php b/vendor/PicoFeed/Parsers/Rss20.php index 2f1bca1..40d6e5c 100644 --- a/vendor/PicoFeed/Parsers/Rss20.php +++ b/vendor/PicoFeed/Parsers/Rss20.php @@ -11,7 +11,7 @@ class Rss20 extends \PicoFeed\Parser if ($xml === false) { - if ($this->debug) $this->displayXmlErrors(); + \PicoFeed\Logging::log($this->getXmlErrors()); return false; } diff --git a/vendor/PicoFeed/Reader.php b/vendor/PicoFeed/Reader.php index efa2a2a..56ea08b 100644 --- a/vendor/PicoFeed/Reader.php +++ b/vendor/PicoFeed/Reader.php @@ -2,6 +2,7 @@ namespace PicoFeed; +require_once __DIR__.'/Logging.php'; require_once __DIR__.'/Parser.php'; require_once __DIR__.'/RemoteResource.php'; @@ -83,29 +84,39 @@ class Reader if (strpos($first_tag, 'content); } else if (strpos($first_tag, 'content); } else if (strpos($first_tag, 'content); } else if (strpos($first_tag, 'content); } else if (strpos($first_tag, 'content); } @@ -129,6 +140,8 @@ class Reader return false; } + Logging::log('Reader: run discover()'); + \libxml_use_internal_errors(true); $dom = new \DOMDocument; diff --git a/vendor/PicoFeed/RemoteResource.php b/vendor/PicoFeed/RemoteResource.php index f5d5c57..45b2cc2 100644 --- a/vendor/PicoFeed/RemoteResource.php +++ b/vendor/PicoFeed/RemoteResource.php @@ -2,6 +2,8 @@ namespace PicoFeed; +require_once __DIR__.'/Logging.php'; + class RemoteResource { public $user_agent; @@ -69,7 +71,7 @@ class RemoteResource public function execute() { - $response = $this->makeRequest(); + $response = $this->doRequest(); $this->etag = isset($response['headers']['ETag']) ? $response['headers']['ETag'] : ''; $this->last_modified = isset($response['headers']['Last-Modified']) ? $response['headers']['Last-Modified'] : ''; @@ -98,7 +100,7 @@ class RemoteResource } - public function makeRequest() + public function doRequest() { $http_code = 200; $http_body = ''; @@ -110,6 +112,10 @@ class RemoteResource } else { + Logging::log('Fetch URL: '.$this->url); + Logging::log('Etag: '.$this->etag); + Logging::log('Last-Modified: '.$this->last_modified); + $headers = array('Connection: close'); if ($this->etag) $headers[] = 'If-None-Match: '.$this->etag; @@ -131,6 +137,25 @@ class RemoteResource curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); $http_response = curl_exec($ch); + + if (curl_errno($ch)) { + + Logging::log('cURL error: '.curl_error($ch)); + + curl_close($ch); + + return array( + 'status' => $http_code, + 'body' => $http_body, + 'headers' => $http_headers + ); + } + + Logging::log('cURL total time: '.curl_getinfo($ch, CURLINFO_TOTAL_TIME)); + Logging::log('cURL dns lookup time: '.curl_getinfo($ch, CURLINFO_NAMELOOKUP_TIME)); + Logging::log('cURL connect time: '.curl_getinfo($ch, CURLINFO_CONNECT_TIME)); + Logging::log('cURL speed download: '.curl_getinfo($ch, CURLINFO_SPEED_DOWNLOAD)); + $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); $http_body = ''; $http_headers = array(); @@ -162,6 +187,13 @@ class RemoteResource $http_body = implode("\r\n", array_splice($lines, $i + 1)); } + Logging::log('HTTP status code: '.$http_code); + + foreach ($http_headers as $header_name => $header_value) { + + Logging::log('HTTP headers: '.$header_name.' => '.$header_value); + } + return array( 'status' => $http_code, 'body' => $http_body,