From 593b1d8f499588d8e44d17e1c3b867e62f9585eb Mon Sep 17 00:00:00 2001 From: LPeter1997 Date: Tue, 12 Sep 2023 12:55:48 +0200 Subject: [PATCH] Update Tracer.cs --- src/Draco.Trace/Tracer.cs | 29 ++++++++++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/src/Draco.Trace/Tracer.cs b/src/Draco.Trace/Tracer.cs index 0fb8abb8d..9419d6141 100644 --- a/src/Draco.Trace/Tracer.cs +++ b/src/Draco.Trace/Tracer.cs @@ -11,10 +11,27 @@ namespace Draco.Trace; public sealed class Tracer { + /// + /// Constructs a new for tracing events. + /// + /// True, if the tracer should be enabled. + /// The constructed . + public static Tracer Create(bool isEnabled) => new(isEnabled); + private static readonly Stopwatch stopwatch = Stopwatch.StartNew(); + /// + /// True, if this tracer is enabled. + /// + public bool IsEnabled { get; } + private readonly ConcurrentQueue messages = new(); + private Tracer(bool isEnabled) + { + this.IsEnabled = isEnabled; + } + internal TraceModel ToScribanModel() { var result = new TraceModel(); @@ -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) {