Skip to content

Commit d5450f0

Browse files
committed
Add comprehensive logging to library loading process
- Add logging functionality to track library loading workflow - Improve error handling with try-catch and detailed error messages - Add debug logging for architecture detection and path resolution - Log library path existence and loading status - Break down complex operations for better traceability - Fix typo in LibraryLoadError message (path -> lib) This change makes it easier to diagnose library loading issues by providing detailed logs about the loading process, architecture detection, and path resolution steps.
1 parent bc94811 commit d5450f0

1 file changed

Lines changed: 43 additions & 8 deletions

File tree

libloader/libloader.py

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
import platform
44
import os
55
import sys
6+
import logging
7+
8+
# Set up logger
9+
logger = logging.getLogger(__name__)
610

711

812
TYPES = {
@@ -33,29 +37,60 @@ class LibraryLoadError(OSError):
3337

3438

3539
def load_library(library, x86_path=".", x64_path=".", *args, **kwargs):
40+
logger.info("Attempting to load library: %s", library)
41+
logger.debug("x86_path: %s, x64_path: %s", os.path.abspath(x86_path), os.path.abspath(x64_path))
42+
3643
lib = find_library_path(library, x86_path=x86_path, x64_path=x64_path)
44+
logger.info("Resolved library path: %s", lib)
45+
3746
loaded = _do_load(lib, *args, **kwargs)
3847
if loaded is not None:
48+
logger.info("Successfully loaded library: %s", lib)
3949
return loaded
50+
51+
logger.error("Failed to load library: %s", lib)
4052
raise LibraryLoadError(
41-
"unable to load %r. Provided library path: %r" % (library, path)
53+
"unable to load %r. Provided library path: %r" % (library, lib)
4254
)
4355

4456

4557
def _do_load(file, *args, **kwargs):
46-
loader = TYPES[platform.system()]["loader"]
47-
return loader(file, *args, **kwargs)
58+
system = platform.system()
59+
logger.debug("Using loader for system: %s", system)
60+
loader = TYPES[system]["loader"]
61+
try:
62+
return loader(file, *args, **kwargs)
63+
except Exception as e:
64+
logger.error("Error loading %s: %s", file, str(e))
65+
return None
4866

4967

5068
def find_library_path(libname, x86_path=".", x64_path="."):
51-
libname = "%s%s" % (TYPES[platform.system()]["prefix"], libname)
52-
if platform.architecture()[0] == "64bit":
53-
path = os.path.join(x64_path, libname)
69+
system = platform.system()
70+
prefix = TYPES[system]["prefix"]
71+
libname_with_prefix = "%s%s" % (prefix, libname)
72+
logger.debug("Library name with prefix: %s", libname_with_prefix)
73+
74+
arch = platform.architecture()[0]
75+
logger.debug("Detected architecture: %s", arch)
76+
77+
if arch == "64bit":
78+
path = os.path.join(x64_path, libname_with_prefix)
79+
logger.debug("Using 64-bit path: %s", x64_path)
5480
else:
55-
path = os.path.join(x86_path, libname)
81+
path = os.path.join(x86_path, libname_with_prefix)
82+
logger.debug("Using 32-bit path: %s", x86_path)
83+
5684
ext = get_library_extension()
85+
logger.debug("Using library extension: %s", ext)
86+
5787
path = "%s%s" % (path, ext)
58-
return os.path.abspath(path)
88+
abs_path = os.path.abspath(path)
89+
90+
logger.info("Resolved library path: %s", abs_path)
91+
logger.debug("Path exists: %s", os.path.exists(abs_path))
92+
93+
return abs_path
5994

6095

6196
def get_functype():

0 commit comments

Comments
 (0)