Skip to content

Python:warnings

catch_warnings

경고를 캡쳐할 수 있다.

from io import StringIO
from warnings import catch_warnings

import pygame

verbose = 1
logger = getLogger("test")
size = 1920, 1080
flags = pygame.DOUBLEBUF | pygame.OPENGL | pygame.FULLSCREEN

with catch_warnings(record=True) as wms:
    # [Warning]
    # PyGame seems to be running through X11 on top of wayland,
    # instead of wayland directly `pygame.display.set_mode(size, flags)`
    pygame.display.set_mode(size, flags)

    for wm in wms:
        buffer = StringIO()
        if verbose >= 1:
            buffer.write(f"<{wm.category.__name__} ")
            buffer.write(f"message='{str(wm.message)}' ")
            buffer.write(f"file={wm.filename}:{wm.lineno}>")
        else:
            buffer.write(f"{str(wm.message)}")
        logger.warning(buffer.getvalue())

See also

Favorite site