55import struct
66import subprocess
77from pathlib import Path
8+ from typing import Dict
89
910import lddwrap
1011# cannot use is_elf because of circular dependency
1112import lief
13+ from lief .logging import LOGGING_LEVEL as lief_loglvl
1214
1315
1416class LibcNotFoundError (Exception ):
@@ -26,6 +28,14 @@ class LibcNotFoundError(Exception):
2628 "/lib/aarch64-linux-gnu/libc.so.6" ,
2729 "/usr/x86_64-gentoo-linux-musl/bin/ld" ,
2830]
31+ PY_LOG_TO_LIEF_LOG : Dict [int , lief_loglvl ] = {
32+ 0 : lief_loglvl .TRACE ,
33+ logging .DEBUG : lief_loglvl .DEBUG ,
34+ logging .INFO : lief_loglvl .INFO ,
35+ logging .WARNING : lief_loglvl .WARNING ,
36+ logging .ERROR : lief_loglvl .ERROR ,
37+ logging .CRITICAL : lief_loglvl .CRITICAL ,
38+ }
2939
3040
3141def find_libc ():
@@ -121,3 +131,21 @@ def find_library_full(name):
121131 if result is None :
122132 raise RuntimeError ("Library %s not found" % name )
123133 return result
134+
135+
136+ def lief_set_logging (lvl : int ):
137+ """Configures LIEF logging level
138+
139+ lvl: Python numeric logging level, corresponding to the logging module level values
140+
141+
142+ example:
143+ import logging
144+ lief_set_logging(logging.WARNING)
145+ """
146+ try :
147+ lief_log = PY_LOG_TO_LIEF_LOG [lvl ]
148+ except KeyError as e :
149+ raise RuntimeError (f"Failed to find LIEF logging level for { lvl } " ) from e
150+ else :
151+ lief .logging .set_level (lief_log )
0 commit comments