diff --git a/TcUnit-Runner/Program.cs b/TcUnit-Runner/Program.cs index 91278e8..a696908 100644 --- a/TcUnit-Runner/Program.cs +++ b/TcUnit-Runner/Program.cs @@ -37,6 +37,8 @@ using System.Reflection; using System.Text; using System.Threading.Tasks; +using System.Xml; +using System.Xml.XPath; using TCatSysManagerLib; using TwinCAT.Ads; @@ -58,6 +60,7 @@ class Program static void Main(string[] args) { bool showHelp = false; + bool enableDebugLoggingLevel = false; Console.CancelKeyPress += new ConsoleCancelEventHandler(CancelKeyPressHandler); log4net.GlobalContext.Properties["LogLocation"] = AppDomain.CurrentDomain.BaseDirectory + "\\logs"; log4net.Config.XmlConfigurator.ConfigureAndWatch(new System.IO.FileInfo(AppDomain.CurrentDomain.BaseDirectory + "log4net.config")); @@ -68,10 +71,12 @@ static void Main(string[] args) .Add("a=|AmsNetId=", "[OPTIONAL] The AMS NetId of the device of where the project and TcUnit should run", a => AmsNetId = a) .Add("w=|TcVersion=", "[OPTIONAL] The TwinCAT version to be used to load the TwinCAT project", w => ForceToThisTwinCATVersion = w) .Add("u=|Timeout=", "[OPTIONAL] Timeout the process with an error after X minutes", u => Timeout = u) + .Add("d|debug", "[OPTIONAL] Increase debug message verbosity", d => enableDebugLoggingLevel = d != null) .Add("?|h|help", h => showHelp = h != null); try { options.Parse(args); + } catch (OptionException e) { @@ -87,18 +92,41 @@ static void Main(string[] args) Environment.Exit(Constants.RETURN_SUCCESSFULL); } + /* Set logging level. + * This is handled by changing the log4net.config file on the fly. + * The following levels are defined in order of increasing priority: + * - ALL + * - DEBUG + * - INFO + * - WARN + * - ERROR + * - FATAL + * - OFF + */ + XmlDocument doc = new XmlDocument(); + doc.Load(AppDomain.CurrentDomain.BaseDirectory + "log4net.config"); + XmlNode root = doc.DocumentElement; + XmlNode subNode1 = root.SelectSingleNode("root"); + XmlNode nodeForModify = subNode1.SelectSingleNode("level"); + if (enableDebugLoggingLevel) + nodeForModify.Attributes[0].Value = "DEBUG"; + else + nodeForModify.Attributes[0].Value = "INFO"; + doc.Save(AppDomain.CurrentDomain.BaseDirectory + "log4net.config"); + System.Threading.Thread.Sleep(500); // A tiny sleep just to make sure that log4net manages to detect the change in the file + /* Make sure the user has supplied the path for the Visual Studio solution file. * Also verify that this file exists. */ if (VisualStudioSolutionFilePath == null) { - log.Error("ERROR: Visual studio solution path not provided!"); + log.Error("Visual studio solution path not provided!"); Environment.Exit(Constants.RETURN_VISUAL_STUDIO_SOLUTION_PATH_NOT_PROVIDED); } if (!File.Exists(VisualStudioSolutionFilePath)) { - log.Error("ERROR: Visual studio solution " + VisualStudioSolutionFilePath + " does not exist!"); + log.Error("Visual studio solution " + VisualStudioSolutionFilePath + " does not exist!"); Environment.Exit(Constants.RETURN_VISUAL_STUDIO_SOLUTION_PATH_NOT_FOUND); } @@ -120,13 +148,13 @@ static void Main(string[] args) TwinCATProjectFilePath = TcFileUtilities.FindTwinCATProjectFile(VisualStudioSolutionFilePath); if (String.IsNullOrEmpty(TwinCATProjectFilePath)) { - log.Error("ERROR: Did not find TwinCAT project file in solution. Is this a TwinCAT project?"); + log.Error("Did not find TwinCAT project file in solution. Is this a TwinCAT project?"); Environment.Exit(Constants.RETURN_TWINCAT_PROJECT_FILE_NOT_FOUND); } if (!File.Exists(TwinCATProjectFilePath)) { - log.Error("ERROR : TwinCAT project file " + TwinCATProjectFilePath + " does not exist!"); + log.Error("TwinCAT project file " + TwinCATProjectFilePath + " does not exist!"); Environment.Exit(Constants.RETURN_TWINCAT_PROJECT_FILE_NOT_FOUND); } @@ -134,7 +162,7 @@ static void Main(string[] args) if (String.IsNullOrEmpty(tcVersion)) { - log.Error("ERROR: Did not find TwinCAT version in TwinCAT project file path"); + log.Error("Did not find TwinCAT version in TwinCAT project file path"); Environment.Exit(Constants.RETURN_TWINCAT_VERSION_NOT_FOUND); } @@ -147,7 +175,7 @@ static void Main(string[] args) } catch { - log.Error("ERROR: Error loading VS DTE. Is the correct version of Visual Studio and TwinCAT installed? Is the TcUnit-Runner running with administrator privileges?"); + log.Error("Error loading VS DTE. Is the correct version of Visual Studio and TwinCAT installed? Is the TcUnit-Runner running with administrator privileges?"); CleanUpAndExitApplication(Constants.RETURN_ERROR_LOADING_VISUAL_STUDIO_DTE); } @@ -157,13 +185,13 @@ static void Main(string[] args) } catch { - log.Error("ERROR: Error loading the solution. Try to open it manually and make sure it's possible to open and that all dependencies are working"); + log.Error("Error loading the solution. Try to open it manually and make sure it's possible to open and that all dependencies are working"); CleanUpAndExitApplication(Constants.RETURN_ERROR_LOADING_VISUAL_STUDIO_SOLUTION); } if (vsInstance.GetVisualStudioVersion() == null) { - log.Error("ERROR: Did not find Visual Studio version in Visual Studio solution file"); + log.Error("Did not find Visual Studio version in Visual Studio solution file"); CleanUpAndExitApplication(Constants.RETURN_ERROR_FINDING_VISUAL_STUDIO_SOLUTION_VERSION); } @@ -171,7 +199,7 @@ static void Main(string[] args) AutomationInterface automationInterface = new AutomationInterface(vsInstance.GetProject()); if (automationInterface.PlcTreeItem.ChildCount <= 0) { - log.Error("ERROR: No PLC-project exists in TwinCAT project"); + log.Error("No PLC-project exists in TwinCAT project"); CleanUpAndExitApplication(Constants.RETURN_NO_PLC_PROJECT_IN_TWINCAT_PROJECT); } @@ -207,14 +235,14 @@ static void Main(string[] args) } catch { - log.Error("ERROR: Could not parse real time task XML data"); + log.Error("Could not parse real time task XML data"); CleanUpAndExitApplication(Constants.RETURN_NOT_POSSIBLE_TO_PARSE_REAL_TIME_TASK_XML_DATA); } } if (!foundTcUnitTaskName) { - log.Error("ERROR: Could not find task '" + TcUnitTaskName + "' in TwinCAT project"); + log.Error("Could not find task '" + TcUnitTaskName + "' in TwinCAT project"); CleanUpAndExitApplication(Constants.RETURN_FAILED_FINDING_DEFINED_UNIT_TEST_TASK_IN_TWINCAT_PROJECT); } } @@ -240,7 +268,7 @@ static void Main(string[] args) /* Less ore more than one task, which is an error */ else { - log.Error("ERROR: The number of tasks is not equal to 1 (one). Found " + realTimeTasksTreeItem.ChildCount.ToString() + " number of tasks. Please provide which task is the TcUnit task"); + log.Error("The number of tasks is not equal to 1 (one). Found " + realTimeTasksTreeItem.ChildCount.ToString() + " number of tasks. Please provide which task is the TcUnit task"); CleanUpAndExitApplication(Constants.RETURN_TASK_COUNT_NOT_EQUAL_TO_ONE); } } @@ -315,7 +343,7 @@ static void Main(string[] args) } else { - log.Error("ERROR: Build errors in project"); + log.Error("Build errors in project"); CleanUpAndExitApplication(Constants.RETURN_BUILD_ERROR); } @@ -347,7 +375,7 @@ static void Main(string[] args) AdsState adsState = tcAdsClient.ReadState().AdsState; if (adsState != AdsState.Run) { - log.Error("ERROR: invalid AdsState "+adsState +"<>" +AdsState.Run +". This could indicate a PLC Exception, terminating ..."); + log.Error("Invalid AdsState "+adsState +"<>" +AdsState.Run +". This could indicate a PLC Exception, terminating ..."); Environment.Exit(Constants.RETURN_INVALID_ADSSTATE); } } @@ -360,6 +388,16 @@ static void Main(string[] args) } errorItems = vsInstance.GetErrorItems(); + + var newErrors = errorList.AddNew(errorItems); + if (log.IsDebugEnabled) + { + foreach (var error in newErrors) + { + log.Debug(error.ErrorLevel + ": " + error.Description); + } + } + log.Info("... got " + errorItems.Count + " report lines so far."); if (tcUnitResultCollector.AreResultsAvailable(errorItems)) { @@ -373,7 +411,6 @@ static void Main(string[] args) } - var newErrors = errorList.AddNew(errorItems); List errors = new List(errorList.Where(e => (e.ErrorLevel == vsBuildErrorLevel.vsBuildErrorLevelHigh || e.ErrorLevel == vsBuildErrorLevel.vsBuildErrorLevelLow))); List errorsSorted = errors.OrderBy(o => o.Description).ToList(); @@ -416,7 +453,7 @@ static void DisplayHelp(OptionSet p) /// static private void KillProcess(Object source, System.Timers.ElapsedEventArgs e) { - log.Error ("ERROR: timeout occured, killing process(es) ..."); + log.Error("Timeout occured, killing process(es) ..."); CleanUpAndExitApplication(Constants.RETURN_TIMEOUT); } diff --git a/TcUnit-Runner/TcUnitResultCollector.cs b/TcUnit-Runner/TcUnitResultCollector.cs index 4d2aa96..535d02e 100644 --- a/TcUnit-Runner/TcUnitResultCollector.cs +++ b/TcUnit-Runner/TcUnitResultCollector.cs @@ -160,7 +160,7 @@ public TcUnitTestResult ParseResults(IEnumerable errors, string } else { - log.Error("ERROR: While parsing TcUnit results, expected " + expectedErrorLogEntryType.ToString() + " but got " + ErrorLogEntryType.TEST_SUITE_FINISHED_RUNNING.ToString()); + log.Error("While parsing TcUnit results, expected " + expectedErrorLogEntryType.ToString() + " but got " + ErrorLogEntryType.TEST_SUITE_FINISHED_RUNNING.ToString()); return null; } } @@ -205,7 +205,7 @@ public TcUnitTestResult ParseResults(IEnumerable errors, string } else { - log.Error("ERROR: While parsing TcUnit results, expected " + expectedErrorLogEntryType.ToString() + " but got " + ErrorLogEntryType.TEST_SUITE_STATISTICS.ToString()); + log.Error("While parsing TcUnit results, expected " + expectedErrorLogEntryType.ToString() + " but got " + ErrorLogEntryType.TEST_SUITE_STATISTICS.ToString()); return null; } } @@ -226,9 +226,9 @@ public TcUnitTestResult ParseResults(IEnumerable errors, string else { if (expectedErrorLogEntryType != ErrorLogEntryType.TEST_NAME) - log.Error("ERROR: While parsing TcUnit results, expected " + expectedErrorLogEntryType.ToString() + " but got " + ErrorLogEntryType.TEST_NAME.ToString()); + log.Error("While parsing TcUnit results, expected " + expectedErrorLogEntryType.ToString() + " but got " + ErrorLogEntryType.TEST_NAME.ToString()); else - log.Error("ERROR: While parsing TcUnit results, got test case number " + currentTestCaseInTestSuiteNumber + " but expected number is " + testSuiteNumberOfTests); + log.Error("While parsing TcUnit results, got test case number " + currentTestCaseInTestSuiteNumber + " but expected number is " + testSuiteNumberOfTests); return null; } } @@ -247,7 +247,7 @@ public TcUnitTestResult ParseResults(IEnumerable errors, string } else { - log.Error("ERROR: While parsing TcUnit results, expected " + expectedErrorLogEntryType.ToString() + " but got " + ErrorLogEntryType.TEST_CLASS_NAME.ToString()); + log.Error("While parsing TcUnit results, expected " + expectedErrorLogEntryType.ToString() + " but got " + ErrorLogEntryType.TEST_CLASS_NAME.ToString()); return null; } } @@ -305,7 +305,7 @@ or the next test suite */ } else { - log.Error("ERROR: While parsing TcUnit results, expected " + expectedErrorLogEntryType.ToString() + " but got " + ErrorLogEntryType.TEST_STATUS_AND_NUMBER_OF_ASSERTS.ToString()); + log.Error("While parsing TcUnit results, expected " + expectedErrorLogEntryType.ToString() + " but got " + ErrorLogEntryType.TEST_STATUS_AND_NUMBER_OF_ASSERTS.ToString()); return null; } } @@ -325,7 +325,7 @@ or the next test suite */ } else { - log.Error("ERROR: While parsing TcUnit results, expected " + expectedErrorLogEntryType.ToString() + " but got " + ErrorLogEntryType.TEST_ASSERT_MESSAGE.ToString()); + log.Error("While parsing TcUnit results, expected " + expectedErrorLogEntryType.ToString() + " but got " + ErrorLogEntryType.TEST_ASSERT_MESSAGE.ToString()); return null; } } @@ -377,7 +377,7 @@ or the next test suite */ } else { - log.Error("ERROR: While parsing TcUnit results, expected " + expectedErrorLogEntryType.ToString() + " but got " + ErrorLogEntryType.TEST_ASSERT_TYPE.ToString()); + log.Error("While parsing TcUnit results, expected " + expectedErrorLogEntryType.ToString() + " but got " + ErrorLogEntryType.TEST_ASSERT_TYPE.ToString()); return null; } } diff --git a/TcUnit-Runner/VisualStudioInstance.cs b/TcUnit-Runner/VisualStudioInstance.cs index fa14f23..496f8ad 100644 --- a/TcUnit-Runner/VisualStudioInstance.cs +++ b/TcUnit-Runner/VisualStudioInstance.cs @@ -194,7 +194,7 @@ private void LoadDevelopmentToolsEnvironment(string visualStudioVersion, bool is } else { - log.Error("Error: It´s not possible to run TwinCAT in this configuration (TwinCAT-ForcedVersion / pinned TwinCAT Version / installed TwinCAT Version "); + log.Error("It´s not possible to run TwinCAT in this configuration (TwinCAT-ForcedVersion / pinned TwinCAT Version / installed TwinCAT Version "); throw new Exception("Wrong configuration for this TwinCAT version"); } diff --git a/TcUnit-Runner/log4net.config b/TcUnit-Runner/log4net.config index e0668da..950a1f6 100644 --- a/TcUnit-Runner/log4net.config +++ b/TcUnit-Runner/log4net.config @@ -6,7 +6,7 @@ - +