Implemented Daemon Logger

This commit is contained in:
Paul Kirby 2018-10-29 16:55:26 -05:00
parent f884a7b674
commit a448921a8c
6 changed files with 236 additions and 104 deletions

1
.gitignore vendored
View file

@ -6,6 +6,7 @@
.DS_Store .DS_Store
tmp/* tmp/*
logs/*
!*.gitkeep !*.gitkeep

View file

@ -9,10 +9,18 @@ class LBRY_Daemon
{ {
private $address = 'localhost:5279'; private $address = 'localhost:5279';
private $logger = null;
/** /**
* LBRY Daemon Object constructor * LBRY Daemon Object constructor
*/ */
public function __construct() public function __construct()
{
$this->logger = new LBRY_Daemon_Logger();
$this->init();
}
public function init()
{ {
} }
@ -23,8 +31,14 @@ class LBRY_Daemon
*/ */
public function wallet_unused_address() public function wallet_unused_address()
{ {
try {
$result = $this->request('wallet_unused_address'); $result = $this->request('wallet_unused_address');
return $result->result; return $result->result;
} catch (LBRYDaemonException $e) {
$this->logger->log('wallet_unused_address error', $e->getMessage() . ' | Code: ' . $e->getCode());
LBRY()->notice->set_notice('error', 'Issue getting unused wallet address.');
return;
}
} }
/** /**
@ -35,8 +49,14 @@ class LBRY_Daemon
*/ */
public function wallet_balance() public function wallet_balance()
{ {
try {
$result = $this->request('wallet_balance'); $result = $this->request('wallet_balance');
return $result->result; return $result->result;
} catch (LBRYDaemonException $e) {
$this->logger->log('wallet_balance error', $e->getMessage() . ' | Code: ' . $e->getCode());
LBRY()->notice->set_notice('error', 'Issue getting wallet address.');
return;
}
} }
/** /**
@ -45,8 +65,14 @@ class LBRY_Daemon
*/ */
public function channel_list() public function channel_list()
{ {
try {
$result = $this->request('channel_list')->result; $result = $this->request('channel_list')->result;
return empty($result) ? null : $result; return empty($result) ? null : $result;
} catch (LBRYDaemonException $e) {
$this->logger->log('channel_list error', $e->getMessage() . ' | Code: ' . $e->getCode());
LBRY()->notice->set_notice('error', 'Issue retrieving channel list.');
return;
}
} }
/** /**
@ -69,6 +95,7 @@ class LBRY_Daemon
$channel_name = '@' . $channel_name; $channel_name = '@' . $channel_name;
try {
$result = $this->request( $result = $this->request(
'channel_new', 'channel_new',
array( array(
@ -76,8 +103,12 @@ class LBRY_Daemon
'amount' => floatval($bid_amount) 'amount' => floatval($bid_amount)
) )
); );
$this->check_for_errors($result);
return $result->result; return $result->result;
} catch (LBRYDaemonException $e) {
$this->logger->log('channel_new error', $e->getMessage() . ' | Code: ' . $e->getCode());
LBRY()->notice->set_notice('error', 'Issue creating new channel.');
return;
}
} }
/** /**
@ -107,13 +138,17 @@ class LBRY_Daemon
} }
// TODO: Bring thumbnails into the mix // TODO: Bring thumbnails into the mix
try {
$result = $this->request( $result = $this->request(
'publish', 'publish',
$args $args
); );
return $result->result;
$this->check_for_errors($result); } catch (LBRYDaemonException $e) {
return $result; $this->logger->log('wallet_unused_address error', $e->getMessage() . ' | Code: ' . $e->getCode());
LBRY()->notice->set_notice('error', 'Issue publishing / updating post to LBRY Network.');
return;
}
} }
/** /**
@ -143,8 +178,18 @@ class LBRY_Daemon
curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_HEADER, 0);
$result = curl_exec($ch); $result = curl_exec($ch);
$response_code = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
curl_close($ch); curl_close($ch);
return json_decode($result);
if ($response_code != '200') {
$this->logger->log("Damon Connection Issue", "Daemon connection returned response code $response_code");
throw new LBRYDaemonException("Daemon Connection Issue", $response_code);
}
$result = json_decode($result);
$this->check_for_errors($result);
return $result;
} }
/** /**
@ -154,7 +199,10 @@ class LBRY_Daemon
private function check_for_errors($response) private function check_for_errors($response)
{ {
if (property_exists($response, 'error')) { if (property_exists($response, 'error')) {
throw new \Exception($response->error->message, $response->error->code); $message = $response->error->message;
$code = $response->error->code;
$this->logger->log("Daemon error code $code", $message);
throw new LBRYDaemonException($message, $code);
} }
} }
@ -162,31 +210,39 @@ class LBRY_Daemon
* Temporary placeholder function for daemon. Not currently in use. * Temporary placeholder function for daemon. Not currently in use.
* @return [type] [description] * @return [type] [description]
*/ */
private function download_daemon() // private function download_daemon()
// {
// $output_filename = "lbrydaemon";
//
// // HACK: Shouldn't just directly download, need to know OS, etc
// // TODO: Make sure we are only installing if not there or corrupted
// $host = "http://build.lbry.io/daemon/build-6788_commit-5099e19_branch-lbryum-refactor/mac/lbrynet";
// $fp = fopen(LBRY_URI . '/' . $output_filename, 'w+');
// $ch = curl_init();
// curl_setopt($ch, CURLOPT_URL, $host);
// curl_setopt($ch, CURLOPT_VERBOSE, 1);
// curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// curl_setopt($ch, CURLOPT_FILE, $fp);
// curl_setopt($ch, CURLOPT_AUTOREFERER, false);
// curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
// curl_setopt($ch, CURLOPT_HEADER, 0);
//
// $result = curl_exec($ch);
// curl_close($ch);
// fclose($fp);
//
// $filepath = LBRY_URI . '/' . $output_filename;
//
// `chmod +x {$filepath}`;
// error_log(`{$filepath} status`);
// `{$filepath} start &`;
// }
}
class LBRYDaemonException extends Exception
{ {
$output_filename = "lbrydaemon"; public function __contstruct($message = '', $code = 0, Exception $previous = null)
{
// HACK: Shouldn't just directly download, need to know OS, etc parent::__construct($message, $code, $previous);
// TODO: Make sure we are only installing if not there or corrupted
$host = "http://build.lbry.io/daemon/build-6788_commit-5099e19_branch-lbryum-refactor/mac/lbrynet";
$fp = fopen(LBRY_URI . '/' . $output_filename, 'w+');
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $host);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_AUTOREFERER, false);
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_HEADER, 0);
$result = curl_exec($ch);
curl_close($ch);
fclose($fp);
$filepath = LBRY_URI . '/' . $output_filename;
`chmod +x {$filepath}`;
error_log(`{$filepath} status`);
`{$filepath} start &`;
} }
} }

