Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401]

   1  <?php
   2  
   3  namespace PhpOffice\PhpSpreadsheet\Calculation\Engine;
   4  
   5  class Logger
   6  {
   7      /**
   8       * Flag to determine whether a debug log should be generated by the calculation engine
   9       *        If true, then a debug log will be generated
  10       *        If false, then a debug log will not be generated.
  11       *
  12       * @var bool
  13       */
  14      private $writeDebugLog = false;
  15  
  16      /**
  17       * Flag to determine whether a debug log should be echoed by the calculation engine
  18       *        If true, then a debug log will be echoed
  19       *        If false, then a debug log will not be echoed
  20       * A debug log can only be echoed if it is generated.
  21       *
  22       * @var bool
  23       */
  24      private $echoDebugLog = false;
  25  
  26      /**
  27       * The debug log generated by the calculation engine.
  28       *
  29       * @var string[]
  30       */
  31      private $debugLog = [];
  32  
  33      /**
  34       * The calculation engine cell reference stack.
  35       *
  36       * @var CyclicReferenceStack
  37       */
  38      private $cellStack;
  39  
  40      /**
  41       * Instantiate a Calculation engine logger.
  42       */
  43      public function __construct(CyclicReferenceStack $stack)
  44      {
  45          $this->cellStack = $stack;
  46      }
  47  
  48      /**
  49       * Enable/Disable Calculation engine logging.
  50       *
  51       * @param bool $writeDebugLog
  52       */
  53      public function setWriteDebugLog($writeDebugLog): void
  54      {
  55          $this->writeDebugLog = $writeDebugLog;
  56      }
  57  
  58      /**
  59       * Return whether calculation engine logging is enabled or disabled.
  60       *
  61       * @return bool
  62       */
  63      public function getWriteDebugLog()
  64      {
  65          return $this->writeDebugLog;
  66      }
  67  
  68      /**
  69       * Enable/Disable echoing of debug log information.
  70       *
  71       * @param bool $echoDebugLog
  72       */
  73      public function setEchoDebugLog($echoDebugLog): void
  74      {
  75          $this->echoDebugLog = $echoDebugLog;
  76      }
  77  
  78      /**
  79       * Return whether echoing of debug log information is enabled or disabled.
  80       *
  81       * @return bool
  82       */
  83      public function getEchoDebugLog()
  84      {
  85          return $this->echoDebugLog;
  86      }
  87  
  88      /**
  89       * Write an entry to the calculation engine debug log.
  90       *
  91       * @param mixed $args
  92       */
  93      public function writeDebugLog(string $message, ...$args): void
  94      {
  95          //    Only write the debug log if logging is enabled
  96          if ($this->writeDebugLog) {
  97              $message = sprintf($message, ...$args);
  98              $cellReference = implode(' -> ', $this->cellStack->showStack());
  99              if ($this->echoDebugLog) {
 100                  echo $cellReference,
 101                  ($this->cellStack->count() > 0 ? ' => ' : ''),
 102                  $message,
 103                  PHP_EOL;
 104              }
 105              $this->debugLog[] = $cellReference .
 106                  ($this->cellStack->count() > 0 ? ' => ' : '') .
 107                  $message;
 108          }
 109      }
 110  
 111      /**
 112       * Write a series of entries to the calculation engine debug log.
 113       *
 114       * @param string[] $args
 115       */
 116      public function mergeDebugLog(array $args): void
 117      {
 118          if ($this->writeDebugLog) {
 119              foreach ($args as $entry) {
 120                  $this->writeDebugLog($entry);
 121              }
 122          }
 123      }
 124  
 125      /**
 126       * Clear the calculation engine debug log.
 127       */
 128      public function clearLog(): void
 129      {
 130          $this->debugLog = [];
 131      }
 132  
 133      /**
 134       * Return the calculation engine debug log.
 135       *
 136       * @return string[]
 137       */
 138      public function getLog()
 139      {
 140          return $this->debugLog;
 141      }
 142  }