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

View File

@ -15,7 +15,7 @@ body {
body {
margin: 0 auto;
max-width: 750px;
max-width: 780px;
color: #333;
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
@ -116,7 +116,7 @@ pre, code {
border: 1px solid #ccc;
font-family: monospace;
color: brown;
line-height: 1.0em;
line-height: 1.3em;
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('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');
defined('DEBUG') or define('DEBUG', true);
defined('DEBUG_FILENAME') or define('DEBUG_FILENAME', 'data/debug.log');
defined('THEME_DIRECTORY') or define('THEME_DIRECTORY', 'themes');
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() {
Model\update_feeds();
Model\write_debug();
Session\flash(t('Your subscriptions are updated'));
Response\redirect('?action=unread');
});
@ -403,7 +401,6 @@ 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');
});
@ -415,7 +412,6 @@ Router\post_action('refresh-feed', function() {
if ($id) {
$result = Model\update_feed($id);
Model\write_debug();
}
Response\json(array('feed_id' => $id, 'result' => $result));
@ -472,7 +468,6 @@ Router\get_action('add', function() {
Router\post_action('add', function() {
$result = Model\import_feed(trim($_POST['url']));
Model\write_debug();
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
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
Router\get_action('config', function() {

View File

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

View File

@ -93,11 +93,13 @@ 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
);
$data = '';
foreach (\PicoFeed\Logging::$messages as $line) {
$data .= $line.PHP_EOL;
}
file_put_contents(DEBUG_FILENAME, $data);
}
}
@ -156,9 +158,13 @@ function import_feeds($content)
$db->closeTransaction();
write_debug();
return true;
}
write_debug();
return false;
}
@ -192,12 +198,15 @@ function import_feed($url)
$feed_id = $db->getConnection()->getLastId();
update_items($feed_id, $feed->items);
write_debug();
return (int) $feed_id;
}
}
}
write_debug();
return false;
}
@ -212,6 +221,8 @@ function update_feeds($limit = LIMIT_ALL)
// Auto-vacuum for people using the cronjob
\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_feed_last_checked($feed_id);
if (! $resource->isModified()) return true;
if (! $resource->isModified()) {
write_debug();
return true;
}
$parser = $reader->getParser();
@ -245,11 +259,14 @@ function update_feed($feed_id)
update_feed_cache_infos($feed_id, $resource->getLastModified(), $resource->getEtag());
update_items($feed_id, $feed->items);
write_debug();
return true;
}
}
write_debug();
return false;
}

View File

@ -63,6 +63,7 @@
<ul>
<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><a href="?action=console"><?= t('Console') ?></a></li>
</ul>
</div>
</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');
}
Logging::log('Fetch URL: '.$this->url);
Logging::log('Etag: '.$this->etag);
Logging::log('Last-Modified: '.$this->last_modified);
Logging::log(\get_called_class().' Fetch URL: '.$this->url);
Logging::log(\get_called_class().' Etag provided: '.$this->etag);
Logging::log(\get_called_class().' Last-Modified provided: '.$this->last_modified);
$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) {
Logging::log('HTTP headers: '.$name.' => '.$value);
Logging::log(\get_called_class().' HTTP headers: '.$name.' => '.$value);
}
return array($status, $headers);

View File

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

View File

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

View File

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

View File

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

View File

@ -6,11 +6,13 @@ class Rss20 extends \PicoFeed\Parser
{
public function execute()
{
\PicoFeed\Logging::log(\get_called_class().': begin parsing');
\libxml_use_internal_errors(true);
$xml = \simplexml_load_string($this->content);
if ($xml === false) {
\PicoFeed\Logging::log(\get_called_class().': XML parsing error');
\PicoFeed\Logging::log($this->getXmlErrors());
return false;
}
@ -41,7 +43,10 @@ class Rss20 extends \PicoFeed\Parser
$this->updated = $this->updated ? strtotime($this->updated) : time();
// 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) {
@ -108,6 +113,8 @@ class Rss20 extends \PicoFeed\Parser
$this->items[] = $item;
}
\PicoFeed\Logging::log(\get_called_class().': parsing finished ('.count($this->items).' items)');
return $this;
}
}

View File

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