core: Add separate Debug class

Replaces 'debugMessage' by specialized debug function 'Debug::log'.
This function takes the same arguments as the previous 'debugMessage'.

A separate Debug class allows for further optimization and separation
of concern.
This commit is contained in:
logmanoriginal 2018-11-10 20:03:03 +01:00
parent 9379854f7a
commit c63af2e7ad
8 changed files with 44 additions and 40 deletions

View file

@ -132,7 +132,7 @@ class JustETFBridge extends BridgeAbstract {
date_time_set($df, 0, 0);
// debugMessage(date_format($df, 'U'));
// Debug::log(date_format($df, 'U'));
return date_format($df, 'U');
}
@ -210,7 +210,7 @@ class JustETFBridge extends BridgeAbstract {
$element = $article->find('div.subheadline', 0)
or returnServerError('Date not found!');
// debugMessage($element->plaintext);
// Debug::log($element->plaintext);
$date = trim(explode('|', $element->plaintext)[0]);
@ -223,7 +223,7 @@ class JustETFBridge extends BridgeAbstract {
$element->find('a', 0)->onclick = '';
// debugMessage($element->innertext);
// Debug::log($element->innertext);
return $element->innertext;
}
@ -288,7 +288,7 @@ class JustETFBridge extends BridgeAbstract {
$element = $html->find('div.infobox div.vallabel', 0)
or returnServerError('Date not found!');
// debugMessage($element->plaintext);
// Debug::log($element->plaintext);
$date = trim(explode("\r\n", $element->plaintext)[1]);

View file

@ -74,7 +74,7 @@ class WordPressPluginUpdateBridge extends BridgeAbstract {
}
private function getCachedDate($url){
debugMessage('getting pubdate from url ' . $url . '');
Debug::log('getting pubdate from url ' . $url . '');
// Initialize cache
$cache = Cache::create('FileCache');
$cache->setPath(PATH_CACHE . 'pages/');

View file

@ -453,7 +453,7 @@ class XenForoBridge extends BridgeAbstract {
}
// debugMessage(date_format($df, 'U'));
// Debug::log(date_format($df, 'U'));
return date_format($df, 'U');

19
lib/Debug.php Normal file
View file

@ -0,0 +1,19 @@
<?php
class Debug {
public static function log($text) {
if(!DEBUG) {
return;
}
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 3);
$calling = $backtrace[2];
$message = $calling['file'] . ':'
. $calling['line'] . ' class '
. (isset($calling['class']) ? $calling['class'] : '<no-class>') . '->'
. $calling['function'] . ' - '
. $text;
error_log($message);
}
}

View file

@ -11,7 +11,7 @@ abstract class FeedExpander extends BridgeAbstract {
returnServerError('There is no $url for this RSS expander');
}
debugMessage('Loading from ' . $url);
Debug::log('Loading from ' . $url);
/* Notice we do not use cache here on purpose:
* we want a fresh view of the RSS stream each time
@ -20,34 +20,34 @@ abstract class FeedExpander extends BridgeAbstract {
or returnServerError('Could not request ' . $url);
$rssContent = simplexml_load_string(trim($content));
debugMessage('Detecting feed format/version');
Debug::log('Detecting feed format/version');
switch(true) {
case isset($rssContent->item[0]):
debugMessage('Detected RSS 1.0 format');
Debug::log('Detected RSS 1.0 format');
$this->feedType = 'RSS_1_0';
break;
case isset($rssContent->channel[0]):
debugMessage('Detected RSS 0.9x or 2.0 format');
Debug::log('Detected RSS 0.9x or 2.0 format');
$this->feedType = 'RSS_2_0';
break;
case isset($rssContent->entry[0]):
debugMessage('Detected ATOM format');
Debug::log('Detected ATOM format');
$this->feedType = 'ATOM_1_0';
break;
default:
debugMessage('Unknown feed format/version');
Debug::log('Unknown feed format/version');
returnServerError('The feed format is unknown!');
break;
}
debugMessage('Calling function "collect_' . $this->feedType . '_data"');
Debug::log('Calling function "collect_' . $this->feedType . '_data"');
$this->{'collect_' . $this->feedType . '_data'}($rssContent, $maxItems);
}
protected function collect_RSS_1_0_data($rssContent, $maxItems){
$this->load_RSS_2_0_feed_data($rssContent->channel[0]);
foreach($rssContent->item as $item) {
debugMessage('parsing item ' . var_export($item, true));
Debug::log('parsing item ' . var_export($item, true));
$tmp_item = $this->parseItem($item);
if (!empty($tmp_item)) {
$this->items[] = $tmp_item;
@ -58,13 +58,13 @@ abstract class FeedExpander extends BridgeAbstract {
protected function collect_RSS_2_0_data($rssContent, $maxItems){
$rssContent = $rssContent->channel[0];
debugMessage('RSS content is ===========\n'
Debug::log('RSS content is ===========\n'
. var_export($rssContent, true)
. '===========');
$this->load_RSS_2_0_feed_data($rssContent);
foreach($rssContent->item as $item) {
debugMessage('parsing item ' . var_export($item, true));
Debug::log('parsing item ' . var_export($item, true));
$tmp_item = $this->parseItem($item);
if (!empty($tmp_item)) {
$this->items[] = $tmp_item;
@ -76,7 +76,7 @@ abstract class FeedExpander extends BridgeAbstract {
protected function collect_ATOM_1_0_data($content, $maxItems){
$this->load_ATOM_feed_data($content);
foreach($content->entry as $item) {
debugMessage('parsing item ' . var_export($item, true));
Debug::log('parsing item ' . var_export($item, true));
$tmp_item = $this->parseItem($item);
if (!empty($tmp_item)) {
$this->items[] = $tmp_item;

View file

@ -1,6 +1,6 @@
<?php
function getContents($url, $header = array(), $opts = array()){
debugMessage('Reading contents from "' . $url . '"');
Debug::log('Reading contents from "' . $url . '"');
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
@ -8,7 +8,7 @@ function getContents($url, $header = array(), $opts = array()){
if(is_array($header) && count($header) !== 0) {
debugMessage('Setting headers: ' . json_encode($header));
Debug::log('Setting headers: ' . json_encode($header));
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
}
@ -19,7 +19,7 @@ function getContents($url, $header = array(), $opts = array()){
if(is_array($opts) && count($opts) !== 0) {
debugMessage('Setting options: ' . json_encode($opts));
Debug::log('Setting options: ' . json_encode($opts));
foreach($opts as $key => $value) {
curl_setopt($ch, $key, $value);
@ -29,7 +29,7 @@ function getContents($url, $header = array(), $opts = array()){
if(defined('PROXY_URL') && !defined('NOPROXY')) {
debugMessage('Setting proxy url: ' . PROXY_URL);
Debug::log('Setting proxy url: ' . PROXY_URL);
curl_setopt($ch, CURLOPT_PROXY, PROXY_URL);
}
@ -42,13 +42,13 @@ function getContents($url, $header = array(), $opts = array()){
$curlErrno = curl_errno($ch);
if($data === false)
debugMessage('Cant\'t download ' . $url . ' cUrl error: ' . $curlError . ' (' . $curlErrno . ')');
Debug::log('Cant\'t download ' . $url . ' cUrl error: ' . $curlError . ' (' . $curlErrno . ')');
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$errorCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$header = substr($data, 0, $headerSize);
debugMessage('Response header: ' . $header);
Debug::log('Response header: ' . $header);
$headers = parseResponseHeader($header);
$finalHeader = end($headers);
@ -109,7 +109,7 @@ $target_charset = DEFAULT_TARGET_CHARSET,
$stripRN = true,
$defaultBRText = DEFAULT_BR_TEXT,
$defaultSpanText = DEFAULT_SPAN_TEXT){
debugMessage('Caching url ' . $url . ', duration ' . $duration);
Debug::log('Caching url ' . $url . ', duration ' . $duration);
// Initialize cache
$cache = Cache::create('FileCache');

View file

@ -10,19 +10,3 @@ function returnClientError($message){
function returnServerError($message){
returnError($message, 500);
}
function debugMessage($text){
if(!file_exists('DEBUG')) {
return;
}
$backtrace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 3);
$calling = $backtrace[2];
$message = $calling['file'] . ':'
. $calling['line'] . ' class '
. (isset($calling['class']) ? $calling['class'] : '<no-class>') . '->'
. $calling['function'] . ' - '
. $text;
error_log($message);
}

View file

@ -15,6 +15,7 @@ require_once PATH_LIB . 'CacheInterface.php';
require_once PATH_LIB . 'FormatInterface.php';
// Classes
require_once PATH_LIB . 'Debug.php';
require_once PATH_LIB . 'Exceptions.php';
require_once PATH_LIB . 'Format.php';
require_once PATH_LIB . 'FormatAbstract.php';