diff --git a/CHANGELOG.md b/CHANGELOG.md index 06e4689..e320b6a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 --- diff --git a/FmuImporter/Common/CommonHelpers.cs b/FmuImporter/Common/CommonHelpers.cs index 9375bea..4dcd9cb 100644 --- a/FmuImporter/Common/CommonHelpers.cs +++ b/FmuImporter/Common/CommonHelpers.cs @@ -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(true, stringBuilder); + } + } + return new Tuple(true, null); } diff --git a/FmuImporter/FmiBridge/Binding/Fmi3Binding.cs b/FmuImporter/FmiBridge/Binding/Fmi3Binding.cs index 3bf4648..7ad7322 100644 --- a/FmuImporter/FmiBridge/Binding/Fmi3Binding.cs +++ b/FmuImporter/FmiBridge/Binding/Fmi3Binding.cs @@ -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; diff --git a/FmuImporter/FmiBridge/Binding/FmiBindingBase.cs b/FmuImporter/FmiBridge/Binding/FmiBindingBase.cs index 07b8428..0b3ef33 100644 --- a/FmuImporter/FmiBridge/Binding/FmiBindingBase.cs +++ b/FmuImporter/FmiBridge/Binding/FmiBindingBase.cs @@ -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; } @@ -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? _loggerAction; diff --git a/FmuImporter/FmuImporter/FmuImporter.cs b/FmuImporter/FmuImporter/FmuImporter.cs index 93a4584..8b3dbf7 100644 --- a/FmuImporter/FmuImporter/FmuImporter.cs +++ b/FmuImporter/FmuImporter/FmuImporter.cs @@ -140,6 +140,7 @@ public FmuImporter( } ExitFmuImporter(); + throw; } } diff --git a/FmuImporter/FmuImporter/Program.cs b/FmuImporter/FmuImporter/Program.cs index 62ebbab..86d593f 100644 --- a/FmuImporter/FmuImporter/Program.cs +++ b/FmuImporter/FmuImporter/Program.cs @@ -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,