Skip to content

Commit

Permalink
Update Tracer.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
LPeter1997 committed Sep 12, 2023
1 parent a83b59d commit 593b1d8
Showing 1 changed file with 26 additions and 3 deletions.
29 changes: 26 additions & 3 deletions src/Draco.Trace/Tracer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,27 @@ namespace Draco.Trace;

public sealed class Tracer
{
/// <summary>
/// Constructs a new <see cref="Tracer"/> for tracing events.
/// </summary>
/// <param name="isEnabled">True, if the tracer should be enabled.</param>
/// <returns>The constructed <see cref="Tracer"/>.</returns>
public static Tracer Create(bool isEnabled) => new(isEnabled);

private static readonly Stopwatch stopwatch = Stopwatch.StartNew();

/// <summary>
/// True, if this tracer is enabled.
/// </summary>
public bool IsEnabled { get; }

private readonly ConcurrentQueue<TraceMessage> messages = new();

private Tracer(bool isEnabled)
{
this.IsEnabled = isEnabled;
}

internal TraceModel ToScribanModel()
{
var result = new TraceModel();
Expand Down Expand Up @@ -64,16 +81,22 @@ MessageModel AddMessage(string message, TimeSpan startTime)
}

public void Event(string message) =>
this.messages.Enqueue(CreateMessage(TraceKind.Event, message));
this.EnqueueMessage(CreateMessage(TraceKind.Event, message));

public IDisposable Begin(string message)
{
this.messages.Enqueue(CreateMessage(TraceKind.Begin, message));
this.EnqueueMessage(CreateMessage(TraceKind.Begin, message));
return new TraceEnd(this);
}

internal void End() =>
this.messages.Enqueue(CreateMessage(TraceKind.End));
this.EnqueueMessage(CreateMessage(TraceKind.End));

private void EnqueueMessage(TraceMessage message)
{
if (!this.IsEnabled) return;
this.messages.Enqueue(message);
}

public void RenderTimeline(Stream stream, CancellationToken cancellationToken)
{
Expand Down

0 comments on commit 593b1d8

Please sign in to comment.