File: File.php

package info (click to toggle)
simplesamlphp 1.19.7-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 42,920 kB
  • sloc: php: 202,044; javascript: 14,867; xml: 2,700; sh: 225; perl: 82; makefile: 70; python: 5
file content (106 lines) | stat: -rw-r--r-- 2,532 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php

declare(strict_types=1);

namespace SimpleSAML\Module\core\Stats\Output;

use SimpleSAML\Configuration;
use SimpleSAML\Error;

/**
 * Statistics logger that writes to a set of log files
 *
 * @package SimpleSAMLphp
 */
class File extends \SimpleSAML\Stats\Output
{
    /**
     * The log directory.
     * @var string
     */
    private $logDir;

    /**
     * The file handle for the current file.
     * @var resource|null
     */
    private $file = null;

    /**
     * The current file date.
     * @var string|null
     */
    private $fileDate = null;


    /**
     * Initialize the output.
     *
     * @param \SimpleSAML\Configuration $config  The configuration for this output.
     */
    public function __construct(Configuration $config)
    {
        $logDir = $config->getPathValue('directory');
        if ($logDir === null) {
            throw new \Exception('Missing "directory" option for core:File');
        }
        if (!is_dir($logDir)) {
            throw new \Exception('Could not find log directory: ' . var_export($logDir, true));
        }
        $this->logDir = $logDir;
    }


    /**
     * Open a log file.
     *
     * @param string $date  The date for the log file.
     * @return void
     */
    private function openLog(string $date): void
    {
        if ($this->file !== null && $this->file !== false) {
            fclose($this->file);
            $this->file = null;
        }

        $fileName = $this->logDir . '/' . $date . '.log';
        $fh = @fopen($fileName, 'a');
        if ($fh === false) {
            throw new Error\Exception('Error opening log file: ' . var_export($fileName, true));
        }

        // Disable output buffering
        stream_set_write_buffer($fh, 0);

        $this->file = $fh;
        $this->fileDate = $date;
    }


    /**
     * Write a stats event.
     *
     * @param array $data  The event.
     * @return void
     */
    public function emit(array $data)
    {
        assert(isset($data['time']));

        $time = $data['time'];
        $milliseconds = intval((($time - intval($time)) * 1000));

        $timestamp = gmdate('Y-m-d\TH:i:s', intval($time)) . sprintf('.%03dZ', $milliseconds);

        $outDate = substr($timestamp, 0, 10); // The date-part of the timstamp

        if ($outDate !== $this->fileDate) {
            $this->openLog($outDate);
        }

        $line = $timestamp . ' ' . json_encode($data) . "\n";
        /** @psalm-suppress PossiblyNullArgument */
        fwrite($this->file, $line);
    }
}