From 14c8389049fde423906078b6602215a703723ccf Mon Sep 17 00:00:00 2001
From: "Hahn Axel (hahn)" <axel.hahn@unibe.ch>
Date: Mon, 15 May 2023 12:07:11 +0200
Subject: [PATCH] update ahlogger

---
 .../deployment/classes/logger.class.php       | 66 +++++++++++++++++--
 1 file changed, 59 insertions(+), 7 deletions(-)

diff --git a/public_html/deployment/classes/logger.class.php b/public_html/deployment/classes/logger.class.php
index b42a41c5..a25fcc95 100644
--- a/public_html/deployment/classes/logger.class.php
+++ b/public_html/deployment/classes/logger.class.php
@@ -1,24 +1,60 @@
 <?php
 
 /**
+ * ----------------------------------------------------------------------
+ * 
  * Debug logging during a client request.
  * So you can measure any action find bottlenecks in your code.
  * 
- * Source: https://github.com/axelhahn/ahlogger
+ * Licence: GNU GPL 3.0
+ * Source:  https://github.com/axelhahn/ahlogger
+ * Docs:    https://www.axel-hahn.de/docs/ahlogger/
  * 
- * USAGE:
- * (1) Trigger a message with add() to add a marker
+ * USAGE:<br>
+ * (1) Trigger a message with add() to add a marker<br>
  * (2) The render() method lists all items in a table with time since start
- *     and the delta to the last message. 
+ *     and the delta to the last message. <br>
  * 
  * @author www.axel-hahn.de
+ * 
+ * ----------------------------------------------------------------------
+ * 2016-02-26  init
+ * 2016-11-19  add memory usage
+ * (...)
+ * 2022-09-25  add memory tracking, add cli renderer 
+ * 2022-09-27  css updates
+ * 2022-10-02  add emoji chars 
+ * 2022-10-16  mark longest action with an icon 
+ * 2022-12-15  make it compatible to PHP 8.2; add doc + comments
+ * 2023-05-15  fix _getBar() - division by zero
+ * ----------------------------------------------------------------------
  */
 class logger {
 
+    /**
+     * @var {array} array of added messages
+     */
     protected $aMessages = [];
+    
+    /**
+     * @var {bool} flag: show debug infos? default: false
+     */
     protected $bShowDebug = false;
+
+    /**
+     * @var {int} memory usage on start
+     */
     protected $_iMemStart = false;
 
+    /**
+     * @var {string} dynamic prefix for used css - it is set in the cronstructor
+     */
+    protected $sCssPrefix = '';
+
+    // ----------------------------------------------------------------------
+    // CONSTRUCTOR
+    // ----------------------------------------------------------------------
+
     /**
      * constuctor
      * @param  string $sInitMessage  init message
@@ -32,6 +68,10 @@ class logger {
         return true;
     }
 
+    // ----------------------------------------------------------------------
+    // PUBLIC METHODS
+    // ----------------------------------------------------------------------
+
     /**
      * add a logging message
      * @param type $sMessage
@@ -76,6 +116,16 @@ class logger {
         }
     }
 
+    /**
+     * helper function: prepare array of added massages before output
+     * - detect warnings and errors
+     * - detect needed time for each action
+     * - detect longest action
+     * - detect maximum of memory usage
+     * - calculate total time
+     * 
+     * @return array
+     */
     protected function _prepareRendering(){
         $iMem=memory_get_usage();
         $this->add('<hr>');
@@ -158,8 +208,10 @@ class logger {
      * @return {string}
      */
     protected function _getBar($iVal, $iMax){
-        $iWidth=$iVal/$iMax*100;
-        return '<div class="bar"><div class="progress" style="width: '.$iWidth.'%;">&nbsp;</div></div>';
+        return $iMax>0
+            ? '<div class="bar"><div class="progress" style="width: '.($iVal/$iMax*100).'%;">&nbsp;</div></div>'
+            : ''
+            ;
     }
 
     /**
@@ -208,7 +260,7 @@ class logger {
                     '<td>' . $aLogentry["counter"] . '</td>' .
                     '<td>' . $aLogentry["level"] . '</td>' .
                     '<td>' . $aLogentry["timer"] . '</td>' .
-                    '<td>' . $this->_getBar($aLogentry["delta"], $aData["maxtime"]*1000). $aLogentry["delta"] .' ms</td>' .
+                    '<td>' . $this->_getBar($aLogentry["delta"], $aData["maxtime"]*1000). $aLogentry["delta"] .' ms'.($aLogentry["delta"]==$aData['maxtime']*1000 ? ' ⏱️' : '').'</td>' .
                     '<td>' . $this->_getBar($aLogentry["memory"], $aData["maxmem"]) . $aLogentry["memory"] .' MB'. '</td>' .
                     '<td>' . $aLogentry["message"] . '</td>' .
                     '</tr>';
-- 
GitLab