I'm using Pygame/SDL's joystick module to get input from a gamepad. Every time I call its get_hat()
method it prints to the console. This is problematic since I use the console to help me debug and now it gets flooded with SDL_JoystickGetHat value:0:
60 times every second. Is there a way I can disable this? Either through an option in Pygame/SDL or suppress console output while the function calls? I saw no mention of this in the Pygame documentation.
edit: This turns out to be due to debugging being turned on when the SDL library was compiled.
You can get around this by assigning the standard out/error (I don't know which one it's going to) to the null device. In Python, the standard out/error files are
sys.stdout
/sys.stderr
, and the null device isos.devnull
, so you doThis should disable these error messages completely. Unfortunately, this will also disable all console output. To get around this, disable output right before calling the
get_hat()
the method, and then restore it by doingwhich restores standard out and error to their original value.
To complete charles's answer, there are two context managers built in to python,
redirect_stdout
andredirect_stderr
which you can use to redirect and or suppress a commands output to a file orStringIO
variable.For a more complete explanation read the docs
I use pythonw.exe (on Windows) instead of python.exe. In other OSes, you could also redirect output to /dev/nul. And in order to still see my debug output, I am using the logging module.
As Demolishun mentions in an answer to a closed duplicate question, there is a thread talking about this issue. The thread is from August of 2009 and one of the developers says the debug code was left in on accident. I had installed Pygame 1.9.1 from pip and the debug output is still present.
To get around it for now, I downloaded the source from pygame.org, removed the print statements from src/joystick.c and compiled the code.
I am on OS X 10.7.5 for what it's worth.
Here's the relevant block of code from joystick.c (via SVN at http://svn.seul.org/viewcvs/viewvc.cgi/trunk/src/joystick.c?view=markup&revision=2652&root=PyGame)
Looks like a problem with having debugging turned on.
If you are on a Debian or Ubuntu machine you can just simply recompile pygame without the messages.
Greetings Max