|
3 | 3 | import platform |
4 | 4 | import os |
5 | 5 | import sys |
| 6 | +import logging |
| 7 | + |
| 8 | +# Set up logger |
| 9 | +logger = logging.getLogger(__name__) |
6 | 10 |
|
7 | 11 |
|
8 | 12 | TYPES = { |
@@ -33,29 +37,60 @@ class LibraryLoadError(OSError): |
33 | 37 |
|
34 | 38 |
|
35 | 39 | 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 | + |
36 | 43 | lib = find_library_path(library, x86_path=x86_path, x64_path=x64_path) |
| 44 | + logger.info("Resolved library path: %s", lib) |
| 45 | + |
37 | 46 | loaded = _do_load(lib, *args, **kwargs) |
38 | 47 | if loaded is not None: |
| 48 | + logger.info("Successfully loaded library: %s", lib) |
39 | 49 | return loaded |
| 50 | + |
| 51 | + logger.error("Failed to load library: %s", lib) |
40 | 52 | raise LibraryLoadError( |
41 | | - "unable to load %r. Provided library path: %r" % (library, path) |
| 53 | + "unable to load %r. Provided library path: %r" % (library, lib) |
42 | 54 | ) |
43 | 55 |
|
44 | 56 |
|
45 | 57 | 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 |
48 | 66 |
|
49 | 67 |
|
50 | 68 | 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) |
54 | 80 | 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 | + |
56 | 84 | ext = get_library_extension() |
| 85 | + logger.debug("Using library extension: %s", ext) |
| 86 | + |
57 | 87 | 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 |
59 | 94 |
|
60 | 95 |
|
61 | 96 | def get_functype(): |
|
0 commit comments