From 75d2409a7ccd394fbe9e726abd1bafacc22c668d Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Date: Tue, 1 Jul 2014 20:50:51 +0200 Subject: [PATCH] Added ThreadedLoggerAttachments --- src/pocketmine/PocketMine.php | 1 + src/pocketmine/utils/MainLogger.php | 5 ++ src/spl/ThreadedLogger.php | 46 +++++++++++++++ src/spl/ThreadedLoggerAttachment.php | 85 ++++++++++++++++++++++++++++ 4 files changed, 137 insertions(+) create mode 100644 src/spl/ThreadedLoggerAttachment.php diff --git a/src/pocketmine/PocketMine.php b/src/pocketmine/PocketMine.php index 888d55374..212f4e72d 100644 --- a/src/pocketmine/PocketMine.php +++ b/src/pocketmine/PocketMine.php @@ -95,6 +95,7 @@ namespace pocketmine { } if(!class_exists("ThreadedLogger", false)){ + require_once(\pocketmine\PATH . "src/spl/ThreadedLoggerAttachment.php"); require_once(\pocketmine\PATH . "src/spl/ThreadedLogger.php"); } diff --git a/src/pocketmine/utils/MainLogger.php b/src/pocketmine/utils/MainLogger.php index dfa4fb087..cd6bb2eed 100644 --- a/src/pocketmine/utils/MainLogger.php +++ b/src/pocketmine/utils/MainLogger.php @@ -145,6 +145,11 @@ class MainLogger extends \ThreadedLogger{ }else{ echo $message; } + + if($this->attachment instanceof \ThreadedLoggerAttachment){ + $this->attachment->call($message); + } + $this->logStream .= date("Y-m-d", $now) . " " . $cleanMessage; } diff --git a/src/spl/ThreadedLogger.php b/src/spl/ThreadedLogger.php index 62c3b6cb0..8b345980e 100644 --- a/src/spl/ThreadedLogger.php +++ b/src/spl/ThreadedLogger.php @@ -21,4 +21,50 @@ abstract class ThreadedLogger extends \Thread implements Logger{ + /** @var \ThreadedLoggerAttachment */ + protected $attachment = null; + + /** + * @param ThreadedLoggerAttachment $attachment + */ + public function addAttachment(\ThreadedLoggerAttachment $attachment){ + if($this->attachment instanceof \ThreadedLoggerAttachment){ + $this->attachment->addAttachment($attachment); + }else{ + $this->attachment = $attachment; + } + } + + /** + * @param ThreadedLoggerAttachment $attachment + */ + public function removeAttachment(\ThreadedLoggerAttachment $attachment){ + if($this->attachment instanceof \ThreadedLoggerAttachment){ + if($this->attachment === $attachment){ + $this->attachment = null; + foreach($attachment->getAttachments() as $attachment){ + $this->addAttachment($attachment); + } + } + } + } + + public function removeAttachments(){ + if($this->attachment instanceof \ThreadedLoggerAttachment){ + $this->attachment->removeAttachments(); + $this->attachment = null; + } + } + + /** + * @return \ThreadedLoggerAttachment[] + */ + public function getAttachments(){ + $attachments = []; + if($this->attachment instanceof \ThreadedLoggerAttachment){ + $attachments[] = $this->attachment; + $attachments += $this->attachment->getAttachments(); + } + return $attachments; + } } \ No newline at end of file diff --git a/src/spl/ThreadedLoggerAttachment.php b/src/spl/ThreadedLoggerAttachment.php new file mode 100644 index 000000000..a5f21f713 --- /dev/null +++ b/src/spl/ThreadedLoggerAttachment.php @@ -0,0 +1,85 @@ +log($message); + if($this->attachment instanceof \ThreadedLoggerAttachment){ + $this->attachment->call($message); + } + } + + /** + * @param ThreadedLoggerAttachment $attachment + */ + public function addAttachment(\ThreadedLoggerAttachment $attachment){ + if($this->attachment instanceof \ThreadedLoggerAttachment){ + $this->attachment->addAttachment($attachment); + }else{ + $this->attachment = $attachment; + } + } + + /** + * @param ThreadedLoggerAttachment $attachment + */ + public function removeAttachment(\ThreadedLoggerAttachment $attachment){ + if($this->attachment instanceof \ThreadedLoggerAttachment){ + if($this->attachment === $attachment){ + $this->attachment = null; + foreach($attachment->getAttachments() as $attachment){ + $this->addAttachment($attachment); + } + } + } + } + + public function removeAttachments(){ + if($this->attachment instanceof \ThreadedLoggerAttachment){ + $this->attachment->removeAttachments(); + $this->attachment = null; + } + } + + /** + * @return \ThreadedLoggerAttachment[] + */ + public function getAttachments(){ + $attachments = []; + if($this->attachment instanceof \ThreadedLoggerAttachment){ + $attachments[] = $this->attachment; + $attachments += $this->attachment->getAttachments(); + } + return $attachments; + } +} \ No newline at end of file