Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BHoM_Engine: ThreadStatic attribute taken off the debug log and replaced by a lock #2750

Merged
merged 3 commits into from
Feb 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions BHoM_Engine/Compute/ClearCurrentEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;

namespace BH.Engine.Base
Expand All @@ -36,11 +37,16 @@ public static partial class Compute
/***************************************************/

[PreviousVersion("5.1", "BH.Engine.Reflection.Compute.ClearCurrentEvents()")]
[Description("Clears the current event log buffer.")]
[Output("success", "True if the buffer is cleared successfully.")]
public static bool ClearCurrentEvents()
{
Log log = Query.DebugLog();
log.CurrentEvents.Clear();
return true;
lock (Global.DebugLogLock)
{
Log log = Query.DebugLog();
log.CurrentEvents.Clear();
return true;
}
}

/***************************************************/
Expand Down
22 changes: 17 additions & 5 deletions BHoM_Engine/Compute/RecordEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
using BH.oM.Base.Debugging;
using BH.oM.Base.Attributes;
using System.Linq;
using System.ComponentModel;

namespace BH.Engine.Base
{
Expand All @@ -33,6 +34,10 @@ public static partial class Compute
/***************************************************/

[PreviousVersion("5.1", "BH.Engine.Reflection.Compute.RecordEvent(System.String, BH.oM.Base.Debugging.EventType)")]
[Description("Records an event in the BHoM event log.")]
[Input("message", "Message related to the event to be logged.")]
[Input("type", "Type of the event to be logged.")]
[Output("success", "True if the event is logged successfully.")]
public static bool RecordEvent(string message, EventType type = EventType.Unknown)
{
return RecordEvent(new Event { Message = message, Type = type });
Expand All @@ -41,6 +46,9 @@ public static bool RecordEvent(string message, EventType type = EventType.Unknow
/***************************************************/

[PreviousVersion("5.1", "BH.Engine.Reflection.Compute.RecordEvent(BH.oM.Base.Debugging.Event)")]
[Description("Records an event in the BHoM event log.")]
[Input("newEvent", "Event to be logged.")]
[Output("success", "True if the event is logged successfully.")]
public static bool RecordEvent(Event newEvent)
{
if (newEvent == null)
Expand All @@ -52,11 +60,15 @@ public static bool RecordEvent(Event newEvent)
string trace = System.Environment.StackTrace;
newEvent.StackTrace = string.Join("\n", trace.Split('\n').Skip(4).ToArray());

Log log = Query.DebugLog();
log.AllEvents.Add(newEvent);
log.CurrentEvents.Add(newEvent);

return true;
lock (Global.DebugLogLock)
{
Log log = Query.DebugLog();
log.AllEvents.Add(newEvent);
log.CurrentEvents.Add(newEvent);
return true;
}
}

/***************************************************/
}
}
35 changes: 24 additions & 11 deletions BHoM_Engine/Compute/RemoveEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
using System;
using System.Collections.Generic;
using BH.oM.Base.Attributes;
using System.ComponentModel;

namespace BH.Engine.Base
{
Expand All @@ -34,28 +35,40 @@ public static partial class Compute
/***************************************************/

[PreviousVersion("5.1", "BH.Engine.Reflection.Compute.RemoveEvent(BH.oM.Base.Debugging.Event)")]
[Description("Removes an event from the BHoM event log.")]
[Input("newEvent", "Event to be removed.")]
[Output("success", "True if the event is removed successfully.")]
public static bool RemoveEvent(Event newEvent)
{
Log log = Query.DebugLog();
bool success = log.AllEvents.Remove(newEvent);
success &= log.CurrentEvents.Remove(newEvent);
return success;
lock (Global.DebugLogLock)
{
Log log = Query.DebugLog();
bool success = log.AllEvents.Remove(newEvent);
success &= log.CurrentEvents.Remove(newEvent);
return success;
}
}

/***************************************************/

[PreviousVersion("5.1", "BH.Engine.Reflection.Compute.RemoveEvents(System.Collections.Generic.List<BH.oM.Base.Debugging.Event>)")]
[Description("Removes a set of events from the BHoM event log.")]
[Input("events", "Events to be removed.")]
[Output("success", "True if the events are removed successfully.")]
public static bool RemoveEvents(List<Event> events)
{
Log log = Query.DebugLog();
bool success = true;
foreach (Event e in events)
lock (Global.DebugLogLock)
{
success &= log.AllEvents.Remove(e);
success &= log.CurrentEvents.Remove(e);
Log log = Query.DebugLog();
bool success = true;
foreach (Event e in events)
{
success &= log.AllEvents.Remove(e);
success &= log.CurrentEvents.Remove(e);
}

return success;
}

return success;
}

/***************************************************/
Expand Down
2 changes: 2 additions & 0 deletions BHoM_Engine/Objects/Global.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ internal static class Global

internal static object AssemblyReflectionLock { get; } = new object();

internal static object DebugLogLock { get; } = new object();


/***************************************************/
/**** Static constructor ****/
Expand Down
11 changes: 6 additions & 5 deletions BHoM_Engine/Query/DebugLog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,22 @@ public static partial class Query
[PreviousVersion("5.1", "BH.Engine.Reflection.Query.DebugLog()")]
internal static Log DebugLog()
{
if (m_DebugLog == null)
m_DebugLog = new Log();
lock (Global.DebugLogLock)
{
if (m_DebugLog == null)
m_DebugLog = new Log();

return m_DebugLog;
return m_DebugLog;
}
}


/***************************************************/
/**** Private Fields ****/
/***************************************************/

[ThreadStatic]
private static Log m_DebugLog = new Log();


/***************************************************/
}
}
Expand Down
19 changes: 15 additions & 4 deletions BHoM_Engine/Query/Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
using System.Linq;
using System.Reflection;
using BH.oM.Base.Attributes;
using System.ComponentModel;

namespace BH.Engine.Base
{
Expand All @@ -36,20 +37,30 @@ public static partial class Query
/***************************************************/

[PreviousVersion("5.1", "BH.Engine.Reflection.Query.AllEvents()")]
[Description("Gets all events stored in the lifetime of the BHoM event log (equal to lifetime of the process).")]
[Output("events", "All events stored in the lifetime of the BHoM event log.")]
public static List<Event> AllEvents()
{
Log log = DebugLog();
return log.AllEvents.ToList();
lock (Global.DebugLogLock)
{
Log log = DebugLog();
return log.AllEvents.ToList();
}
}


/***************************************************/

[PreviousVersion("5.1", "BH.Engine.Reflection.Query.CurrentEvents()")]
[Description("Gets the events from the BHoM event log that are related to the current action.")]
[Output("events", "Events from the BHoM event log that are related to the current action.")]
public static List<Event> CurrentEvents()
{
Log log = DebugLog();
return log.CurrentEvents.ToList();
lock (Global.DebugLogLock)
{
Log log = DebugLog();
return log.CurrentEvents.ToList();
}
}


Expand Down