Add console to display debug messages

This commit is contained in:
Frédéric Guillot 2013-08-29 19:34:11 -04:00
parent e0bbaaa3b5
commit b6adfb7168
16 changed files with 135 additions and 64 deletions

View File

@ -175,8 +175,8 @@ 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` => default is true (enable logging of PicoFeed)
- `DEBUG_DIRECTORY` => default is /tmp (place to store log files) - `DEBUG_FILENAME` => default is `data/debug.log`
- `THEME_DIRECTORY` => default is themes - `THEME_DIRECTORY` => default is themes
- `SESSION_SAVE_PATH` => default is empty (used to store session files in a custom directory) - `SESSION_SAVE_PATH` => default is empty (used to store session files in a custom directory)

View File

@ -15,7 +15,7 @@ body {
body { body {
margin: 0 auto; margin: 0 auto;
max-width: 750px; max-width: 780px;
color: #333; color: #333;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased; -webkit-font-smoothing: antialiased;
@ -116,7 +116,7 @@ pre, code {
border: 1px solid #ccc; border: 1px solid #ccc;
font-family: monospace; font-family: monospace;
color: brown; color: brown;
line-height: 1.0em; line-height: 1.3em;
border-radius: 5px; border-radius: 5px;
} }

View File

@ -14,8 +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') or define('DEBUG', true);
defined('DEBUG_DIRECTORY') or define('DEBUG_DIRECTORY', '/tmp'); defined('DEBUG_FILENAME') or define('DEBUG_FILENAME', 'data/debug.log');
defined('THEME_DIRECTORY') or define('THEME_DIRECTORY', 'themes'); defined('THEME_DIRECTORY') or define('THEME_DIRECTORY', 'themes');
defined('SESSION_SAVE_PATH') or define('SESSION_SAVE_PATH', ''); defined('SESSION_SAVE_PATH') or define('SESSION_SAVE_PATH', '');

View File

@ -317,8 +317,6 @@ 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');
}); });
@ -403,7 +401,6 @@ 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');
}); });
@ -415,7 +412,6 @@ Router\post_action('refresh-feed', function() {
if ($id) { if ($id) {
$result = Model\update_feed($id); $result = Model\update_feed($id);
Model\write_debug();
} }
Response\json(array('feed_id' => $id, 'result' => $result)); Response\json(array('feed_id' => $id, 'result' => $result));
@ -472,7 +468,6 @@ Router\get_action('add', function() {
Router\post_action('add', function() { Router\post_action('add', function() {
$result = Model\import_feed(trim($_POST['url'])); $result = Model\import_feed(trim($_POST['url']));
Model\write_debug();
if ($result) { if ($result) {
@ -492,30 +487,6 @@ Router\post_action('add', function() {
}); });
// Re-generate tokens
Router\get_action('generate-tokens', function() {
Model\new_tokens();
Response\redirect('?action=config#api');
});
// Optimize the database manually
Router\get_action('optimize-db', function() {
\PicoTools\singleton('db')->getConnection()->exec('VACUUM');
Response\redirect('?action=config');
});
// Download the compressed database
Router\get_action('download-db', function() {
Response\force_download('db.sqlite.gz');
Response\binary(gzencode(file_get_contents(DB_FILENAME)));
});
// OPML export // OPML export
Router\get_action('export', function() { Router\get_action('export', function() {
@ -551,6 +522,48 @@ Router\post_action('import', function() {
}); });
// Re-generate tokens
Router\get_action('generate-tokens', function() {
Model\new_tokens();
Response\redirect('?action=config#api');
});
// Optimize the database manually
Router\get_action('optimize-db', function() {
\PicoTools\singleton('db')->getConnection()->exec('VACUUM');
Response\redirect('?action=config');
});
// Download the compressed database
Router\get_action('download-db', function() {
Response\force_download('db.sqlite.gz');
Response\binary(gzencode(file_get_contents(DB_FILENAME)));
});
// Flush console messages
Router\get_action('flush-console', function() {
@unlink(DEBUG_FILENAME);
Response\redirect('?action=console');
});
// Display console
Router\get_action('console', function() {
Response\html(Template\layout('console', array(
'content' => @file_get_contents(DEBUG_FILENAME),
'title' => t('Console')
)));
});
// Display preferences page // Display preferences page
Router\get_action('config', function() { Router\get_action('config', function() {

View File

@ -1,6 +1,7 @@
<?php <?php
return array( return array(
'flush messages' => 'supprimer les messages',
'API endpoint:' => 'URL de l\'API : ', 'API endpoint:' => 'URL de l\'API : ',
'API username:' => 'Identifiant pour l\'API : ', 'API username:' => 'Identifiant pour l\'API : ',
'API token:' => 'Clé pour l\'API : ', 'API token:' => 'Clé pour l\'API : ',

View File

@ -93,11 +93,13 @@ function write_debug()
{ {
if (DEBUG) { if (DEBUG) {
file_put_contents( $data = '';
DEBUG_DIRECTORY.'/miniflux_'.date('YmdH').'.debug',
var_export(\PicoFeed\Logging::$messages, true).PHP_EOL, foreach (\PicoFeed\Logging::$messages as $line) {
FILE_APPEND | LOCK_EX $data .= $line.PHP_EOL;
); }
file_put_contents(DEBUG_FILENAME, $data);
} }
} }
@ -156,9 +158,13 @@ function import_feeds($content)
$db->closeTransaction(); $db->closeTransaction();
write_debug();
return true; return true;
} }
write_debug();
return false; return false;
} }
@ -192,12 +198,15 @@ function import_feed($url)
$feed_id = $db->getConnection()->getLastId(); $feed_id = $db->getConnection()->getLastId();
update_items($feed_id, $feed->items); update_items($feed_id, $feed->items);
write_debug();
return (int) $feed_id; return (int) $feed_id;
} }
} }
} }
write_debug();
return false; return false;
} }
@ -212,6 +221,8 @@ function update_feeds($limit = LIMIT_ALL)
// Auto-vacuum for people using the cronjob // Auto-vacuum for people using the cronjob
\PicoTools\singleton('db')->getConnection()->exec('VACUUM'); \PicoTools\singleton('db')->getConnection()->exec('VACUUM');
return true;
} }
@ -233,7 +244,10 @@ function update_feed($feed_id)
// Update the `last_checked` column each time, HTTP cache or not // Update the `last_checked` column each time, HTTP cache or not
update_feed_last_checked($feed_id); update_feed_last_checked($feed_id);
if (! $resource->isModified()) return true; if (! $resource->isModified()) {
write_debug();
return true;
}
$parser = $reader->getParser(); $parser = $reader->getParser();
@ -245,11 +259,14 @@ function update_feed($feed_id)
update_feed_cache_infos($feed_id, $resource->getLastModified(), $resource->getEtag()); update_feed_cache_infos($feed_id, $resource->getLastModified(), $resource->getEtag());
update_items($feed_id, $feed->items); update_items($feed_id, $feed->items);
write_debug();
return true; return true;
} }
} }
write_debug();
return false; return false;
} }

View File

@ -63,6 +63,7 @@
<ul> <ul>
<li><?= t('Miniflux version:') ?> <strong><?= APP_VERSION ?></strong></li> <li><?= t('Miniflux version:') ?> <strong><?= APP_VERSION ?></strong></li>
<li><?= t('Official website:') ?> <a href="http://miniflux.net" target="_blank">http://miniflux.net</a></li> <li><?= t('Official website:') ?> <a href="http://miniflux.net" target="_blank">http://miniflux.net</a></li>
<li><a href="?action=console"><?= t('Console') ?></a></li>
</ul> </ul>
</div> </div>
</section> </section>

13
templates/console.php Normal file
View File

@ -0,0 +1,13 @@
<div class="page-header">
<h2><?= t('Console') ?></h2>
<ul>
<li><a href="?action=console"><?= t('refresh') ?></a></li>
<li><a href="?action=flush-console"><?= t('flush messages') ?></a></li>
</ul>
</div>
<?php if (empty($content)): ?>
<p class="alert alert-info"><?= t('No messages') ?></p>
<?php else: ?>
<pre id="console"><code><?= Helper\escape($content) ?></code></pre>
<?php endif ?>

View File

@ -46,9 +46,9 @@ abstract class Client
throw new \LogicException('The URL is missing'); throw new \LogicException('The URL is missing');
} }
Logging::log('Fetch URL: '.$this->url); Logging::log(\get_called_class().' Fetch URL: '.$this->url);
Logging::log('Etag: '.$this->etag); Logging::log(\get_called_class().' Etag provided: '.$this->etag);
Logging::log('Last-Modified: '.$this->last_modified); Logging::log(\get_called_class().' Last-Modified provided: '.$this->last_modified);
$response = $this->doRequest(); $response = $this->doRequest();
@ -84,10 +84,10 @@ abstract class Client
} }
} }
Logging::log('HTTP status code: '.$status); Logging::log(\get_called_class().' HTTP status code: '.$status);
foreach ($headers as $name => $value) { foreach ($headers as $name => $value) {
Logging::log('HTTP headers: '.$name.' => '.$value); Logging::log(\get_called_class().' HTTP headers: '.$name.' => '.$value);
} }
return array($status, $headers); return array($status, $headers);

View File

@ -66,14 +66,14 @@ class Curl extends \PicoFeed\Client
curl_setopt($ch, CURLOPT_HEADERFUNCTION, array($this, 'readHeaders')); curl_setopt($ch, CURLOPT_HEADERFUNCTION, array($this, 'readHeaders'));
curl_exec($ch); curl_exec($ch);
Logging::log('cURL total time: '.curl_getinfo($ch, CURLINFO_TOTAL_TIME)); Logging::log(\get_called_class().' cURL total time: '.curl_getinfo($ch, CURLINFO_TOTAL_TIME));
Logging::log('cURL dns lookup time: '.curl_getinfo($ch, CURLINFO_NAMELOOKUP_TIME)); Logging::log(\get_called_class().' cURL dns lookup time: '.curl_getinfo($ch, CURLINFO_NAMELOOKUP_TIME));
Logging::log('cURL connect time: '.curl_getinfo($ch, CURLINFO_CONNECT_TIME)); Logging::log(\get_called_class().' cURL connect time: '.curl_getinfo($ch, CURLINFO_CONNECT_TIME));
Logging::log('cURL speed download: '.curl_getinfo($ch, CURLINFO_SPEED_DOWNLOAD)); Logging::log(\get_called_class().' cURL speed download: '.curl_getinfo($ch, CURLINFO_SPEED_DOWNLOAD));
if (curl_errno($ch)) { if (curl_errno($ch)) {
Logging::log('cURL error: '.curl_error($ch)); Logging::log(\get_called_class().' cURL error: '.curl_error($ch));
curl_close($ch); curl_close($ch);
return false; return false;

View File

@ -16,6 +16,8 @@ class Import
public function execute() public function execute()
{ {
\PicoFeed\Logging::log(\get_called_class().': start importation');
try { try {
\libxml_use_internal_errors(true); \libxml_use_internal_errors(true);
@ -23,14 +25,16 @@ class Import
$xml = new \SimpleXMLElement(trim($this->content)); $xml = new \SimpleXMLElement(trim($this->content));
if ($xml->getName() !== 'opml' || ! isset($xml->body)) { if ($xml->getName() !== 'opml' || ! isset($xml->body)) {
\PicoFeed\Logging::log(\get_called_class().': OPML tag not found');
return false; return false;
} }
$this->parseEntries($xml->body); $this->parseEntries($xml->body);
\PicoFeed\Logging::log(\get_called_class().': '.count($this->items).' subscriptions found');
} }
catch (\Exception $e) { catch (\Exception $e) {
\PicoFeed\Logging::log(\get_called_class().': '.$e->getMessage());
return false; return false;
} }

View File

@ -8,6 +8,6 @@ class Logging
public static function log($message) public static function log($message)
{ {
self::$messages[] = $message; self::$messages[] = '['.date('Y-m-d H:i:s').'] '.$message;
} }
} }

View File

@ -6,11 +6,13 @@ class Atom extends \PicoFeed\Parser
{ {
public function execute() public function execute()
{ {
\PicoFeed\Logging::log(\get_called_class().': begin parsing');
\libxml_use_internal_errors(true); \libxml_use_internal_errors(true);
$xml = \simplexml_load_string($this->content); $xml = \simplexml_load_string($this->content);
if ($xml === false) { if ($xml === false) {
\PicoFeed\Logging::log(\get_called_class().': XML parsing error');
\PicoFeed\Logging::log($this->getXmlErrors()); \PicoFeed\Logging::log($this->getXmlErrors());
return false; return false;
} }
@ -43,6 +45,8 @@ class Atom extends \PicoFeed\Parser
$this->items[] = $item; $this->items[] = $item;
} }
\PicoFeed\Logging::log(\get_called_class().': parsing finished ('.count($this->items).' items)');
return $this; return $this;
} }

View File

@ -6,11 +6,13 @@ class Rss10 extends \PicoFeed\Parser
{ {
public function execute() public function execute()
{ {
\PicoFeed\Logging::log(\get_called_class().': begin parsing');
\libxml_use_internal_errors(true); \libxml_use_internal_errors(true);
$xml = \simplexml_load_string($this->content); $xml = \simplexml_load_string($this->content);
if ($xml === false) { if ($xml === false) {
\PicoFeed\Logging::log(\get_called_class().': XML parsing error');
\PicoFeed\Logging::log($this->getXmlErrors()); \PicoFeed\Logging::log($this->getXmlErrors());
return false; return false;
} }
@ -78,6 +80,8 @@ class Rss10 extends \PicoFeed\Parser
$this->items[] = $item; $this->items[] = $item;
} }
\PicoFeed\Logging::log(\get_called_class().': parsing finished ('.count($this->items).' items)');
return $this; return $this;
} }
} }

View File

@ -6,11 +6,13 @@ class Rss20 extends \PicoFeed\Parser
{ {
public function execute() public function execute()
{ {
\PicoFeed\Logging::log(\get_called_class().': begin parsing');
\libxml_use_internal_errors(true); \libxml_use_internal_errors(true);
$xml = \simplexml_load_string($this->content); $xml = \simplexml_load_string($this->content);
if ($xml === false) { if ($xml === false) {
\PicoFeed\Logging::log(\get_called_class().': XML parsing error');
\PicoFeed\Logging::log($this->getXmlErrors()); \PicoFeed\Logging::log($this->getXmlErrors());
return false; return false;
} }
@ -41,7 +43,10 @@ class Rss20 extends \PicoFeed\Parser
$this->updated = $this->updated ? strtotime($this->updated) : time(); $this->updated = $this->updated ? strtotime($this->updated) : time();
// RSS feed might be empty // RSS feed might be empty
if (! $xml->channel->item) return $this; if (! $xml->channel->item) {
\PicoFeed\Logging::log(\get_called_class().': feed empty or malformed');
return $this;
}
foreach ($xml->channel->item as $entry) { foreach ($xml->channel->item as $entry) {
@ -108,6 +113,8 @@ class Rss20 extends \PicoFeed\Parser
$this->items[] = $item; $this->items[] = $item;
} }
\PicoFeed\Logging::log(\get_called_class().': parsing finished ('.count($this->items).' items)');
return $this; return $this;
} }
} }

View File

@ -87,7 +87,7 @@ class Reader
if (strpos($first_tag, '<feed') !== false) { if (strpos($first_tag, '<feed') !== false) {
Logging::log('Reader: discover Atom feed'); Logging::log(\get_called_class().': 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);
@ -95,7 +95,7 @@ class Reader
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'); Logging::log(\get_called_class().': 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);
@ -103,7 +103,7 @@ class Reader
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'); Logging::log(\get_called_class().': 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);
@ -111,20 +111,23 @@ class Reader
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'); Logging::log(\get_called_class().': 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'); Logging::log(\get_called_class().': 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);
} }
else if ($discover === true) { else if ($discover === true) {
Logging::log(\get_called_class().': Format not supported or malformed');
Logging::log(\get_called_class().':'.PHP_EOL.$this->content);
return false; return false;
} }
else if ($this->discover()) { else if ($this->discover()) {
@ -132,6 +135,9 @@ class Reader
return $this->getParser(true); return $this->getParser(true);
} }
Logging::log(\get_called_class().': Subscription not found');
Logging::log(\get_called_class().': Content => '.PHP_EOL.$this->content);
return false; return false;
} }
@ -143,7 +149,7 @@ class Reader
return false; return false;
} }
Logging::log('Reader: run discover()'); Logging::log(\get_called_class().': Try to discover a subscription');
\libxml_use_internal_errors(true); \libxml_use_internal_errors(true);
@ -176,6 +182,7 @@ class Reader
$link = $this->url.$link; $link = $this->url.$link;
} }
Logging::log(\get_called_class().': Find subscription link: '.$link);
$this->download($link); $this->download($link);
return true; return true;