As LabVIEW programmers we are often tasked with producing large applications that may run for several days at a time without throwing an error or failing. Despite our best efforts we simply cannot hope to predict every possible error that might occur in a complex piece of code and write code ahead of time to deal with it gracefully.
Having said that, if our code does fall over at some point it would help if we knew exactly which error occurred in which VI and when it happened in relation to all the other processes going on in our application.
It is the aim of the AMH Generic Error Handler.vi to offer a one-size-fits-all solution to your error capture and logging needs. This code has been fine-tuned over the years and we now use it in every application we develop. It has saved us countless hours of looking at a LabVIEW error dialog in a crashed application and going “So what went wrong and when?” It also looks good in front of customers as they see that, whatever the problem was, you are able to dig down and find out what happened very quickly.
Who Can Use It
You are free to download and use the code according to a 3-clause BSD license (see the readme file). If you like it then please tell other people to download it too. However, please keep the name as AMH Generic Error Handler as I would like people to know where it came from!
Every LabVIEW programmer has their own wish list when it comes to writing code. If this is not quite for you then I hope it will inspire you to try and write your own error and comment logger.
© 2017 AMH Test Systems Limited
Use of this code implies acceptance of a 3-clause BSD license
Here’s how it works:
- Download the zip file and unzip it
- Place the folder AMH Test Systems into the folder:
C:\Program Files (x86)\National Instruments\LabVIEW 20nn\user.lib
- Launch LabVIEW and from quick-drop type AMH to select the VI
- Place the VI at the start of your main VI
- Initialise the logging and select whether you want to log errors and comments or just errors
- Drop the VI in as many locations within your code as you need error checking or comments
- Ensure that on your last call you set Save Log File Now to TRUE
- Run your code or application and view the timestamped log file(s) in Excel. (Log files are limited to 5 MB to make them readable, otherwise you may end up with files that are 100’s of MB in size and are impossible to read through)
- Once you have finished debugging you can simply turn the whole system off by setting Initialise to FALSE at the first call
The first call of this VI should be at the start of the main VI. It should only be initialised once, there is no need to re-initialise it in any sub VIs. There are three ways to initialise the VI.
1) Initialise Logging of Errors and Comments
i) Select PC or RT or PXI from the Enumerated Typedef. This simply tells the system what folder to store the logs in. Normally this will be in
\\Application Folder\PC Activity Log.
The reason for having options for RT and PXI is that, in a distributed system separate logs can be held on different devices and then ftp’d back to the host PC at the end of a run. For example, if you have a PC and PXI applications running simultaneously you can log data for both separately.
ii) Set Initialise to TRUE. This tells the system to log both errors and comments.
2) Initialise Logging of Errors Only
i) Set Only Log Errors to TRUE. Errors will be logged but comments will not be.
3) No Logging
i) Set Initialise to FALSE. Neither errors nor comments will be logged. All calls to the VI will be transparent and LabVIEW will respond to errors as normal.
Subsequent calls may be made in any subVI or launched VI. There are three ways to call the VI.
1) Log Errors but No Comments
If an error occurs in ‘Some Code’ then the error is logged and cleared without displaying a dialog box. If no error occurs the code does nothing.
2) Log Errors and Comments
This logs the comment “Result = ” to the log file. You can of course format the comment to include important results or other data. If an error occurs in ‘Some Code’ then the error is logged before the comment.
3) Save Log File Now
If Save Log File Now is set to TRUE then the log file is immediately written to. This may be used for important results, but should not be used on every call as this avoids thrashing the hard drive.
You should ensure that when calling the VI for the last time in the main VI that Save Log File Now is set to TRUE. This ensures that the data in the buffer is not lost when the applications exits.
The log files will be in the folder
\\Application Folder\PC/RT/PXI Activity Log
as one or more timestamped CSV files. A long run of a large application will result in several contiguous files of about 5 MB in size. The file size limit is there to prevent files from growing to several hundred MB in size, which makes them impossible to read in Excel.
Within the file there is a date, time (Excel does not format the hours for some reason, you will have to do this manually), calling VI and comment.In the event of an error occurring, the call chain is recorded as well as all the relevant error data.
Each error is preceded by an empty line to help with finding errors when scrolling through a large Excel document by eye.