Skip to content

Commit

Permalink
SKA-432: Fixed lifecycle issue handling & logging improvement
Browse files Browse the repository at this point in the history
* Importer erroneously started SIL Kit lifecycle even if FMU or Importer initialization failed
* FMI binding always logged with Error severity, even if the message was supposed to be logged as debug (-> stack trace was printed to console)
* Errors are only printed to console if they are not logged otherwise
  • Loading branch information
DominikHerr committed Mar 25, 2024
1 parent 3dc1992 commit aa71d94
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ The format is based on `Keep a Changelog (http://keepachangelog.com/en/1.0.0/) <
* The documentation now correctly states that there are prebuilt packages and the requirements to run them
* Fixed a possible StackOverflow exception if an FMU returns a discard or error status code
* Fixed a possible stall of the SIL Kit component if the FMU component terminates unexpectedly
* FMU Importer erroneously started SIL Kit lifecycle even if the initialization of the FMU or Importer failed

---

Expand Down
21 changes: 21 additions & 0 deletions FmuImporter/Common/CommonHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,27 @@ internal static class Helpers
{
if (okResultCodes.Contains(resultCode))
{
if (resultCode == 1 /* warning */ && methodHandle != null)
{
var methodInfo = System.Reflection.MethodBase.GetMethodFromHandle(methodHandle.Value);
if (methodInfo != null)
{
var stringBuilder = new StringBuilder(
$"FMU Importer encountered a call with return value '{resultCode}' ({returnCodeName})");
var fullName = methodInfo.DeclaringType?.FullName + "." + methodInfo.Name;

if (!string.IsNullOrEmpty(fullName))
{
stringBuilder.AppendLine($" while calling '{fullName}'.");
}
else
{
stringBuilder.AppendLine(".");
}
return new Tuple<bool, StringBuilder?>(true, stringBuilder);
}
}

return new Tuple<bool, StringBuilder?>(true, null);
}

Expand Down
4 changes: 2 additions & 2 deletions FmuImporter/FmiBridge/Binding/Fmi3Binding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -926,8 +926,8 @@ public override void Terminate()
}
catch (Exception e)
{
Log(LogSeverity.Error, "Terminate encountered an error:" + e.Message);
Log(LogSeverity.Debug, "Terminate encountered an error:" + e);
Log(LogSeverity.Error, "Terminate encountered an error: " + e.Message);
Log(LogSeverity.Debug, "Terminate encountered an error: " + e);
if (Environment.ExitCode == ExitCodes.Success)
{
Environment.ExitCode = ExitCodes.FmuFailedToTerminate;
Expand Down
7 changes: 6 additions & 1 deletion FmuImporter/FmiBridge/Binding/FmiBindingBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,11 @@ public void ProcessReturnCode(int statusCode, RuntimeMethodHandle? methodHandle)
methodHandle);
if (result.Item1)
{
if (result.Item2 != null)
{
Log(LogSeverity.Warning, result.Item2.ToString());
}

return;
}

Expand Down Expand Up @@ -227,7 +232,7 @@ public void ProcessReturnCode(int statusCode, RuntimeMethodHandle? methodHandle)

protected void Log(LogSeverity severity, string message)
{
_loggerAction?.Invoke(LogSeverity.Error, message);
_loggerAction?.Invoke(severity, message);
}

private Action<LogSeverity, string>? _loggerAction;
Expand Down
1 change: 1 addition & 0 deletions FmuImporter/FmuImporter/FmuImporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ public FmuImporter(
}

ExitFmuImporter();
throw;
}
}

Expand Down
12 changes: 6 additions & 6 deletions FmuImporter/FmuImporter/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -165,13 +165,13 @@ private static async Task Main(string[] args)
{
Environment.ExitCode = ExitCodes.UnhandledException;
}
}

Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(
$"Encountered exception: {e.Message}.\nMore information was written to the debug console.");
Debug.WriteLine($"Encountered exception: {e}.");
Console.ResetColor();
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(
$"Encountered exception: {e.Message}.\nMore information was written to the debug console.");
Debug.WriteLine($"Encountered exception: {e}.");
Console.ResetColor();
}
}
},
fmuPathOption,
Expand Down

0 comments on commit aa71d94

Please sign in to comment.