Logger.php 3.0 KB

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