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
//            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.Collections.Generic;
using System.IO;
using System.Text;
 
namespace OpenTap.Diagnostic
{
    /// <summary>
    /// A simple log listener that writes events to a binary file.
    /// </summary>
    public class LogFile : ILogListener, IDisposable
    {
        private readonly StreamWriter _sw;
 
        /// <summary>
        /// Initializes a new instance of the <see cref="LogFile"/> class.
        /// Logs events to a given stream.
        /// </summary>
        /// <param name="st">The stream to use.</param>
        public LogFile(Stream st)
        {
            if (st == null)
                throw new ArgumentNullException("st");
            _sw = new StreamWriter(st, Encoding.UTF8, 1024 * 1024);
        }
 
        /// <summary>
        /// Implements the ILogListener method.
        /// </summary>
        /// <param name="logEvents">Events array.</param>
        public void EventsLogged(IEnumerable<Event> logEvents)
        {
            if (logEvents == null)
                throw new ArgumentNullException("logEvent");
            foreach (var evt in logEvents)
                WriteLog(evt);
        }
 
        /// <summary>
        /// This method must be called to close the stream.
        /// </summary>
        public void Close()
        {
            _sw.Flush();
            _sw.Close();
        }
 
        internal void WriteLog(Event evt)
        {
            _sw.WriteLine("{3} ; {0,-13} ; {1,-11} ; {2}", evt.Source, evt.EventType, evt.Message.Replace("\n", " ").Replace("\r", string.Empty), evt.Timestamp);
        }
 
        /// <summary>
        /// Flush the current stream.
        /// </summary>
        public void Flush()
        {
            _sw.Flush();
        }
 
        /// <summary>
        /// Dispose method since BufferedStream implements IDisposable.
        /// </summary>
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
 
        /// <summary>
        /// Dispose method since BufferedStream implements IDisposable.
        /// </summary>
        /// <param name="cleanupScope">If false, cleanup native resources, if true, clean up native and managed.</param>
        protected virtual void Dispose(bool cleanupScope)
        {
            if (_sw != null)
            {
                _sw.Dispose();
            }
        }
    }
 
    /// <summary>
    /// A simple log listener that writes events to a  binary file.
    /// </summary>
    public class BinaryLog : ILogListener, IDisposable
    {
        private Stream str;
 
        /// <summary>
        /// Initializes a new instance of the <see cref="BinaryLog"/> class.
        /// Logs events to a given stream.
        /// </summary>
        /// <param name="st">The stream to use.</param>
        public BinaryLog(Stream st)
        {
            if (st == null)
                throw new ArgumentNullException("st");
            str = new BufferedStream(st, 1024 * 1024);
        }
 
        /// <summary>
        /// Method that must be called to properly close the stream.  
        /// </summary>
        public void Close()
        {
            str.Flush();
            str.Close();
        }
 
        /// <summary>
        /// Flushes the messages from the listener.  
        /// </summary>
        public void Flush()
        {
            str.Flush();
        }
 
        /// <summary>
        /// Logs the events.  
        /// </summary>
        /// <param name="logEvents">List of events to log.</param>
        public void EventsLogged(IEnumerable<Event> logEvents)
        {
            if (logEvents == null)
                throw new ArgumentNullException("logEvents");
            foreach (var evt in logEvents)
            {
                byte[] source = Encoding.UTF8.GetBytes(evt.Source);
                byte[] message = Encoding.UTF8.GetBytes(evt.Message);
 
                Int32 len =
                    4 +
                    8 +
                    8 +
                    4 + message.Length +
                    4 + source.Length;
 
                str.Write(BitConverter.GetBytes(len), 0, 4);
 
                str.Write(BitConverter.GetBytes((Int32)evt.EventType), 0, 4);
                str.Write(BitConverter.GetBytes(evt.Timestamp), 0, 8);
                str.Write(BitConverter.GetBytes(evt.DurationNS), 0, 8);
 
                str.Write(BitConverter.GetBytes((Int32)message.Length), 0, 4);
                str.Write(message, 0, message.Length);
 
                str.Write(BitConverter.GetBytes((Int32)source.Length), 0, 4);
                str.Write(source, 0, source.Length);
            }
        }
 
        /// <summary>
        /// Resets and releases resources for this unmanaged class
        /// </summary>
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
 
        /// <summary>
        /// Dispose method since BufferedStream implements IDisposable.
        /// </summary>
        /// <param name="cleanupScope">If false, cleanup native resources, if true, clean up native and managed.</param>
        protected virtual void Dispose(bool cleanupScope)
        {
            if (str != null)
            {
                str.Dispose();
            }
        }
    }
}