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: Enhance the logging functionality by including ability to suppress logging if desired #3286

Merged
merged 12 commits into from
Feb 16, 2024
26 changes: 23 additions & 3 deletions BHoM_Engine/Compute/RecordEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
using System.Linq;
using System.ComponentModel;
using System;
using BH.Engine.Base.Objects;

namespace BH.Engine.Base
{
Expand Down Expand Up @@ -96,14 +95,26 @@ public static bool RecordEvent(Event newEvent)
newEvent.StackTrace = string.Join("\n", trace.Split('\n').Skip(4).ToArray());
}

bool areWeSwitchedOff = (newEvent.Type == EventType.Error && !m_RecordError) || (newEvent.Type == EventType.Warning && !m_RecordWarning) || (newEvent.Type == EventType.Note && !m_RecordNote);

Log log = null;
if (!areWeSwitchedOff)
log = Query.DebugLog();
else
log = Query.SwitchedOffDebugLog();

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

if(!areWeSwitchedOff)
OnEventRecorded(newEvent); //Only raise an event if we're not in switched off mode
}

if (newEvent.Type == EventType.Error && m_ThrowError)
throw new Exception(newEvent.ToText());

return true;
}

Expand All @@ -129,6 +140,15 @@ private static void OnEventRecorded(Event ev)
}

/***************************************************/
/**** Private Variables ****/
/***************************************************/

private static bool m_RecordError = true; //Default to true, record any events which come through the system
private static bool m_RecordWarning = true;
private static bool m_RecordNote = true;

private static bool m_ThrowError = false; //Default to false - do not throw errors. However, if a user (developer user or UI user) has switched this on, then errors will be thrown for try/catch statements to handle.
//ToDo: Discuss whether we want this to be true by default and have BHoM_UI switch it off on load, or keep as is. FYI @alelom
}
}

Expand Down
55 changes: 55 additions & 0 deletions BHoM_Engine/Compute/RetriveSwitchedOffLog.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* This file is part of the Buildings and Habitats object Model (BHoM)
* Copyright (c) 2015 - 2024, the respective contributors. All rights reserved.
*
* Each contributor holds copyright over their respective contributions.
* The project versioning (Git) records all such contribution source information.
*
*
* The BHoM is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3.0 of the License, or
* (at your option) any later version.
*
* The BHoM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this code. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
*/

using BH.oM.Base.Debugging;
using BH.oM.Base.Attributes;
using System.Linq;
using System.ComponentModel;
using System;

namespace BH.Engine.Base
{
public static partial class Compute
{
/***************************************************/
/**** Public Methods ****/
/***************************************************/

[Description("Retrive the events recorded while the logging system was switched off and put them into the main log. When the logging system is switched off, recording of events is done in quiet mode which means UIs are not made aware of the events and the main event log does not have knowledge of them. We still record the events though because they may be useful. This method will move any events stored within the log when it was switched off up to the main log for visibiliy and inspection, and will reset the quiet log to a clean state.")]
[Output("True if no error occurs in moving up events recorded during a quiet period.")]
public static bool RetriveSwitchedOffLog()
{
lock(Global.DebugLogLock)
{
Log switchedOffLog = Query.SwitchedOffDebugLog();
Log debugLog = Query.DebugLog();

debugLog.CurrentEvents.AddRange(switchedOffLog.CurrentEvents);
debugLog.AllEvents.AddRange(switchedOffLog.AllEvents);

Query.ResetSwitchedOffDebugLog(); //Now we've moved the switched off log events into the main log, we don't need the switched off log to also keep a copy of them
}

return true;
}
}
}
55 changes: 55 additions & 0 deletions BHoM_Engine/Compute/SwitchErrorThrow.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* This file is part of the Buildings and Habitats object Model (BHoM)
* Copyright (c) 2015 - 2024, the respective contributors. All rights reserved.
*
* Each contributor holds copyright over their respective contributions.
* The project versioning (Git) records all such contribution source information.
*
*
* The BHoM is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3.0 of the License, or
* (at your option) any later version.
*
* The BHoM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this code. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
*/

using BH.oM.Base.Debugging;
using BH.oM.Base.Attributes;
using System.Linq;
using System.ComponentModel;
using System;

namespace BH.Engine.Base
{
public static partial class Compute
{
/***************************************************/
/**** Public Methods ****/
/***************************************************/

[Description("Switch off throwing errors when they reach the event log.")]
public static void SwitchErrorThrowOff()
{
m_ThrowError = false;
}

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

[Description("Switch on throwing errors by the event log. By default, errors are NOT thrown when they reach the BHoM event log. Switching this on will result in errors hitting the event log and being thrown, and if a suitable try/catch is not in place to catch this, you may encounter crashes in your system. Use at your own risk. Please consult the documentation for more information.")] //ToDo: Write the documentation on BHoM.xyz for this system
[Input("areYouSure", "Set this to true if you are sure you want to throw all errors recorded by the event log for try/catch statements to handle.")]
public static void SwitchErrorThrowOn(bool areYouSure)
{
if (!areYouSure)
return;

m_ThrowError = true;
}
}
}
85 changes: 85 additions & 0 deletions BHoM_Engine/Compute/SwitchOffRecording.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* This file is part of the Buildings and Habitats object Model (BHoM)
* Copyright (c) 2015 - 2024, the respective contributors. All rights reserved.
*
* Each contributor holds copyright over their respective contributions.
* The project versioning (Git) records all such contribution source information.
*
*
* The BHoM is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3.0 of the License, or
* (at your option) any later version.
*
* The BHoM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this code. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
*/

using BH.oM.Base.Debugging;
using BH.oM.Base.Attributes;
using System.Linq;
using System.ComponentModel;
using System;

