Hello
Completely new with MicroPython. Been tinkering with a Pycom LoPy4 for a week now but I do not believe my question is hardware specific.
So I have been writing some routines to making connecting to WiFi a bit more reliable(retrying to connect if connecting or connecting fails etc.). Its not pretty(I am a horrible programmer) but it works.
I have a lot of print("some information") scattered over the code to make it more clear what the wlan module is doing and what may be going wrong. It's all being run in a thread.
However is there some simple way that I can restrict the thread or main functions permission to print information? Basically I would like to be able to toggle print() abilities for the whole thread on and off without having to clutter the code with if statements at every print().
Can it be done?
Thanks
SeaAnt
Restricting print() permission for a thread or function?
Re: Restricting print() permission for a thread or function?
I do not know about a general flag in the firmware, but atr least you can do something at each print without clobbering it too much in the form:
do_log and print(....)
do_log is the name of a global variable, which you would either set True or False. If False, the print statement will not be executed, if True, it will. Obviously, this is a shorthand for:
if do_log: print(....)
Both variants can be easily done by a search & replace in your code. You may also use some kind of logger function.
do_log and print(....)
do_log is the name of a global variable, which you would either set True or False. If False, the print statement will not be executed, if True, it will. Obviously, this is a shorthand for:
if do_log: print(....)
Both variants can be easily done by a search & replace in your code. You may also use some kind of logger function.
Re: Restricting print() permission for a thread or function?
Thanks for the reply
Yea I knew an inbuilt solution was probably a longshot. But your solution is at least a little less discomforting to look at so I say thank you for the suggestion.
Best regards
SeaAnt
Yea I knew an inbuilt solution was probably a longshot. But your solution is at least a little less discomforting to look at so I say thank you for the suggestion.
Best regards
SeaAnt
Re: Restricting print() permission for a thread or function?
Replacing the print function itself on a per-module basis is another common trick. At the top of your file:
Doesn't solve the 'per thread' part of your question though: for that I don't see another way then to redefine print like
and then add threads which you want to allow print to allowedThreadIds.
But as roberthh says, eventually these are all just workarounds or even hacks whereas you could be using the logging module which does most/all of this out of box.
Code: Select all
def noop(*args):
pass
print = noop
Code: Select all
allowedThreadIds = []
def print_in_thread(*args):
if thread.get_ident() in allowedThreadIds:
print(*args)
print = print_in_thread
But as roberthh says, eventually these are all just workarounds or even hacks whereas you could be using the logging module which does most/all of this out of box.