Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use local Logger instead of the global one #20

Merged
merged 2 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/binexport/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from multiprocessing import Pool, Queue, Manager
import queue
from binexport import ProgramBinExport
from binexport.utils import logger

BINARY_FORMAT = {
"application/x-dosexec",
Expand Down Expand Up @@ -111,7 +112,7 @@ def main(ida_path: str, input_file: str, threads: int, verbose: bool) -> None:
ingress.put(file)
total += 1

logging.info(f"Start exporting {total} binaries")
logger.info(f"Start exporting {total} binaries")

i = 0
while True:
Expand All @@ -122,7 +123,7 @@ def main(ida_path: str, input_file: str, threads: int, verbose: bool) -> None:
pp_res = Bcolors.OKGREEN + "OK" + Bcolors.ENDC
else:
pp_res = Bcolors.FAIL + "KO" + Bcolors.ENDC
logging.info(f"[{i}/{total}] {str(path) + '.BinExport'} [{pp_res}]")
logger.info(f"[{i}/{total}] {str(path) + '.BinExport'} [{pp_res}]")
if i == total:
break

Expand Down
6 changes: 2 additions & 4 deletions src/binexport/expression.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from __future__ import annotations
import logging
from functools import cached_property
from typing import TYPE_CHECKING

from binexport.binexport2_pb2 import BinExport2
from binexport.types import ExpressionType
from binexport.utils import logger

if TYPE_CHECKING:
from .program import ProgramBinExport
Expand Down Expand Up @@ -156,6 +156,4 @@ def _parse_protobuf(
self._value = self.pb_expr.symbol

else:
logging.error(
f"Malformed protobuf message. Invalid expression type {self.pb_expr.type}"
)
logger.error(f"Malformed protobuf message. Invalid expression type {self.pb_expr.type}")
7 changes: 3 additions & 4 deletions src/binexport/function.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from __future__ import annotations
import logging
import weakref
import networkx
from functools import cached_property
from typing import TYPE_CHECKING

from binexport.utils import get_basic_block_addr
from binexport.utils import get_basic_block_addr, logger
from binexport.basic_block import BasicBlockBinExport
from binexport.types import FunctionType

Expand Down Expand Up @@ -56,7 +55,7 @@ def __init__(

if is_import:
if self.addr is None:
logging.error("Missing function address for imported function")
logger.error("Missing function address for imported function")
return

assert pb_fun is not None, "pb_fun must be provided"
Expand Down Expand Up @@ -196,7 +195,7 @@ def blocks(self) -> dict[Addr, BasicBlockBinExport]:
)

if basic_block.addr in bblocks:
logging.error(
logger.error(
f"0x{self.addr:x} basic block address (0x{basic_block.addr:x}) already in(idx:{bb_idx})"
)

Expand Down
12 changes: 6 additions & 6 deletions src/binexport/program.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import annotations
import pathlib
import logging
import networkx
import weakref
from collections import defaultdict
Expand All @@ -9,6 +8,7 @@
from binexport.binexport2_pb2 import BinExport2
from binexport.function import FunctionBinExport
from binexport.types import FunctionType
from binexport.utils import logger

if TYPE_CHECKING:
from binexport.types import Addr
Expand Down Expand Up @@ -66,7 +66,7 @@ def __init__(self, file: pathlib.Path | str):
for i, pb_fun in enumerate(self.proto.flow_graph):
f = FunctionBinExport(weakref.ref(self), pb_fun=pb_fun)
if f.addr in self:
logging.error(f"Address collision for 0x{f.addr:x}")
logger.error(f"Address collision for 0x{f.addr:x}")
coll += 1
self[f.addr] = f
count_f += 1
Expand All @@ -81,7 +81,7 @@ def __init__(self, file: pathlib.Path | str):
)
count_imp += 1
if node.address not in self:
logging.error(f"Missing function address: 0x{node.address:x} ({node.type})")
logger.error(f"Missing function address: 0x{node.address:x} ({node.type})")
continue

self[node.address].type = FunctionType.from_proto(node.type)
Expand All @@ -103,7 +103,7 @@ def __init__(self, file: pathlib.Path | str):
for f in self.values():
self.fun_names[f.name] = f

logging.debug(
logger.debug(
f"total all:{count_f}, imported:{count_imp} collision:{coll} (total:{count_f + count_imp + coll})"
)

Expand Down Expand Up @@ -160,15 +160,15 @@ def from_binary_file(

if retcode != 0 and not binexport_file.exists():
# Still continue if retcode != 0, because idat64 something crashes but still manage to export file
logging.warning(
logger.warning(
f"{exec_file.name} failed to export [ret:{retcode}, binexport:{binexport_file.exists()}]"
)
return False

if binexport_file.exists():
return ProgramBinExport(binexport_file) if open_export else True
else:
logging.error(f"{exec_file} can't find binexport generated")
logger.error(f"{exec_file} can't find binexport generated")
return False

@property
Expand Down
5 changes: 5 additions & 0 deletions src/binexport/utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from __future__ import annotations
import logging
from collections.abc import Iterator
from typing import TYPE_CHECKING

Expand Down Expand Up @@ -69,3 +70,7 @@ def instruction_index_range(rng: Binexport2.BasicBlock.IndexRange) -> Iterator[i
:return: iterator over the indices
"""
return range(rng.begin_index, (rng.end_index if rng.end_index else rng.begin_index + 1))


# Main logger object
logger = logging.getLogger("python-binexport")