Completed
Branch master (d17104)
by Christian
21:20
created

InMemoryLogWriter   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 45
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A lockWriter() 0 11 1
A writeLog() 0 16 4
1
<?php
2
declare(strict_types = 1);
3
4
namespace TYPO3\CMS\Adminpanel\Log;
5
6
/*
7
 * This file is part of the TYPO3 CMS project.
8
 *
9
 * It is free software; you can redistribute it and/or modify it under
10
 * the terms of the GNU General Public License, either version 2
11
 * of the License, or any later version.
12
 *
13
 * For the full copyright and license information, please read the
14
 * LICENSE.txt file that was distributed with this source code.
15
 *
16
 * The TYPO3 project - inspiring people to share!
17
 */
18
19
use TYPO3\CMS\Adminpanel\Utility\MemoryUtility;
20
use TYPO3\CMS\Core\Log\LogLevel;
21
use TYPO3\CMS\Core\Log\LogRecord;
22
use TYPO3\CMS\Core\Log\Writer\AbstractWriter;
23
use TYPO3\CMS\Core\Utility\GeneralUtility;
24
25
/**
26
 * Log writer that writes the log records into a static public class variable
27
 * for InMemory processing
28
 */
29
class InMemoryLogWriter extends AbstractWriter
30
{
31
    public static $log = [];
32
33
    private static $memoryLock = false;
34
35
    /**
36
     * Writes the log record
37
     *
38
     * @param LogRecord $record Log record
39
     * @return self
40
     * @throws \RuntimeException
41
     */
42
    public function writeLog(LogRecord $record): self
43
    {
44
        // Guard: Locked Writer
45
        if (self::$memoryLock === true) {
46
            return $this;
47
        }
48
49
        // Guard: Memory Usage
50
        if (!self::$memoryLock && MemoryUtility::isMemoryConsumptionTooHigh()) {
51
            $this->lockWriter();
52
            return $this;
53
        }
54
55
        self::$log[] = $record;
56
57
        return $this;
58
    }
59
60
    /**
61
     * Lock writer and add a info message that there may potentially be more entries.
62
     */
63
    protected function lockWriter(): void
64
    {
65
        self::$memoryLock = true;
66
        /** @var LogRecord $record */
67
        $record = GeneralUtility::makeInstance(
68
            LogRecord::class,
69
            'TYPO3.CMS.AdminPanel.Log.InMemoryLogWriter',
70
            LogLevel::INFO,
71
            '... Further log entries omitted, memory usage too high.'
72
        );
73
        self::$log[] = $record;
74
    }
75
}
76