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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
//            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;
using System.Collections.Generic;
using System.Runtime.InteropServices;
 
namespace OpenTap.Diagnostic
{
    /// <summary>
    /// Log source interface. Instances of this are always created by a corresponding ILogContext.
    /// </summary>
    [ComVisible(false)]
    public interface ILog
    {
        /// <summary>
        /// Logs an event.
        /// </summary>
        /// <param name="EventType">Event type constant. Typically matches System.Diagnostics.TraceEventType.</param>
        /// <param name="Message">Message for the event.</param>
        void LogEvent(int EventType, string Message);
 
        /// <summary>
        /// Logs an event.
        /// </summary>
        /// <param name="EventType">Event type constant. Typically matches System.Diagnostics.TraceEventType.</param>
        /// <param name="Message">Message for the event. Formatted with arguments in Args.</param>
        /// <param name="Args">Arguments for String.Format() call.</param>
        void LogEvent(int EventType, string Message, params object[] Args);
 
        /// <summary>
        /// Logs an event.
        /// </summary>
        /// <param name="EventType">Event type constant. Typically matches System.Diagnostics.TraceEventType.</param>
        /// <param name="DurationNS">Duration in nanoseconds of this event.</param>
        /// <param name="Message">Message for the event.</param>
        void LogEvent(int EventType, long DurationNS, string Message);
 
        /// <summary>
        /// Logs an event.
        /// </summary>
        /// <param name="EventType">Event type constant. Typically matches System.Diagnostics.TraceEventType.</param>
        /// <param name="DurationNS">Duration in nanoseconds of this event.</param>
        /// <param name="Message">Message for the event. Formatted with arguments in Args.</param>
        /// <param name="Args">Arguments for String.Format() call.</param>
        void LogEvent(int EventType, long DurationNS, string Message, params object[] Args);
 
        /// <summary>
        /// Identifier name of this source.
        /// </summary>
        string Source { get; }
    }
 
    /// <summary>
    /// A structure containing all information about an event.
    /// </summary>
    public struct Event
    {
        /// <summary>
        /// Construct new <c ref="Event"/> structure.
        /// </summary>
        /// <param name="duration">The duration of the event in nanoseconds.  </param>
        /// <param name="eventType">The event type this event was logged with. </param>
        /// <param name="message">The message for the event.  </param>
        /// <param name="source">The log source identifier this event was logged from.  </param>
        /// <param name="timestamp">The timestamp for the event in system ticks.  </param>
        public Event(long duration, int eventType, string message, string source, long timestamp)
        {
            DurationNS = duration;
            EventType = eventType;
            Message = message;
            Source = source;
            Timestamp = timestamp;
        }
 
        /// <summary>
        /// The event type this event was logged with. Typically matches System.Diagnostics.TraceEventType.
        /// </summary>
        public int EventType;
        /// <summary>
        /// The log source identifier this event was logged from.
        /// </summary>
        public string Source;
 
        /// <summary>
        /// The Timestamp for the event in Ticks.
        /// </summary>
        public long Timestamp;
        /// <summary>
        /// The duration of the event in nanoseconds.
        /// </summary>
        public long DurationNS;
 
        /// <summary>
        /// The message for the event.
        /// </summary>
        public string Message;
 
        /// <summary> Creates a string representation of this event structure. </summary>
        public override string ToString() => $"{Timestamp} : {Source} : {Message}";
    }
    
    /// <summary>
    /// A collection class that provide posibility to iterate over an array of <see cref="Event">events</see>
    /// </summary>
    internal class EventCollection : IEnumerable<Event>, IDisposable
    {
        #region private fields
        private Event[] events = null;
        #endregion
 
        #region nested types
        private class EventCollectionEnumerator : IEnumerator<Event>
        {
            #region private fields
            private int index = -1;
            private EventCollection eventCollection = null;
            private bool disposed = false;
            #endregion
 
            #region properties
            public Event Current
            {
                get
                {
                    VerifyNotDisposed();
                    try
                    {
                        Event element = eventCollection.events[index];
                        return element;
                    }
                    catch (IndexOutOfRangeException e)
                    {
                        throw new InvalidOperationException(e.Message);
                    }
                }
            }
 
            object IEnumerator.Current
            {
                get
                {
                    VerifyNotDisposed();
                    return Current;
                }
            }
            #endregion
 
            #region ctor
            public EventCollectionEnumerator(EventCollection eventCollection)
            {
                disposed = false;
                this.eventCollection = eventCollection;
            }
            #endregion
            
            public void Dispose()
            {
                VerifyNotDisposed();
                disposed = true;
            }
 
            public bool MoveNext()
            {
                VerifyNotDisposed();
                index++;
                return (index < eventCollection.events.Length);
            }
 
            public void Reset()
            {
                VerifyNotDisposed();
                index = -1;
            }
            private void VerifyNotDisposed()
            {
                if (disposed)
                {
                    throw new ObjectDisposedException("EventCollectionEnumerator");
                }
                else if (eventCollection.Disposed)
                {
                    throw new ObjectDisposedException("EventCollection");
                }
            }
        }
        #endregion
 
        #region properties
 