namespace BH.Engine.Base
{
public static partial class Compute
{
/***************************************************/
/**** Public Methods ****/
/***************************************************/

[Description("Switch off the entire logging system used within BHoM. Any part of the code base which tries to log any note, warning, error, or other event into the log system will be housed in the silent recorder and not displayed to users. By default, all recording systems are turned on when BHoM is initialised.")]
[Input("areYouSure", "Set this to true if you are sure you want to turn off all log recording. This boolean exists so that if this component is placed on a BHoM UI accidently, it does not turn off the system unless then expressly requested.")]
public static void SwitchOffRecording(bool areYouSure)
FraserGreenroyd marked this conversation as resolved.
Show resolved Hide resolved
FraserGreenroyd marked this conversation as resolved.
Show resolved Hide resolved
{
if (!areYouSure)
return;

m_RecordError = false;
m_RecordWarning = false;
m_RecordNote = false;
}

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

[Description("Switch off the entire logging system for ERRORS only. All other types of events will be recorded.")]
[Input("areYouSure", "Set this to true if you are sure you want to turn off all ERROR log recording. This boolean exists so that if this component is placed on a BHoM UI accidently, it does not turn off the system unless then expressly requested.")]
public static void SwitchOffRecordingErrors(bool areYouSure)
FraserGreenroyd marked this conversation as resolved.
Show resolved Hide resolved
{
if (!areYouSure)
return;

m_RecordError = false;
}

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

[Description("Switch off the entire logging system for WARNINGS only. All other types of events will be recorded.")]
[Input("areYouSure", "Set this to true if you are sure you want to turn off all WARNING log recording. This boolean exists so that if this component is placed on a BHoM UI accidently, it does not turn off the system unless then expressly requested.")]
public static void SwitchOffRecordingWarnings(bool areYouSure)
FraserGreenroyd marked this conversation as resolved.
Show resolved Hide resolved
{
if (!areYouSure)
return;

m_RecordWarning = false;
}

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

[Description("Switch off the entire logging system for NOTES only. All other types of events will be recorded.")]
[Input("areYouSure", "Set this to true if you are sure you want to turn off all NOTE log recording. This boolean exists so that if this component is placed on a BHoM UI accidently, it does not turn off the system unless then expressly requested.")]
public static void SwitchOffRecordingNotes(bool areYouSure)
FraserGreenroyd marked this conversation as resolved.
Show resolved Hide resolved
{
if (!areYouSure)
return;

m_RecordNote = false;
}
}
}
69 changes: 69 additions & 0 deletions BHoM_Engine/Compute/SwitchOnRecording.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* This file is part of the Buildings and Habitats object Model (BHoM)
* Copyright (c) 2015 - 2024, the respective contributors. All rights reserved.
*
* Each contributor holds copyright over their respective contributions.
* The project versioning (Git) records all such contribution source information.
*
*
* The BHoM is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3.0 of the License, or
* (at your option) any later version.
*
* The BHoM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this code. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
*/

using BH.oM.Base.Debugging;
using BH.oM.Base.Attributes;
using System.Linq;
using System.ComponentModel;
using System;

namespace BH.Engine.Base
{
public static partial class Compute
{
/***************************************************/
/**** Public Methods ****/
/***************************************************/

[Description("Switch on the entire logging system used within BHoM. By default all recording systems are switched on when BHoM is initialised.")]
public static void SwitchOnRecording()
FraserGreenroyd marked this conversation as resolved.
Show resolved Hide resolved
{
m_RecordError = true;
m_RecordWarning = true;
m_RecordNote = true;
}

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

[Description("Switch on the entire logging system for ERRORS only. By default all recording systems are switched on when BHoM is initialised.")]
public static void SwitchOnRecordingErrors()
FraserGreenroyd marked this conversation as resolved.
Show resolved Hide resolved
{
m_RecordError = true;
}

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

[Description("Switch on the entire logging system for WARNINGS only. By default all recording systems are switched on when BHoM is initialised.")]
public static void SwitchOnRecordingWarnings()
FraserGreenroyd marked this conversation as resolved.
Show resolved Hide resolved
{
m_RecordWarning = true;
}

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

[Description("Switch on the entire logging system for NOTES only. By default all recording systems are switched on when BHoM is initialised.")]
public static void SwitchOnRecordingNotes()
FraserGreenroyd marked this conversation as resolved.
Show resolved Hide resolved
{
m_RecordNote = true;
}
}
}
31 changes: 25 additions & 6 deletions BHoM_Engine/Convert/ToText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
using System.Threading.Tasks;
using BH.oM.Base.Attributes;
using BH.oM.Base.Attributes.Enums;
using BH.oM.Base.Debugging;

namespace BH.Engine.Base
{
Expand Down Expand Up @@ -227,6 +228,29 @@ public static string ToText(this Enum item, bool includePath = false)
}
}

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

[Description("Get a string representation of a BHoM event.")]
[Input("bhomEvent", "The BHoM event to convert to text.")]
[Input("includePath", "If true, the stack trace for the event will be included.")]
[Output("Text representation.")]
public static string ToText(this Event bhomEvent, bool includePath = true)
{
if(bhomEvent == null)
return "null";

string result = "";
result += $"Type: {bhomEvent.Type.ToString()}\n";
result += $"Message: {bhomEvent.Message}\n";

if(includePath)
result += $"Stack Trace: {bhomEvent.StackTrace}\n";

result += $"UTC Time: {bhomEvent.UtcTime.ToString("HH:mm:ss dd/MM/yyyy")}\n";

return result;
}

/***************************************************/
/**** Fallback Methods ****/
/***************************************************/
Expand All @@ -244,9 +268,4 @@ private static string ToText(object item, bool includePath = false)

/***************************************************/
}
}





}
Loading