How to hide SDL library debug messages in Python?
Question: I am trying to write a simple python app, which will detect a 2-axis joystick
axis movements and call other functions when one axis has been moved to an
endpoint. I don't do programming regularly, I'm doing sysadmin tasks.

Using the pygame library this would be easy, but when I call the get_axis()
method, I am getting an output on the console. Example :

    
    
    import pygame
    pygame.joystick.init()
    stick = pygame.joystick.Joystick(0)  
    stick.init()
    pygame.init()  
    while True:
     for e in pygame.event.get():
      x=stick.get_axis(0)
      y=stick.get_axis(1)
      print x,y
    

And on the console I got :

    
    
    SDL_JoystickGetAxis value:258:
    SDL_JoystickGetAxis value:258:
    0.00787353515625 0.00787353515625
    

I will be running the script in text mode, not for gaming purposes, so in my
case the output is flooded with useless stuff. Although question(s) similar to
were already posted, in my opinion none of them offers a real solution. The
cause of this seems to be the fact that the SDL library was compiled with
debugging turned on. How can I disable the SDL library console output ? I
don't want to suppress the stdout/stderr as other posts suggest.


Answer: As another answered explained, unless you recompile the SDL source yourself,
SDL is going to try to write to the stdout because they left a debug feature
on. I suggest writing a function to get_axis that first turns off stdout,
calls SDL, turns back on stdout, and then returns the value. Something like:

    
    
    import sys
    import os
    import pygame
    
    def quiet_get_axis(joystick):
       """Returns two floats representing joystick x,y values."""
       sys.stdout = os.devnull
       sys.stderr = os.devnull
       x = joystick.get_axis(0)
       y = joystick.get_axis(1)
       sys.stdout = sys.__stdout__
       sys.stderr = sys.__stderr__
       return x, y
    
    stick = pygame.joystick.Joystick(0)
    stick.init()
    pygame.init()
    
    while True:
        for e in pygame.event.get():
            x, y = quiet_get_axis(stick)
            print x, y
    

