Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

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