Skip to content

Commit 4adb60e

Browse files
author
Lessley Dennington
committed
trace2: add initial functionality
Add initial TRACE2 functionality, including: 1. The ability to add writers (which will eventually write to Normal, Perf, and Event format targets). 2. An abstract Trace2Message class. 3. Logic to send Trace2Messages to writers. 4. Ability to release writers prior to application exit.
1 parent c2366f7 commit 4adb60e

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed

src/shared/Core/ITrace2Writer.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System;
2+
3+
namespace GitCredentialManager;
4+
5+
public interface ITrace2Writer : IDisposable
6+
{
7+
bool Failed { get; }
8+
9+
void Write(Trace2Message message);
10+
}

src/shared/Core/Trace2.cs

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using Newtonsoft.Json;
5+
using Newtonsoft.Json.Converters;
6+
using Newtonsoft.Json.Serialization;
7+
8+
namespace GitCredentialManager;
9+
10+
/// <summary>
11+
/// The different event types tracked in the TRACE2 tracing
12+
/// system.
13+
/// </summary>
14+
public enum Trace2Event
15+
{ }
16+
17+
/// <summary>
18+
/// Represents the application's TRACE2 tracing system.
19+
/// </summary>
20+
public interface ITrace2 : IDisposable
21+
{ }
22+
23+
public class Trace2 : DisposableObject, ITrace2
24+
{
25+
private readonly object _writersLock = new object();
26+
private List<ITrace2Writer> _writers = new List<ITrace2Writer>();
27+
28+
protected override void ReleaseManagedResources()
29+
{
30+
lock (_writersLock)
31+
{
32+
try
33+
{
34+
for (int i = 0; i < _writers.Count; i += 1)
35+
{
36+
using (var writer = _writers[i])
37+
{
38+
_writers.Remove(writer);
39+
}
40+
}
41+
}
42+
catch
43+
{
44+
/* squelch */
45+
}
46+
}
47+
48+
base.ReleaseManagedResources();
49+
}
50+
51+
private void AddWriter(ITrace2Writer writer)
52+
{
53+
ThrowIfDisposed();
54+
55+
lock (_writersLock)
56+
{
57+
// Try not to add the same writer more than once
58+
if (_writers.Contains(writer))
59+
return;
60+
61+
_writers.Add(writer);
62+
}
63+
}
64+
65+
private void WriteMessage(Trace2Message message)
66+
{
67+
ThrowIfDisposed();
68+
69+
lock (_writersLock)
70+
{
71+
if (_writers.Count == 0)
72+
{
73+
return;
74+
}
75+
76+
foreach (var writer in _writers)
77+
{
78+
if (!writer.Failed)
79+
{
80+
writer.Write(message);
81+
}
82+
}
83+
}
84+
}
85+
}
86+
87+
public abstract class Trace2Message
88+
{
89+
private const int SourceColumnMaxWidth = 23;
90+
protected const string TimeFormat = "yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'ffffff'Z'";
91+
92+
[JsonProperty("event", Order = 1)]
93+
public Trace2Event Event { get; set; }
94+
95+
[JsonProperty("sid", Order = 2)]
96+
public string Sid { get; set; }
97+
98+
[JsonProperty("thread", Order = 3)]
99+
public string Thread { get; set; }
100+
101+
[JsonProperty("time", Order = 4)]
102+
public DateTimeOffset Time { get; set; }
103+
104+
[JsonProperty("file", Order = 5)]
105+
public string File { get; set; }
106+
107+
[JsonProperty("line", Order = 6)]
108+
public int Line { get; set; }
109+
}

0 commit comments

Comments
 (0)