Restricting print() permission for a thread or function?

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
SeaAnt
Posts: 2
Joined: Sun Mar 08, 2020 1:09 pm

Restricting print() permission for a thread or function?

Post by SeaAnt » Sun Mar 08, 2020 1:17 pm

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

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: Restricting print() permission for a thread or function?

Post by Roberthh » Sun Mar 08, 2020 1:35 pm

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.

SeaAnt
Posts: 2
Joined: Sun Mar 08, 2020 1:09 pm

Re: Restricting print() permission for a thread or function?

Post by SeaAnt » Mon Mar 09, 2020 8:49 pm

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

stijn
Posts: 735
Joined: Thu Apr 24, 2014 9:13 am

Re: Restricting print() permission for a thread or function?

Post by stijn » Tue Mar 10, 2020 9:19 am

Replacing the print function itself on a per-module basis is another common trick. At the top of your file:

Code: Select all

def noop(*args):
  pass

print = noop
Doesn't solve the 'per thread' part of your question though: for that I don't see another way then to redefine print like

Code: Select all

allowedThreadIds = []

def print_in_thread(*args):
  if thread.get_ident() in allowedThreadIds:
    print(*args)

print = print_in_thread
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.

Post Reply