        /// <summary>
        /// Returns a boolean indicating whether this instance has been disposed or not.
        /// </summary>
        public bool Disposed
        {
            get
            {
                VerifyNotDisposed();
                return events == null;
            }
        }
        
        /// <summary>
        /// Gets the number of the elements in the collection
        /// </summary>
        public int Length
        {
            get
            {
                VerifyNotDisposed();
 
                if (events == null)
                {
                    throw new ObjectDisposedException("EventCollection");
                }
 
                if (events != null)
                    return events.Length;
                else
                    return 0;
            }
        }
        #endregion
 
        #region ctor 
        /// <summary>
        /// Creates a new instance of <see cref="EventCollection"/>.
        /// </summary>
        /// <param name="events">The event array that will be wrapped around by this class.</param>
        public EventCollection(Event[] events)
        {
            this.events = events;
        }
        #endregion
        
        /// <summary>
        /// Dispose this instance.
        /// </summary>
        public void Dispose()
        {
            VerifyNotDisposed();
            events = null;
        }
        
        /// <summary>
        /// An enumerator that can be used to enumerate this collection.
        /// </summary>
        /// <returns>An enumerator that can be used to enumerate this collection.</returns>
        public IEnumerator<Event> GetEnumerator()
        {
            VerifyNotDisposed();
            return new EventCollectionEnumerator(this);
        }
 
        /// <summary>
        /// An enumerator that can be used to enumerate this collection.
        /// </summary>
        /// <returns>An enumerator that can be used to enumerate this collection.</returns>
        IEnumerator IEnumerable.GetEnumerator()
        {
            VerifyNotDisposed();
            return new EventCollectionEnumerator(this);
        }
        private void VerifyNotDisposed()
        {
            if (events == null)
            {
                throw new ObjectDisposedException("EventCollection");
            }
        }
    }
 
    /// <summary>
    /// Interface a log listener must implement.
    /// </summary>
    public interface ILogListener
    {
        /// <summary>
        /// Message called when multiple events have been logged.  
        /// </summary>
        /// <param name="Events">Array containing a number of events.</param>
        void EventsLogged(IEnumerable<Event> Events);
        /// <summary>
        /// Called when the log context requests that this listener must flush all of its output resources.  
        /// </summary>
        void Flush();
    }
    
    /// <summary>
    /// The timestamping mechanism used by ILogContext.
    /// </summary>
    public interface ILogTimestampProvider : ITapPlugin
    {
        /// <summary>
        /// Generates a timestamp for the current instant.
        /// </summary>
        long Timestamp();
        /// <summary>
        /// Converts a timestamp generated by the Timestamp method into Ticks.
        /// </summary>
        /// <param name="timestamp"></param>
        long ConvertToTicks(long timestamp);
    }
 
 
    /// <summary>
    /// A log context that can have multiple log sources and <see cref="LogResultListener"/>. 
    /// </summary>
    [ComVisible(false)]
    public interface ILogContext
    {
        /// <summary>
        /// Creates a log source with a given source identifier.
        /// </summary>
        /// <param name="Source">The source identifier of this log source.</param>
        ILog CreateLog(string Source);
 
        /// <summary>
        /// Removes a log source from the context.
        /// </summary>
        /// <param name="LogSource">The given log source.</param>
        void RemoveLog(ILog LogSource);
 
        /// <summary>
        /// Attaches a log listener.
        /// </summary>
        void AttachListener(ILogListener Listener);
 
        /// <summary>
        /// Detaches a log listener. Automatically flushes the context.
        /// </summary>
        void DetachListener(ILogListener Listener);
 
        /// <summary>
        /// Flush all events received at the time instant this method is called, but only waits a number of milliseconds.
        /// </summary>
        /// <param name="TimeoutMS">Max time to wait for messages. If 0 it will wait infinitely.</param>
        /// <returns>True if it waited successfully, or false if a timeout occurred.</returns>
        bool Flush(int TimeoutMS = 0);
 
        /// <summary>
        /// Flush all events received at the time instant this method is called, but only waits a given duration.
        /// </summary>
        /// <param name="Timeout">Max time to wait for messages, or zero to wait infinitely.</param>
        /// <returns>True if it waited successfully, or false if a timeout occurred.</returns>
        bool Flush(TimeSpan Timeout);
 
        /// <summary>
        /// Timestamp method to use for all subsequent logged events.
        /// </summary>
        ILogTimestampProvider Timestamper { get; set; }
 
        /// <summary>
        /// When true, sets the log context to an asynchronous mode (avoiding the potential synchronous mode problem of log sources returning from <see cref="ILog.LogEvent(int, string)"/> calls before the events have been processed). 
        /// When false, log sources always wait until all log listeners have processed the events.  
        /// </summary>
        bool Async { get; set; }
 
        /// <summary>
        /// Maximum number of outstanding events. Only relevant for <see cref="Async"/> mode.  
        /// </summary>
        int MessageBufferSize { get; set; }
    }
 
    /// <summary>
    /// Extended ILogContext interface.
    /// </summary>
    public interface ILogContext2 : ILogContext
    {
        /// <summary> Registers a new event</summary>
        /// <param name="event"></param>
        void AddEvent(Event @event);
        /// <summary> gets if the context has any listeners.</summary>
        bool HasListeners { get; }
    }
}