From 739759ddc324132cccb2fddad9635cd08db88790 Mon Sep 17 00:00:00 2001 From: Milan Hanajik Date: Mon, 16 Jan 2017 13:34:40 +0100 Subject: [PATCH] Component/Device name displayed in case of an exception during components loading or devices initialization. --- .../BenchControl/StateMachine.cs | 17 +++++++++++---- .../BenchControl/TbfComponents.cs | 9 ++++++++ TestBenchFramework/MainWnd.cs | 21 +++++++++++++------ .../Resources/Strings.Designer.cs | 18 ++++++++++++++++ TestBenchFramework/Resources/Strings.de.resx | 6 ++++++ TestBenchFramework/Resources/Strings.resx | 6 ++++++ 6 files changed, 67 insertions(+), 10 deletions(-) diff --git a/TestBenchFramework/BenchControl/StateMachine.cs b/TestBenchFramework/BenchControl/StateMachine.cs index c51fce76b..9d53c09a4 100644 --- a/TestBenchFramework/BenchControl/StateMachine.cs +++ b/TestBenchFramework/BenchControl/StateMachine.cs @@ -263,9 +263,14 @@ namespace TBF.BenchControl } } + + public static string CurrentlyInitializedDeviceName; + /// public static string InitializeDevices() { - bool anyComponentIsInSimulMode = false; + CurrentlyInitializedDeviceName = "-"; + + bool anyComponentIsInSimulMode = false; StringBuilder inSimulMode = new StringBuilder(); /// Add all devices to the state machine and initialize them @@ -277,12 +282,16 @@ namespace TBF.BenchControl anyComponentIsInSimulMode = true; } - if (cmpnt is IDevice) + IDevice device = cmpnt as IDevice; + if (device != null) { - (cmpnt as IDevice).Initialize(); - AddDevice(cmpnt as IDevice); /// Only components that were initialized are added + CurrentlyInitializedDeviceName = device.Name; + device.Initialize(); + AddDevice(device); /// Only components that were initialized are added } } + + CurrentlyInitializedDeviceName = "---"; /// Propagate debug levels from parents to children when necessary foreach (var cmpnt in components) diff --git a/TestBenchFramework/BenchControl/TbfComponents.cs b/TestBenchFramework/BenchControl/TbfComponents.cs index 6d59f612d..e86766e82 100644 --- a/TestBenchFramework/BenchControl/TbfComponents.cs +++ b/TestBenchFramework/BenchControl/TbfComponents.cs @@ -125,6 +125,9 @@ namespace TBF.BenchControl return (cmpntFactory != null) ? cmpntFactory.CmpntCfgFromCmpntEntity(entity) : null; } + + public static string CurrentlyLoadedComponentName; + /// /// Load component entities from the database and convert them to TBF components /// derived from IComponent with mutual references. @@ -133,6 +136,8 @@ namespace TBF.BenchControl /// A list of TBF Components public static IList LoadComponentsFromDB(NHibernate.ISession session) { + CurrentlyLoadedComponentName = string.Empty; + foreach (var factory in Factories) factory.ResetStaticProperties(); /// Load the list of components from the database @@ -144,6 +149,8 @@ namespace TBF.BenchControl IList components = new List(); foreach (var entity in cmptnEntities) { + CurrentlyLoadedComponentName = entity.Name; + IComponentFactory cmpntFactory = BenchControl.TbfComponents.CmpntFactoryFromClassName(entity.ClassName); if (cmpntFactory == null) continue; @@ -177,6 +184,8 @@ namespace TBF.BenchControl if (cmpnt.Cfg.DebugLevel != DebugMode.Off) components.Add(cmpnt); } + CurrentlyLoadedComponentName = string.Empty; + return components; } diff --git a/TestBenchFramework/MainWnd.cs b/TestBenchFramework/MainWnd.cs index 7b88fce4a..d805c6b11 100644 --- a/TestBenchFramework/MainWnd.cs +++ b/TestBenchFramework/MainWnd.cs @@ -112,9 +112,14 @@ namespace TBF catch (Exception exc) { benchInitializationFailed = true; - log.FatalFormat("Failed to initialize a test bench component: {0}", exc.Message); - MessageBox.Show(string.Format("Failed to initialize a test bench component: {0}", exc.Message), - Strings.Error, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); + + string errMsg = string.Format(Strings.Failed_to_initialize_component_0_crlf_1, + BenchControl.TbfComponents.CurrentlyLoadedComponentName, + exc.Message); + log.Fatal(errMsg); + MessageBox.Show(errMsg, Strings.Error, + System.Windows.Forms.MessageBoxButtons.OK, + System.Windows.Forms.MessageBoxIcon.Exclamation); } } } @@ -202,9 +207,13 @@ namespace TBF catch (Exception exc) { benchInitializationFailed = true; - log.Fatal("Failed to initialize a test bench component", exc); - MessageBox.Show(exc.Message, Strings.Error, System.Windows.Forms.MessageBoxButtons.OK, - System.Windows.Forms.MessageBoxIcon.Exclamation); + string errMsg = string.Format(Strings.Failed_to_initialize_device_0_crlf_1, + BenchControl.StateMachine.CurrentlyInitializedDeviceName, + exc.Message); + log.Fatal(errMsg); + MessageBox.Show(errMsg, Strings.Error, + System.Windows.Forms.MessageBoxButtons.OK, + System.Windows.Forms.MessageBoxIcon.Exclamation); } } diff --git a/TestBenchFramework/Resources/Strings.Designer.cs b/TestBenchFramework/Resources/Strings.Designer.cs index d3e301d57..e2cc21991 100644 --- a/TestBenchFramework/Resources/Strings.Designer.cs +++ b/TestBenchFramework/Resources/Strings.Designer.cs @@ -1410,6 +1410,24 @@ namespace TBF.Resources { } } + /// + /// Looks up a localized string similar to Failed to initialize component {0}\r\n{1}. + /// + internal static string Failed_to_initialize_component_0_crlf_1 { + get { + return ResourceManager.GetString("Failed_to_initialize_component_0_crlf_1", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Failed to initialize device {0}\r\n{1}. + /// + internal static string Failed_to_initialize_device_0_crlf_1 { + get { + return ResourceManager.GetString("Failed_to_initialize_device_0_crlf_1", resourceCulture); + } + } + /// /// Looks up a localized string similar to fast. /// diff --git a/TestBenchFramework/Resources/Strings.de.resx b/TestBenchFramework/Resources/Strings.de.resx index 38591f049..bc2b1e625 100644 --- a/TestBenchFramework/Resources/Strings.de.resx +++ b/TestBenchFramework/Resources/Strings.de.resx @@ -1374,4 +1374,10 @@ Wollen sie die Änderungen speichern? + + Fehler beim Komponentestart {0}{1}{2} + + + Fehler beim Gerätstart {0}{1}{2} + \ No newline at end of file diff --git a/TestBenchFramework/Resources/Strings.resx b/TestBenchFramework/Resources/Strings.resx index 4240d13d9..6448eaa0e 100644 --- a/TestBenchFramework/Resources/Strings.resx +++ b/TestBenchFramework/Resources/Strings.resx @@ -1624,4 +1624,10 @@ User code + + Failed to initialize device {0}\r\n{1} + + + Failed to initialize component {0}\r\n{1} + \ No newline at end of file