View file

@ -8,9 +8,82 @@
class LBRY_Daemon_Logger class LBRY_Daemon_Logger
{ {
/** /**
* [__construct description] * The directory to log to
* @var string
*/ */
public function __construct() private $dir = LBRY_ABSPATH . 'logs/';
/**
* The filename to log to
* @var string
*/
private $filename;
/**
* The file ext
* @var string
*/
private $ext = '.log';
/**
* Max size of the file before rotating
* @var int
*/
private $maxsize = 15728640; // 15MB;
/**
* @param string $filename unique name for this logger to log to
*/
public function __construct($filename = 'daemon')
{ {
$this->filename = $filename;
}
/**
* Log to a file, with file size maximum. Adds hashs to old files
*
* @param string $event
* @param string $text
*/
public function log($event, $message = null)
{
$filepath = $this->dir . $this->filename . $this->ext;
// If our file is past our size limit, stash it
if (file_exists($filepath) && filesize($filepath) > $this->maxsize) {
rename($filepath, $this->dir . $this->filename . time() . $this->ext);
$logfiles = scandir($this->dir);
// If we have to many files, delete the oldest one
if (count($logfiles) > 7) {
$oldest = PHP_INT_MAX;
foreach ($logfiles as $file) {
if (!strstr($file, $this->filename)) {
continue;
}
$stamp = substr($file, strlen($this->filename));
$stamp = str_replace($this->ext, '', $stamp);
if ($stamp && $stamp < $oldest) {
$oldest = $stamp;
}
}
foreach ($logfiles as $file) {
if (strstr($file, $this->filename) && strstr($file, $oldest)) {
unlink($this->dir . $file);
break;
}
}
}
}
$date = date('Y-m-d H:i:s');
$space = str_repeat(' ', strlen($date) + 1);
$data = date('Y-m-d H:i:s') . " ";
$data .= "EVENT: " . $event . PHP_EOL;
$data .= $space . "MESSAGE: " . $message . PHP_EOL;
file_put_contents($filepath, $data, FILE_APPEND);
} }
} }

View file

