Skip to content

Commit 8d5d810

Browse files
Switch to logging in __init__.py, set load message to debug (#171)
* Switch to logging in `__init__.py`, set load message to debug - Replace print statements with logger.error() and logger.warning() calls. - Follow best practices by using a module-specific logger via getLogger(__name__). - Use parameterized log messages to avoid unnecessary string formatting. - Change the "RAYLIB STATIC x.x.x LOADED" message from direct stderr output to logger warning level. Motivation: In [commaai/openpilot#35076][openpilot-issue], we experienced noisy test outputs because the RAYLIB STATIC message is printed unnecessarily, cluttering stderr. This change allows library consumers to control the visibility of this message. [openpilot-issue]: commaai/openpilot#35076 * use warning rather than debug logging --------- Co-authored-by: Richard Smith <github@electronstudio.co.uk>
1 parent c58d89f commit 8d5d810

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

dynamic/raylib/__init__.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,10 @@
2222
import os
2323
import pathlib
2424
import platform
25+
import logging
2526
from .version import __version__
2627

27-
28+
logger = logging.getLogger(__name__)
2829
MODULE = pathlib.Path(__file__).parent
2930

3031
def raylib_library_path():
@@ -54,9 +55,9 @@ def so_name():
5455
try:
5556
raylib_fname = raylib_library_path()
5657
rl = ffi.dlopen(raylib_fname)
57-
print('LOADED DYNAMICALLY SHARED LIB {} {}'.format(__version__, raylib_fname))
58+
logger.warning('LOADED DYNAMICALLY SHARED LIB {} {}'.format(__version__, raylib_fname))
5859
except Exception as e:
59-
print(e)
60+
logger.exception(e)
6061

6162
LIGHTGRAY =( 200, 200, 200, 255 )
6263
GRAY =( 130, 130, 130, 255 )

raylib/__init__.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,22 @@
1313
# SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0
1414

1515
import sys
16+
import logging
17+
18+
logger = logging.getLogger(__name__)
19+
1620
try:
1721
from ._raylib_cffi import ffi, lib as rl
1822
except ModuleNotFoundError:
19-
print("\n*** ERROR LOADING NATIVE CODE ***\n")
20-
print("See https://github.com/electronstudio/raylib-python-cffi/issues/142\n", file=sys.stderr)
21-
print("Your Python is: "+str(sys.implementation)+"\n", file=sys.stderr)
23+
logger.error("*** ERROR LOADING NATIVE CODE ***")
24+
logger.error("See https://github.com/electronstudio/raylib-python-cffi/issues/142")
25+
logger.error("Your Python is: %s", str(sys.implementation))
2226
raise
27+
2328
from raylib._raylib_cffi.lib import *
2429
from raylib.colors import *
2530
from raylib.defines import *
2631
import cffi
2732
from .version import __version__
2833

29-
print("RAYLIB STATIC "+__version__+" LOADED", file=sys.stderr)
30-
34+
logger.warning("RAYLIB STATIC %s LOADED", __version__)

0 commit comments

Comments
 (0)