aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/embeddable-dll-service/csharp/Ringlogger.cs
blob: 711c22e75d55937eb46d3ed261e1b4a83a77ad90 (plain) (blame)
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
/* SPDX-License-Identifier: MIT
 *
 * Copyright (C) 2019 WireGuard LLC. All Rights Reserved.
 */

using System;
using System.IO;
using System.IO.MemoryMappedFiles;
using System.Text;

namespace Tunnel
{
    public class Ringlogger
    {
        private readonly MemoryMappedViewAccessor _viewAccessor;

        public Ringlogger(string filename)
        {
            var file = File.Open(filename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete);
            var mmap = MemoryMappedFile.CreateFromFile(file, null, 0, MemoryMappedFileAccess.Read, HandleInheritability.None, false);
            _viewAccessor = mmap.CreateViewAccessor(0, 8 + 2048 * (512 + 8), MemoryMappedFileAccess.Read);
            if (_viewAccessor.ReadUInt32(0) != 0xbadbabe)
                throw new InvalidDataException("The provided file is missing the magic number.");
        }

        public void WriteTo(TextWriter writer)
        {
            var start = _viewAccessor.ReadUInt32(4);
            for (var i = 0; i < 2048; ++i)
            {
                var lineOffset = 8 + (8 + 512) * ((i + start) % 2048);
                var timeNs = _viewAccessor.ReadInt64(lineOffset);
                if (timeNs == 0)
                    continue;
                var textBytes = new byte[512];
                _viewAccessor.ReadArray<byte>(lineOffset + 8, textBytes, 0, textBytes.Length);
                var nullByte = Array.IndexOf<byte>(textBytes, 0);
                if (nullByte <= 0)
                    continue;
                var text = Encoding.UTF8.GetString(textBytes, 0, nullByte);
                var time = DateTimeOffset.FromUnixTimeMilliseconds(timeNs / 1000000).ToString("yyyy'-'MM'-'dd HH':'mm':'ss'.'ffffff");
                writer.WriteLine(String.Format("{0}: {1}", time, text));
            }
        }
    }
}