Skip to content

Commit

Permalink
Improve logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Frédéric Guillot committed Aug 30, 2013
1 parent 00f7fe3 commit 9d9d44f
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 24 deletions.
12 changes: 6 additions & 6 deletions lib/PicoFeed/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,17 @@ public function execute()
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();

if (is_array($response)) {

if ($response['status'] == 304) {
$this->is_modified = false;
Logging::log(\get_called_class().' Resource not modified');
}
else {
$this->etag = isset($response['headers']['ETag']) ? $response['headers']['ETag'] : '';
Expand All @@ -74,7 +75,6 @@ public function parseHeaders(array $lines)
foreach ($lines as $line) {

if (strpos($line, 'HTTP') === 0/* && strpos($line, '301') === false && strpos($line, '302') === false*/) {

$status = (int) substr($line, 9, 3);
}
else if (strpos($line, ':') !== false) {
Expand All @@ -84,10 +84,10 @@ public function parseHeaders(array $lines)
}
}

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 header: '.$name.' => '.$value);
}

return array($status, $headers);
Expand Down
10 changes: 5 additions & 5 deletions lib/PicoFeed/Clients/Curl.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,14 @@ public function doRequest($follow_location = true)
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;
Expand Down
8 changes: 6 additions & 2 deletions lib/PicoFeed/Import.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,25 @@ public function __construct($content)

public function execute()
{
\PicoFeed\Logging::log(\get_called_class().': start importation');

try {

\libxml_use_internal_errors(true);

$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;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/PicoFeed/Logging.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ class Logging

public static function log($message)
{
self::$messages[] = $message;
self::$messages[] = '['.date('Y-m-d H:i:s').'] '.$message;
}
}
6 changes: 5 additions & 1 deletion lib/PicoFeed/Parsers/Atom.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -43,6 +45,8 @@ public function execute()
$this->items[] = $item;
}

\PicoFeed\Logging::log(\get_called_class().': parsing finished ('.count($this->items).' items)');

return $this;
}

Expand Down
6 changes: 5 additions & 1 deletion lib/PicoFeed/Parsers/Rss10.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -78,6 +80,8 @@ public function execute()
$this->items[] = $item;
}

\PicoFeed\Logging::log(\get_called_class().': parsing finished ('.count($this->items).' items)');

return $this;
}
}
11 changes: 9 additions & 2 deletions lib/PicoFeed/Parsers/Rss20.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -41,7 +43,10 @@ public function execute()
$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) {

Expand Down Expand Up @@ -108,6 +113,8 @@ public function execute()
$this->items[] = $item;
}

\PicoFeed\Logging::log(\get_called_class().': parsing finished ('.count($this->items).' items)');

return $this;
}
}
19 changes: 13 additions & 6 deletions lib/PicoFeed/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,51 +87,57 @@ public function getParser($discover = 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';
return new Parsers\Atom($this->content);
}
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);
}
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);
}
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()) {

return $this->getParser(true);
}

Logging::log(\get_called_class().': Subscription not found');
Logging::log(\get_called_class().': Content => '.PHP_EOL.$this->content);

return false;
}

Expand All @@ -143,7 +149,7 @@ public function discover()
return false;
}

Logging::log('Reader: run discover()');
Logging::log(\get_called_class().': Try to discover a subscription');

\libxml_use_internal_errors(true);

Expand Down Expand Up @@ -176,6 +182,7 @@ public function discover()
$link = $this->url.$link;
}

Logging::log(\get_called_class().': Find subscription link: '.$link);
$this->download($link);

return true;
Expand Down

0 comments on commit 9d9d44f

Please sign in to comment.