-
Notifications
You must be signed in to change notification settings - Fork 0
Using debugmsgs Module
This module provides a cleaner and more uniform approach across our codebase so all messages look exactly the same in the output.
See The best practices page for more info on following formatting rules on this specific module.
When using modules.debugmsgs, you can call the errorMsg() method which accepts:
- A custom error message
- The error (caught in a try/except loop)
- (OPTIONAL) custom file path different that the one called in
modules.debugmsgs.init()
Here is an example of all 3 debugging messages:
(main.py)
from modules.debugmsgs import *
# Adds the directory of the script that IMPORTED the module to the debugger
modules.debugmsgs.init(__file__)
# Make sure to follow best practices (messages always start capitalized, no punctuation of any type)
successMsg('Hooray')
debugMsg('The hostname is 668')
errorMsg('Failed to initialize:',e)
But lets say you also imported another module, and you want to use the same module to debug errors!
(motors.py)
from modules.debugmsgs import *
# Adds the directory of the script that IMPORTED the module to the debugger
modules.debugmsgs.init(__file__)
# Make sure to follow best practices (messages always start capitalized, no punctuation of any type)
successMsg('Hooray for motors')
debugMsg('The motor ID is 7')
errorMsg('Failed to connect to motor:',e)
What will happen is that since you called modules.debugmsgs.init(__file__) in both 'main.py' and 'motors.py', debugmsgs will default the file path to main.py since it is located in the "main" namespace. This is because the code being executed first is 'main.py'.
The confict described only arises:
- In the method
modules.debugmsgs.errorMsg() - If you called
modules.debugmsgs.init(__file__)in one python script and are trying to get traceback information in a different python script you imported.
To solve for this, the method modules.debugmsgs.errorMsg() has an optional argument optionalScriptFile.
For further understanding, this is the method modules.debugmsgs.errorMsg():
def errorMsg(message, error, optionalScriptFile=None):
# Print message with different formatting based on operating system
if optionalScriptFile != None:
print('ERROR: '
+ str(message) + '\n\n\t'
+ f'{optionalScriptFile}' + '\n\t> '+
str(error) + f' -> [Line: {error.__traceback__.tb_lineno}]')
else:
print('ERROR: '
+ str(message) + '\n\n\t'
+ f'{scriptFile}' + '\n\t> '+
str(error) + f' -> [Line: {error.__traceback__.tb_lineno}]')
sys.exit(1)
In our example, main.py (see above) imports motors.py, in motors.py, we can change the error messaging code to:
from modules.debugmsgs import *
# Make sure to follow best practices (messages always start capitalized, no punctuation of any type)
successMsg('Hooray for motors')
debugMsg('The motor ID is 7')
errorMsg('Failed to connect to motor:', e, __file__) # <--- SEE HERE
As you can see, manually declaring optionalScriptFile when you call the method allows for you to insert the path to motors.py where the specific method was called, and not the path to main.py that imports python script B.