chr
2026-04-05 fe750b791d5b517cc4e9bc8e99a9a75139a0cfba
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//            Copyright Keysight Technologies 2012-2019
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, you can obtain one at http://mozilla.org/MPL/2.0/.
using System;
using System.IO;
using OpenTap.Diagnostic;
using System.Threading;
using System.Collections.Generic;
using System.Text;
 
namespace OpenTap
{
    /// <summary>
    /// TraceListener to be used in the App.Config file of the executable to write trace/log data to
    /// a file.
    /// </summary>
    sealed class FileTraceListener : TextWriterTraceListener
    {
        string _fileName;
 
        /// <summary>
        /// If the log should be written with absolute or relative time.
        /// </summary>
        public bool IsRelative { get; set; }
        
        /// <summary> The current file name of the trace file. </summary>
        public string FileName => _fileName;
        /// <summary>
        /// Initializes a new instance of the FileTraceListener class.
        /// </summary>
        /// <param name="fileName">Name of the file to write to.</param>
        public FileTraceListener(string fileName)
            : base(fileName)
        {
            _fileName = Path.GetFullPath(fileName);
 
        }
 
        public event EventHandler FileSizeLimitReached;
 
        /// <summary> Installs a file limit. When the limit is reached FileSIzeLimitReached is invoked. </summary>
        public ulong FileSizeLimit = ulong.MaxValue;
 
        /// <summary>
        ///  Initializes a new instance of the <see cref="OpenTap.FileTraceListener"/>
        ///  class, using the stream as the recipient of the debugging and tracing output.
        /// </summary>
        /// <param name="stream">A System.IO.Stream that represents the stream the System.Diagnostics.TextWriterTraceListener writes to.</param>
        public FileTraceListener(Stream stream) : base(stream)
        {
        }
 
        /// <summary>
        /// Changes the target file name of the file trace listener. Depending on arguments it might leave the old file as it is
        /// or move the content to the new file.
        /// </summary>
        /// <param name="fileName">The file name of the target file.</param>
        /// <param name="noExclusiveWriteLock">Allow multiple processes to modify the file.</param>
        /// <param name="startNewLog">Whether to move the content or to start a new log file.</param>
        internal void ChangeFileName(string fileName, bool noExclusiveWriteLock, bool startNewLog = false)
        {
            string dir = Path.GetDirectoryName(fileName);
            if (string.IsNullOrWhiteSpace(dir) == false)
                Directory.CreateDirectory(Path.GetDirectoryName(fileName));
 
            StreamWriter newwriter = null;
            if (noExclusiveWriteLock)
            {
                // Initialize a stream where the underlying file can be deleted. If the file is deleted, writes just go into the void.
                var stream = new FileStream(fileName, FileMode.Append, FileAccess.Write, FileShare.Read | FileShare.Delete);
                newwriter = new StreamWriter(stream, Encoding.UTF8)
                {
                    AutoFlush = true
                };
            }
            else
            {
                newwriter = new StreamWriter(fileName, true, System.Text.Encoding.UTF8)
                {
                    AutoFlush = true
                };
            }
 
            var OldWriter = base.Writer;
            base.Writer = newwriter;
 
            OldWriter.Close();
 
            if (!startNewLog && _fileName != null)
            {
                Writer.Write(File.ReadAllText(_fileName));
                File.Delete(_fileName);
            }
 
            _fileName = Path.GetFullPath(fileName);
        }
 
        long first_timestamp = -1;
        static readonly ThreadLocal<System.Text.StringBuilder> _sb = new ThreadLocal<System.Text.StringBuilder>(() => new System.Text.StringBuilder());
        readonly object fileTraceLock = new object();
        public override void TraceEvents(IEnumerable<Event> events)
        {
            // Note, this function is heavily optimized.
            // profile carefully after doing any changes!!
 
            if (events == null)
                throw new ArgumentNullException(nameof(events));
 
            System.Text.StringBuilder sb = _sb.Value;
 
            sb.Clear();
            long lastTick = 0;
            string tickmsg = "";
            foreach (var evt in events)
            {
                if (first_timestamp == -1)
                    first_timestamp = evt.Timestamp;
 
                if (lastTick != evt.Timestamp)
                { // lastTick is to ms resolution
                    // If its the same, dont waste time generating a new string.
                    if (IsRelative)
                    {
                        var elapsed = TimeSpan.FromTicks(evt.Timestamp - first_timestamp);
                        tickmsg = elapsed.ToString("hh\\:mm\\:ss\\.ffffff");
                    }
                    else
                    {
                        var time = new DateTime(evt.Timestamp);
                        tickmsg = time.ToString("yyyy-MM-dd HH:mm:ss.ffffff");
                    }
                    lastTick = evt.Timestamp;
                }
 
                sb.Append(tickmsg);
                sb.Append(" ; ");
                sb.Append(evt.Source);
                int paddingcount = 14 - evt.Source.Length;
                if (paddingcount > 0)
                    sb.Append(' ', paddingcount);
 
                sb.Append(" ; ");
                switch ((LogEventType)evt.EventType)
                { // All these should have same padding, but dont calculate runtime.
                    case LogEventType.Debug:
                        sb.Append("Debug      ");
                        break;
                    case LogEventType.Information:
                        sb.Append("Information");
                        break;
                    case LogEventType.Warning:
                        sb.Append("Warning    ");
                        break;
                    case LogEventType.Error:
                        sb.Append("Error      ");
                        break;
                }
 
                sb.Append(" ; ");
                if (evt.Message != null)
                {
                    sb.Append(evt.Message.Replace("\n", "").Replace("\r", ""));
                }
                sb.AppendLine();
            }
            var logStr = sb.ToString();
            lock (fileTraceLock)
            {
                this.Write(logStr);
 
                if (Writer is StreamWriter streamWriter && (ulong)streamWriter.BaseStream.Length > FileSizeLimit)
                {
                    if (FileSizeLimitReached != null)
                        FileSizeLimitReached(this, new EventArgs());
                }
            }
        }
    }
}