Debugging: Difference between revisions
No edit summary |
|||
Line 15: | Line 15: | ||
== Programming == | == Programming == | ||
Whenever you create a SweepMe! instrument driver or a CustomFunction script or enter a formula in to Calc module, you might need to debug your code or your progam flow. | |||
Basically, all error messages are forwarded to the Debug widget. Here are further strategies that might help you: | |||
# If the code has an Exception, it will be shown in the Debug widget. The Exception is shown with the full traceback so that you should be able to identify the line number in your code that makes trouble. The traceback typically containes all lines of code that have been called to raise the Exception. Thus, you carefully need to read each line of the traceback to follow the Exception until you find the origin of the Exception. Often, the last line of the traceback is the important one. | |||
# For simple scripts it is often sufficient to add print(...) statements to your code to show the value of certain variables in the Debug widget. Any print-statement inside your SweepMe! driver will lead to an output via the the Debug widget. Thus, you can output whatever is needed to debug your code. Whenever, your driver is used, e.g. by running a measurement or pushing buttons like 'Retrieve' or "Apply', SweepMe! checks whether the code of the driver or script has been changed and reloads the driver. A restart of SweepMe! is not needed. | |||
# SweepMe! comes with Pnotepad as an editor to open drivers and scripts. You can change the editor via menu 'File' -> 'Options' -> 'Paths'. Here, you can select a more advanced IDE like PyCharm or Visual Studio Code that often come with additional code checking featues in case you have installed it beforehand. | |||
# Advanced debugging features require to run the code in an own Python environment. SweepMe! itself does not come with advanced Debugging features. In case you are working on a more complex instrument driver or [[CustomFunction]] script, you can install the same Python environment as used by SweepMe!. We recommend to use the same Python version as SweepMe!, e.g. Python 3.9 in case of SweepMe! 1.5.6. The requirements.txt file of SweepMe! can be found in the libs folder of your SweepMe! installatiom and be used with pip to install necessary Python packages in the correct combination of versions. Now, you are able to use our open-source library [[pysweepme]] to run a SweepMe! driver in your Python environment and use all debugging features from your IDE such as investitating/modifying the values of variables during runtime, or stopping the code during interpretation. | |||
== ErrorMessage == | == ErrorMessage == |
Latest revision as of 11:12, 15 January 2025
Whenever you create a SweepMe! driver or a [[CustomFunction] script you might need to access error messages to debug your code. For this reason, the Debug widget can be shown in the Dashboard that prints out all messages. In general, all Python errors in SweepMe!, that are not already catched and handled, are shown in this widget.
Debug widget
SweepMe! redirects all error and print messages to the Debug widget which can be accessed via the key 'F2' or the menu 'Window' -> 'Debug'. Furthermore, the Debug console is a permanent widget of the Dashboard window. If you cannot see it, then it is simply hidden. You can show the Debug widget if you right-click on the header of any DockWidget in the DockingStation and select 'Debug' again.
debug.log
All debug messages will also be saved to the file 'debug.log' that you can find in the installation path of your SweepMe! version. This file can be used to create a support ticket. Please make sure that no personal information are sent. Furthermore, whenever SweepMe! starts, the debug.log will be emptied. Please make sure that you do not overwrite delete any debug message by restarting. As SweepMe! opens the file debug.log, the might not be complete until SweepMe! is closed. To check whether the file debug.log contains all error messages, you can make a copy of it. If multiple instances of SweepMe! are open, you will find multiple debug.log files with an additional instance id in the file name. The Debug widget now contains a button in its top-right toolbar to open the file.
Program startup
The debug.log also contains all error message that occur during the startup of your program. Thus, if SweepMe! does not start correctly, you can check the error messages using the debug file. Alternatively, you can start SweepMe! by open Windows command line in the the SweepMe! installation folder and type in "SweepMe!.exe". The option '-d' will result in further debug messages that might help us to identify your problem if you create a support ticket. In this case, you have to use "SweepMe!.exe -d"
Programming
Whenever you create a SweepMe! instrument driver or a CustomFunction script or enter a formula in to Calc module, you might need to debug your code or your progam flow.
Basically, all error messages are forwarded to the Debug widget. Here are further strategies that might help you:
- If the code has an Exception, it will be shown in the Debug widget. The Exception is shown with the full traceback so that you should be able to identify the line number in your code that makes trouble. The traceback typically containes all lines of code that have been called to raise the Exception. Thus, you carefully need to read each line of the traceback to follow the Exception until you find the origin of the Exception. Often, the last line of the traceback is the important one.
- For simple scripts it is often sufficient to add print(...) statements to your code to show the value of certain variables in the Debug widget. Any print-statement inside your SweepMe! driver will lead to an output via the the Debug widget. Thus, you can output whatever is needed to debug your code. Whenever, your driver is used, e.g. by running a measurement or pushing buttons like 'Retrieve' or "Apply', SweepMe! checks whether the code of the driver or script has been changed and reloads the driver. A restart of SweepMe! is not needed.
- SweepMe! comes with Pnotepad as an editor to open drivers and scripts. You can change the editor via menu 'File' -> 'Options' -> 'Paths'. Here, you can select a more advanced IDE like PyCharm or Visual Studio Code that often come with additional code checking featues in case you have installed it beforehand.
- Advanced debugging features require to run the code in an own Python environment. SweepMe! itself does not come with advanced Debugging features. In case you are working on a more complex instrument driver or CustomFunction script, you can install the same Python environment as used by SweepMe!. We recommend to use the same Python version as SweepMe!, e.g. Python 3.9 in case of SweepMe! 1.5.6. The requirements.txt file of SweepMe! can be found in the libs folder of your SweepMe! installatiom and be used with pip to install necessary Python packages in the correct combination of versions. Now, you are able to use our open-source library pysweepme to run a SweepMe! driver in your Python environment and use all debugging features from your IDE such as investitating/modifying the values of variables during runtime, or stopping the code during interpretation.
ErrorMessage
You can import from the modeule ErrorMessage from the pysweepme package that comes with SweepMe!. Two convenience functions called 'error' and 'debug' that can be used to output the last error traceback or debug messages with timestamp.
from ErrorMessage import error, debug
Usage
from pysweepme.ErrorMessage import error
error("<your message>")
will print <your message> and the last Exception that has been raised. It is typically used in combination with try-except
from pysweepme.ErrorMessage import debug
debug("<your message>")
will print <your message> starting with a time stamp