Source of file DebugTools.php
Size: 5,421 Bytes - Last Modified: 2021-12-23T10:21:36+00:00
/var/www/docs.ssmods.com/process/src/src/Dev/DebugTools.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 | <?php namespace SilverCart\Dev; /** * Provides some debug helper methods. * * @package SilverCart * @subpackage Dev * @author Sebastian Diel <sdiel@pixeltricks.de> * @since 25.09.2017 * @copyright 2017 pixeltricks GmbH * @license see license file in modules root directory */ class DebugTools { /** * Start time of the timer * * @var float */ public static $starttime = null; /** * Counter of print outputs * * @var int */ public static $printCounter = 0; /** * Starts the timer to debug some processing durations * * @return void * * @author Sebastian Diel <sdiel@pixeltricks.de> * @since 22.03.2012 */ public static function startTimer() { self::setStarttime(microtime(true)); } /** * Clears the timer * * @return void * * @author Sebastian Diel <sdiel@pixeltricks.de> * @since 22.03.2012 */ public static function clearTimer() { self::setStarttime(null); } /** * Returns the current time difference. Timer has to be started with * self::startTimer(). * If $print is set to true or not passed, the time difference will be * printed to default output. * * @param bool $print Print time difference to output? * * @return float */ public static function getTimeDifference($print = true) { $timeDifference = microtime(true) - self::getStarttime(); if ($print) { self::printString($timeDifference, false); } return $timeDifference; } /** * Prints the given string to default output. * * @param string $string String to print * @param bool $withCounter Print counter? * @param bool $plain Set this to true to use \n for line break (HTML will be used by default). * * @return void * * @author Sebastian Diel <sdiel@pixeltricks.de> * @since 04.11.2013 */ public static function printString($string, $withCounter = true, $plain = false) { if ($withCounter) { print '#' . ++self::$printCounter . ': '; } print $string; if ($plain) { print PHP_EOL; } else { print "<br/>" . PHP_EOL; } } /** * Exports the given var to default output * * @param mixed $var Var to export * * @return void * * @author Sebastian Diel <sdiel@pixeltricks.de> * @since 04.04.2012 */ public static function varExport($var, bool $withPreTag = false) : void { $string = var_export($var, true); if ($withPreTag) { $string = "<pre>{$string}</pre>"; } self::printString($string); } /** * Prints the current time difference to default output. Timer has to be * started with self::startTimer(). * * @param string $label Label to print with time difference * * @return void * * @author Sebastian Diel <sdiel@pixeltricks.de> * @since 22.03.2012 */ public static function printTimeDifference($label) { self::printString($label); self::getTimeDifference(); self::printString("<hr/>", false); } /** * Prints the current memory usage. * * @return void * * @author Sebastian Diel <sdiel@pixeltricks.de> * @since 04.11.2013 */ public static function printCurrentMemoryUsage() { self::printString('Current Memory Usage: ' . self::getCurrentMemoryUsage() . ' (' . self::getCurrentMemoryUsage(true) . ')', true, true); } /** * Returns the current memory usage. * * @param bool $real_usage [optional] <p>Set this to <b>TRUE</b> to get the * real size of memory allocated from system. If * not set or <b>FALSE</b> only the memory used by * emalloc() is reported.</p> * * @return void * * @author Sebastian Diel <sdiel@pixeltricks.de> * @since 04.11.2013 */ public static function getCurrentMemoryUsage($real_usage = false) { $memory_get_usage = memory_get_usage($real_usage); $memory_get_usage_mb = ($memory_get_usage / 1024) / 1024; return number_format($memory_get_usage_mb, 2, ',', '.'); } /** * Checks whether the timer is already started * * @return bool * * @author Sebastian Diel <sdiel@pixeltricks.de> * @since 28.03.2012 */ public static function timerIsStarted() { $timerIsStarted = false; if (!is_null(self::$starttime)) { $timerIsStarted = true; } return $timerIsStarted; } //////////////////////////////////////////////////////////////////////////// // DEFAULT ACCESSORS //////////////////////////////////////////////////////////////////////////// /** * Sets the start time * * @param float $starttime Start time * * @return void */ public static function setStarttime($starttime) { self::$starttime = $starttime; } /** * Returns the start time * * @return float */ public static function getStarttime() { return self::$starttime; } } |