Skip to content

Commit 7548c94

Browse files
WenzelMaximilian Blenk
authored andcommitted
utils: expose function to set lief logging
1 parent 06a6084 commit 7548c94

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

checksec/__main__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@
1818
from pathlib import Path
1919
from typing import Iterator, List, Optional, Union
2020

21-
import lief # only to set the lief logging level
2221
from docopt import docopt
2322

2423
from .elf import ELFChecksecData, ELFSecurity, get_libc, is_elf
2524
from .errors import ErrorParsingFailed
2625
from .output import JSONOutput, RichOutput
2726
from .pe import PEChecksecData, PESecurity, is_pe
27+
from .utils import lief_set_logging
2828

2929

3030
def walk_filepath_list(filepath_list: List[Path], recursive: bool = False) -> Iterator[Path]:
@@ -76,12 +76,12 @@ def main(args):
7676
# logging
7777
formatter = "%(asctime)s %(levelname)s:%(name)s:%(message)s"
7878
log_lvl = logging.INFO
79-
lief_logging = lief.logging.LOGGING_LEVEL.CRITICAL # silence lief warnings
79+
lief_logging = logging.CRITICAL # silence lief warnings
8080
if debug:
8181
log_lvl = logging.DEBUG
82-
lief_logging = lief.logging.LOGGING_LEVEL.DEBUG
82+
lief_logging = logging.DEBUG
8383
logging.basicConfig(level=log_lvl, format=formatter)
84-
lief.logging.set_level(lief_logging)
84+
lief_set_logging(lief_logging)
8585

8686
libc_detected = False
8787
# init Libc LIEF object

checksec/utils.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
import struct
66
import subprocess
77
from pathlib import Path
8+
from typing import Dict
89

910
import lddwrap
1011
# cannot use is_elf because of circular dependency
1112
import lief
13+
from lief.logging import LOGGING_LEVEL as lief_loglvl
1214

1315

1416
class 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

3141
def 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

Comments
 (0)