Skip to content

Commit

Permalink
Added new parameter "-d, --debug [OPTIONAL] Increase debug message ve…
Browse files Browse the repository at this point in the history
…rbosity".

Implemented this by changing the log4net config file on the fly.
Removed text string "Error" from all ERROR log messages (as this is anyway outputted).
Changed the log4net configuration output formatting.
See tcunit#21 - "Building the project in TCXaeShell does not collect test results correctly when generating the XML file"
  • Loading branch information
sagatowski committed Mar 27, 2021
1 parent e38dc45 commit 324fd9f
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 26 deletions.
69 changes: 53 additions & 16 deletions TcUnit-Runner/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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"));
Expand All @@ -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)
{
Expand All @@ -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);
}

Expand All @@ -120,21 +148,21 @@ 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);
}

string tcVersion = TcFileUtilities.GetTcVersion(TwinCATProjectFilePath);

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);
}

Expand All @@ -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);
}

Expand All @@ -157,21 +185,21 @@ 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);
}


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);
}

Expand Down Expand Up @@ -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);
}
}
Expand All @@ -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);
}
}
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}
}
Expand All @@ -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))
{
Expand All @@ -373,7 +411,6 @@ static void Main(string[] args)

}

var newErrors = errorList.AddNew(errorItems);
List<ErrorList.Error> errors = new List<ErrorList.Error>(errorList.Where(e => (e.ErrorLevel == vsBuildErrorLevel.vsBuildErrorLevelHigh || e.ErrorLevel == vsBuildErrorLevel.vsBuildErrorLevelLow)));
List<ErrorList.Error> errorsSorted = errors.OrderBy(o => o.Description).ToList();

Expand Down Expand Up @@ -416,7 +453,7 @@ static void DisplayHelp(OptionSet p)
/// </summary>
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);
}

Expand Down
16 changes: 8 additions & 8 deletions TcUnit-Runner/TcUnitResultCollector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ public TcUnitTestResult ParseResults(IEnumerable<ErrorList.Error> 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;
}
}
Expand Down Expand Up @@ -205,7 +205,7 @@ public TcUnitTestResult ParseResults(IEnumerable<ErrorList.Error> 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;
}
}
Expand All @@ -226,9 +226,9 @@ public TcUnitTestResult ParseResults(IEnumerable<ErrorList.Error> 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;
}
}
Expand All @@ -247,7 +247,7 @@ public TcUnitTestResult ParseResults(IEnumerable<ErrorList.Error> 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;
}
}
Expand Down Expand Up @@ -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;
}
}
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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;
}
}
Expand Down
2 changes: 1 addition & 1 deletion TcUnit-Runner/VisualStudioInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

Expand Down
2 changes: 1 addition & 1 deletion TcUnit-Runner/log4net.config
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</root>
<appender name="console" type="log4net.Appender.ConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%utcdate{yyyy-MM-dd HH:mm:ss} - %message%newline" />
<conversionPattern value="%utcdate{yyyy-MM-dd HH:mm:ss} [%-5level] - %message%newline" />
</layout>
</appender>
<appender name="file" type="log4net.Appender.RollingFileAppender">
Expand Down

0 comments on commit 324fd9f

Please sign in to comment.