From 41fda53d58f194b1cd85a286ed0402369f4b29f4 Mon Sep 17 00:00:00 2001 From: Joshua Ramon Enslin Date: Sat, 8 Aug 2020 22:04:34 +0200 Subject: [PATCH] Add MDOutputHandler --- exceptions/MDConsoleInvalidHeadingNumber.php | 2 +- src/MDConsole.php | 2 + src/MDOutputHandler.php | 176 +++++++++++++++++++ 3 files changed, 179 insertions(+), 1 deletion(-) create mode 100644 src/MDOutputHandler.php diff --git a/exceptions/MDConsoleInvalidHeadingNumber.php b/exceptions/MDConsoleInvalidHeadingNumber.php index 62b9019..d527c3e 100644 --- a/exceptions/MDConsoleInvalidHeadingNumber.php +++ b/exceptions/MDConsoleInvalidHeadingNumber.php @@ -4,7 +4,7 @@ declare(strict_types = 1); /** * Custom exception class for invalid page parameters. */ -class MDConsoleInvalidHeadingNumber { +class MDConsoleInvalidHeadingNumber extends Exception { /** * Error message. diff --git a/src/MDConsole.php b/src/MDConsole.php index 4afa8e6..7c962a5 100644 --- a/src/MDConsole.php +++ b/src/MDConsole.php @@ -14,12 +14,14 @@ class MDConsole { const CONSOLE_STATUS_UPDATE = 1; const CONSOLE_STATUS_DELETION = 2; const CONSOLE_STATUS_ERROR = 3; + const CONSOLE_STATUS_SKIPPED = 3; const CONSOLE_LEVEL_COLORS = [ self::CONSOLE_STATUS_NOTICE => "", self::CONSOLE_STATUS_ERROR => "41", // 0: Red background | Problem self::CONSOLE_STATUS_UPDATE => "42", // 1: Green background | Edited automatically self::CONSOLE_STATUS_DELETION => "44", // 2: Blue background | Deleted + self::CONSOLE_STATUS_SKIPPED => "40", // 2: Blue background | Deleted ]; const CONSOLE_LOG_USED_RESOURCES_ENABLED = 1; diff --git a/src/MDOutputHandler.php b/src/MDOutputHandler.php new file mode 100644 index 0000000..d5f5279 --- /dev/null +++ b/src/MDOutputHandler.php @@ -0,0 +1,176 @@ + + */ + +/** + * Class for handling outputs and logs for museum-digital's background tools. + */ +class MDOutputHandler { + + /* + * Variables + */ + + /** @var integer */ + private $_verbosity; + /** @var string */ + private $_context; + /** @var string */ + private $_logfile; + /** @var integer */ + private $_counter; + /** @var boolean */ + private $_file_logging; + /** @var integer */ + private $_startTime; + + /** + * Function human_filesize translates byte-level filesizes to human readable ones. + * Thanks to Jeffrey Sambells http://jeffreysambells.com/2012/10/25/human-readable-filesize-php + * + * @param string $bytes A file size, e.g. returned from filesize(). + * @param integer $decimals Number of decimal digits to allow. + * + * @return string + */ + final private function _human_filesize(string $bytes, int $decimals = 2):string { + + $size = ['B','kB','MB','GB','TB','PB','EB','ZB','YB']; + $factor = floor((strlen($bytes) - 1) / 3); + return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . $size[$factor]; + + } + + /** + * Setter function for the verbosity option. + * + * @param integer $input New value of boolean value verbosity. + * + * @return void + */ + public function setVerbosity(int $input = 0) { + + $this->_verbosity = $input; + + } + + /** + * Setter function for the additional context description. + * + * @param string $input New value. + * + * @return void + */ + public function setContext(string $input = "") { + + $this->_context = $input; + + } + + /** + * Setter function for file logging. + * + * @param boolean $input New value. + * + * @return void + */ + public function setFileLogging(bool $input = false) { + + $this->_file_logging = $input; + + if ($this->_file_logging === true) { + if (!is_dir(__DIR__ . "/../logs")) mkdir(__DIR__ . "/../logs", 0755); + touch($this->_logfile); + } + + } + + /** + * Main function of this class. + * Formats a message uniformly and writes it to the log, maybe also to stdout. + * + * @param string $msg Message to log. + * @param integer $statusCode Status code. + * 0: Note + * 1: Skipped + * 2: Update + * 3: Error. + * + * @return void + */ + public function toLog(string $msg, int $statusCode = 0) { + + // Get parts for generating the message to log. + $this->_counter++; // Increment counter + $date = date("Y-m-d H:i:s"); // Get current date & time. + + switch ($statusCode) { + case 1: + $statusSymbol = "-"; + break; + case 2: + $statusSymbol = "U"; + break; + case 3: + $statusSymbol = "E"; + break; + case 0: + default: + $statusSymbol = "N"; + break; + } + + if (!empty($this->_context)) $contextStr = "{$this->_context} │ "; + else $contextStr = ""; + + // Generate the message to log. + $message = "$statusSymbol │ {$contextStr}#" . sprintf("%1$07s", $this->_counter) . " │ " . sprintf("%1$7s", $this->_human_filesize(memory_get_usage())) . " │ {$date} │ {$msg}" . PHP_EOL; + + if ($this->_file_logging === true) { + file_put_contents($this->_logfile, $message, FILE_APPEND); + } + + if (($this->_verbosity > 0 && $statusCode > 1) || $this->_verbosity === 2) echo $message; + + if ($this->_counter % 250 === 0) { + echo '―――> ' . sprintf("%1$07d", $this->_counter) . " operations processed" . PHP_EOL; + echo str_replace("_", "―", sprintf("%'_80s", "_")) . PHP_EOL; + } + + } + + /** + * Constructor function that sets defaults and ensures the logfile exists. + */ + function __construct() { + + $this->_startTime = microtime(true); + $this->_counter = 0; + $this->setVerbosity(); + $this->setContext(); + $this->setFileLogging(); + $this->_logfile = __DIR__ . "/../logs/" . date("Ymd_His") . ".txt"; + + // Ensure the log directory exists + + $this->toLog("Activated logger."); + + } + + /** + * Destructor. + * + * @return void + */ + function __destruct() { + $endtime = microtime(true); + $this->toLog("Finished operation. Processing duration: " . strval($endtime - $this->_startTime)); + + } + +} + +