@ -52,7 +52,7 @@ class LBRY_Speech
return; return;
} }
// error_log('======================== START ====================='); error_log('======================== START =====================');
$speech_url = get_option(LBRY_SETTINGS)[LBRY_SPEECH]; $speech_url = get_option(LBRY_SETTINGS)[LBRY_SPEECH];
@ -132,8 +132,8 @@ class LBRY_Speech
$images = empty($images[0]) ? array() : $images[0]; $images = empty($images[0]) ? array() : $images[0];
$videos = empty($videos[0]) ? array() : $videos[0]; $videos = empty($videos[0]) ? array() : $videos[0];
// error_log(print_r($images, true)); error_log(print_r($images, true));
// error_log(print_r($videos, true)); error_log(print_r($videos, true));
// TODO: only create media objects if hasn't been uploaded. IE check meta here // TODO: only create media objects if hasn't been uploaded. IE check meta here
// Throw each image into a media object // Throw each image into a media object
@ -142,16 +142,18 @@ class LBRY_Speech
// Looks for wp image class first, if not, pull id from source // Looks for wp image class first, if not, pull id from source
if (preg_match('/wp-image-([0-9]+)/i', $image, $class_id)) { if (preg_match('/wp-image-([0-9]+)/i', $image, $class_id)) {
$attachment_id = absint($class_id[1]); $attachment_id = absint($class_id[1]);
// error_log('found with wp-image: ' . $attachment_id); error_log('found with wp-image: ' . $attachment_id);
} elseif (preg_match('/src="((?:https?:)?\/\/[^"]+)"/', $image, $src) && $this->is_local($src[1])) { } elseif (preg_match('/src="((?:https?:)?\/\/[^"]+)"/', $image, $src) && $this->is_local($src[1])) {
$attachment_id = $this->rigid_attachment_url_to_postid($src[1]); $attachment_id = $this->rigid_attachment_url_to_postid($src[1]);
// error_log('found with url: ' . $attachment_id); error_log('found with url: ' . $attachment_id);
} }
if ($attachment_id) { if ($attachment_id) {
// Create main image media object // Create main image media object
$meta = wp_get_attachment_metadata($attachment_id); $meta = wp_get_attachment_metadata($attachment_id);
error_log(print_r($meta, true));
// If we don't have meta, get out because none of this will work // If we don't have meta, get out because none of this will work
if (!$meta) { if (!$meta) {
break; break;

View file

@ -26,6 +26,8 @@ class LBRY_Speech_Parser
$new_sources = $sources; $new_sources = $sources;
$sizes = $image_meta['sizes']; $sizes = $image_meta['sizes'];
error_log(print_r($image_meta, true));
foreach ($sources as $width => $source) { foreach ($sources as $width => $source) {
$speech_url = $this->find_speech_url($sizes, $width); $speech_url = $this->find_speech_url($sizes, $width);
@ -43,51 +45,51 @@ class LBRY_Speech_Parser
public function replace_urls_with_speech($content) public function replace_urls_with_speech($content)
{ {
// Find all images // // Find all images
preg_match_all('/<img [^>]+>/', $content, $images); // preg_match_all('/<img [^>]+>/', $content, $images);
//
// Check to make sure we have results // // Check to make sure we have results
$images = empty($images[0]) ? array() : $images[0]; // $images = empty($images[0]) ? array() : $images[0];
//
foreach ($images as $image) { // foreach ($images as $image) {
$attachment_id = null; // $attachment_id = null;
// Looks for wp image class first, if not, pull id from source // // Looks for wp image class first, if not, pull id from source
if (preg_match('/wp-image-([0-9]+)/i', $image, $class_id)) { // if (preg_match('/wp-image-([0-9]+)/i', $image, $class_id)) {
$attachment_id = absint($class_id[1]); // $attachment_id = absint($class_id[1]);
} elseif (preg_match('/src="((?:https?:)?\/\/[^"]+)"/', $image, $src) && $this->is_local($src[1])) { // } elseif (preg_match('/src="((?:https?:)?\/\/[^"]+)"/', $image, $src) && $this->is_local($src[1])) {
$attachment_id = $this->rigid_attachment_url_to_postid($src[1]); // $attachment_id = $this->rigid_attachment_url_to_postid($src[1]);
} // }
//
// Look for size class // // Look for size class
if (!preg_match('/wp-image-([0-9]+)/i', $image, $class_id)) { // if (!preg_match('/wp-image-([0-9]+)/i', $image, $class_id)) {
} // }
if ($attachment_id) { //
// Create main image media object // if ($attachment_id) {
$meta = wp_get_attachment_metadata($attachment_id); // // Create main image media object
// $meta = wp_get_attachment_metadata($attachment_id);
// If we don't have meta, get out because none of this will work //
if (!$meta) { // // If we don't have meta, get out because none of this will work
break; // if (!$meta) {
} // break;
// }
if (!$this->is_published($meta)) { //
$all_media[] = new LBRY_Speech_Media($attachment_id); // if (!$this->is_published($meta)) {
} // $all_media[] = new LBRY_Speech_Media($attachment_id);
// }
// COMBAK: find a way to make this more efficient? //
// Create a media object for each image size // // COMBAK: find a way to make this more efficient?
// Get images sizes for this attachment, as not all image sizes implemented // // Create a media object for each image size
$image_sizes = wp_get_attachment_metadata($attachment_id)['sizes']; // // Get images sizes for this attachment, as not all image sizes implemented
// $image_sizes = wp_get_attachment_metadata($attachment_id)['sizes'];
foreach ($image_sizes as $size => $meta) { //
if (!$this->is_published($meta)) { // foreach ($image_sizes as $size => $meta) {
$all_media[] = new LBRY_Speech_Media($attachment_id, array('image_size' => $size)); // if (!$this->is_published($meta)) {
} // $all_media[] = new LBRY_Speech_Media($attachment_id, array('image_size' => $size));
} // }
} // }
} // }
} // }
error_log(print_r($content, true)); // error_log(print_r($content, true));
return $content; return $content;
} }
@ -109,8 +111,6 @@ class LBRY_Speech_Parser
return false; return false;
} }
private function microtime_float() private function microtime_float()
{ {
list($usec, $sec) = explode(" ", microtime()); list($usec, $sec) = explode(" ", microtime());

0
logs/.gitkeep Normal file
View file