Add support to dump debug output to a log file

This commit is contained in:
Frederic Guillot 2013-07-06 14:29:45 -04:00
parent 16f0bd4966
commit d6c65bec9d
12 changed files with 100 additions and 13 deletions

View File

@ -140,3 +140,5 @@ Actually, the following constants can be overrided:
- `HTTP_TIMEOUT` => default value is 10 seconds - `HTTP_TIMEOUT` => default value is 10 seconds
- `APP_VERSION` => default value is master - `APP_VERSION` => default value is master
- `DB_FILENAME` => default value is `data/db.sqlite` - `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)

View File

@ -14,6 +14,8 @@ if (file_exists('config.php')) require 'config.php';
defined('APP_VERSION') or define('APP_VERSION', 'master'); defined('APP_VERSION') or define('APP_VERSION', 'master');
defined('HTTP_TIMEOUT') or define('HTTP_TIMEOUT', 10); defined('HTTP_TIMEOUT') or define('HTTP_TIMEOUT', 10);
defined('DB_FILENAME') or define('DB_FILENAME', 'data/db.sqlite'); 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() { PicoTools\container('db', function() {

View File

@ -26,3 +26,4 @@ if ($update_interval !== null && $call_interval !== null && $limit === Model\LIM
} }
Model\update_feeds($limit); Model\update_feeds($limit);
Model\write_debug();

View File

@ -284,7 +284,7 @@ Router\get_action('refresh-feed', function() {
$id = Request\int_param('feed_id'); $id = Request\int_param('feed_id');
if ($id) Model\update_feed($id); if ($id) Model\update_feed($id);
Model\write_debug();
Response\redirect('?action=unread'); Response\redirect('?action=unread');
}); });
@ -292,13 +292,15 @@ Router\get_action('refresh-feed', function() {
// Ajax call to refresh one feed // Ajax call to refresh one feed
Router\post_action('refresh-feed', function() { Router\post_action('refresh-feed', function() {
$id = Request\int_param('feed_id'); $id = Request\int_param('feed_id', 0);
if ($id) { 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() { Router\get_action('refresh-all', function() {
Model\update_feeds(); Model\update_feeds();
Model\write_debug();
Session\flash(t('Your subscriptions are updated')); Session\flash(t('Your subscriptions are updated'));
Response\redirect('?action=unread'); Response\redirect('?action=unread');
}); });
@ -364,7 +368,10 @@ Router\get_action('add', function() {
// Add the feed // Add the feed
Router\post_action('add', function() { 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.')); Session\flash(t('Subscription added successfully.'));
Response\redirect('?action=feeds'); Response\redirect('?action=feeds');

View File

@ -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) function encode_item_id($input)
{ {
return strtr(base64_encode($input), '+/=', '-_,'); return strtr(base64_encode($input), '+/=', '-_,');

13
vendor/PicoFeed/Logging.php vendored Normal file
View File

@ -0,0 +1,13 @@
<?php
namespace PicoFeed;
class Logging
{
public static $messages = array();
public static function log($message)
{
self::$messages[] = $message;
}
}

View File

@ -2,6 +2,7 @@
namespace PicoFeed; namespace PicoFeed;
require_once __DIR__.'/Logging.php';
require_once __DIR__.'/Filter.php'; require_once __DIR__.'/Filter.php';
require_once __DIR__.'/Encoding.php'; require_once __DIR__.'/Encoding.php';
@ -14,7 +15,6 @@ abstract class Parser
public $title = ''; public $title = '';
public $updated = ''; public $updated = '';
public $items = array(); public $items = array();
public $debug = false;
abstract public function execute(); abstract public function execute();
@ -47,17 +47,21 @@ abstract class Parser
} }
public function displayXmlErrors() public function getXmlErrors()
{ {
$errors = array();
foreach(\libxml_get_errors() as $error) { foreach(\libxml_get_errors() as $error) {
printf("Message: %s\nLine: %d\nColumn: %d\nCode: %d\n", $errors[] = sprintf('XML error: %s (Line: %d - Column: %d - Code: %d)',
$error->message, $error->message,
$error->line, $error->line,
$error->column, $error->column,
$error->code $error->code
); );
} }
return implode(', ', $errors);
} }

View File

@ -11,7 +11,7 @@ class Atom extends \PicoFeed\Parser
if ($xml === false) { if ($xml === false) {
if ($this->debug) $this->displayXmlErrors(); \PicoFeed\Logging::log($this->getXmlErrors());
return false; return false;
} }

View File

@ -11,7 +11,7 @@ class Rss10 extends \PicoFeed\Parser
if ($xml === false) { if ($xml === false) {
if ($this->debug) $this->displayXmlErrors(); \PicoFeed\Logging::log($this->getXmlErrors());
return false; return false;
} }

View File

@ -11,7 +11,7 @@ class Rss20 extends \PicoFeed\Parser
if ($xml === false) { if ($xml === false) {
if ($this->debug) $this->displayXmlErrors(); \PicoFeed\Logging::log($this->getXmlErrors());
return false; return false;
} }

View File

@ -2,6 +2,7 @@
namespace PicoFeed; namespace PicoFeed;
require_once __DIR__.'/Logging.php';
require_once __DIR__.'/Parser.php'; require_once __DIR__.'/Parser.php';
require_once __DIR__.'/RemoteResource.php'; require_once __DIR__.'/RemoteResource.php';
@ -83,29 +84,39 @@ class Reader
if (strpos($first_tag, '<feed') !== false) { if (strpos($first_tag, '<feed') !== false) {
Logging::log('Reader: discover Atom feed');
require_once __DIR__.'/Parsers/Atom.php'; require_once __DIR__.'/Parsers/Atom.php';
return new Parsers\Atom($this->content); return new Parsers\Atom($this->content);
} }
else if (strpos($first_tag, '<rss') !== false && else if (strpos($first_tag, '<rss') !== false &&
(strpos($first_tag, 'version="2.0"') !== false || strpos($first_tag, 'version=\'2.0\'') !== false)) { (strpos($first_tag, 'version="2.0"') !== false || strpos($first_tag, 'version=\'2.0\'') !== false)) {
Logging::log('Reader: discover RSS 2.0 feed');
require_once __DIR__.'/Parsers/Rss20.php'; require_once __DIR__.'/Parsers/Rss20.php';
return new Parsers\Rss20($this->content); return new Parsers\Rss20($this->content);
} }
else if (strpos($first_tag, '<rss') !== false && else if (strpos($first_tag, '<rss') !== false &&
(strpos($first_tag, 'version="0.92"') !== false || strpos($first_tag, 'version=\'0.92\'') !== false)) { (strpos($first_tag, 'version="0.92"') !== false || strpos($first_tag, 'version=\'0.92\'') !== false)) {
Logging::log('Reader: discover RSS 0.92 feed');
require_once __DIR__.'/Parsers/Rss92.php'; require_once __DIR__.'/Parsers/Rss92.php';
return new Parsers\Rss92($this->content); return new Parsers\Rss92($this->content);
} }
else if (strpos($first_tag, '<rss') !== false && else if (strpos($first_tag, '<rss') !== false &&
(strpos($first_tag, 'version="0.91"') !== false || strpos($first_tag, 'version=\'0.91\'') !== false)) { (strpos($first_tag, 'version="0.91"') !== false || strpos($first_tag, 'version=\'0.91\'') !== false)) {
Logging::log('Reader: discover RSS 0.91 feed');
require_once __DIR__.'/Parsers/Rss91.php'; require_once __DIR__.'/Parsers/Rss91.php';
return new Parsers\Rss91($this->content); return new Parsers\Rss91($this->content);
} }
else if (strpos($first_tag, '<rdf:') !== false && strpos($first_tag, 'xmlns="http://purl.org/rss/1.0/"') !== false) { else if (strpos($first_tag, '<rdf:') !== false && strpos($first_tag, 'xmlns="http://purl.org/rss/1.0/"') !== false) {
Logging::log('Reader: discover RSS 1.0 feed');
require_once __DIR__.'/Parsers/Rss10.php'; require_once __DIR__.'/Parsers/Rss10.php';
return new Parsers\Rss10($this->content); return new Parsers\Rss10($this->content);
} }
@ -129,6 +140,8 @@ class Reader
return false; return false;
} }
Logging::log('Reader: run discover()');
\libxml_use_internal_errors(true); \libxml_use_internal_errors(true);
$dom = new \DOMDocument; $dom = new \DOMDocument;

View File

@ -2,6 +2,8 @@
namespace PicoFeed; namespace PicoFeed;
require_once __DIR__.'/Logging.php';
class RemoteResource class RemoteResource
{ {
public $user_agent; public $user_agent;
@ -69,7 +71,7 @@ class RemoteResource
public function execute() public function execute()
{ {
$response = $this->makeRequest(); $response = $this->doRequest();
$this->etag = isset($response['headers']['ETag']) ? $response['headers']['ETag'] : ''; $this->etag = isset($response['headers']['ETag']) ? $response['headers']['ETag'] : '';
$this->last_modified = isset($response['headers']['Last-Modified']) ? $response['headers']['Last-Modified'] : ''; $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_code = 200;
$http_body = ''; $http_body = '';
@ -110,6 +112,10 @@ class RemoteResource
} }
else { else {
Logging::log('Fetch URL: '.$this->url);
Logging::log('Etag: '.$this->etag);
Logging::log('Last-Modified: '.$this->last_modified);
$headers = array('Connection: close'); $headers = array('Connection: close');
if ($this->etag) $headers[] = 'If-None-Match: '.$this->etag; if ($this->etag) $headers[] = 'If-None-Match: '.$this->etag;
@ -131,6 +137,25 @@ class RemoteResource
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$http_response = curl_exec($ch); $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_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$http_body = ''; $http_body = '';
$http_headers = array(); $http_headers = array();
@ -162,6 +187,13 @@ class RemoteResource
$http_body = implode("\r\n", array_splice($lines, $i + 1)); $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( return array(
'status' => $http_code, 'status' => $http_code,
'body' => $http_body, 'body' => $http_body,