Skip to content

Commit

Permalink
Standardize loggers (#507)
Browse files Browse the repository at this point in the history
* Use __name__ for loggers

* Call all the loggers log
  • Loading branch information
twizmwazin authored Sep 21, 2024
1 parent d6dbcdb commit a8a629d
Show file tree
Hide file tree
Showing 18 changed files with 72 additions and 72 deletions.
2 changes: 1 addition & 1 deletion claripy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@


def BV(name, size, explicit_name=None): # pylint:disable=function-redefined
l.critical(
log.critical(
"DEPRECATION WARNING: claripy.BV is deprecated and will soon be removed. Please use claripy.BVS, instead."
)
print("DEPRECATION WARNING: claripy.BV is deprecated and will soon be removed. Please use claripy.BVS, instead.")
Expand Down
10 changes: 5 additions & 5 deletions claripy/ast/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
from claripy import Backend
from claripy.annotation import Annotation

l = logging.getLogger("claripy.ast")
log = logging.getLogger(__name__)

blake2b_unpacker = struct.Struct("Q")
from_iterable = chain.from_iterable
Expand Down Expand Up @@ -487,7 +487,7 @@ def _arg_serialize(arg: ArgType | Annotation) -> bytes:
if hasattr(arg, "__hash__"):
return hash(arg).to_bytes(8, "little", signed=True)

l.debug("Don't know how to serialize %s, consider implementing __hash__", arg)
log.debug("Don't know how to serialize %s, consider implementing __hash__", arg)
return pickle.dumps(arg)

def __hash__(self) -> int:
Expand Down Expand Up @@ -844,7 +844,7 @@ def children_asts(self) -> Iterator[Base]:
if isinstance(ast, Base):
ast_queue.append(iter(ast.args))

l.debug("Yielding AST %s with hash %s with %d children", ast, ast.hash(), len(ast.args))
log.debug("Yielding AST %s with hash %s with %d children", ast, ast.hash(), len(ast.args))
yield ast

def leaf_asts(self) -> Iterator[Base]:
Expand Down Expand Up @@ -873,7 +873,7 @@ def is_leaf(self) -> bool:
return self.depth == 1

def dbg_is_looped(self) -> Base | bool: # TODO: this return type is bad
l.debug("Checking AST with hash %s for looping", self.hash())
log.debug("Checking AST with hash %s for looping", self.hash())

seen = set()
for child_ast in self.children_asts():
Expand Down Expand Up @@ -1281,7 +1281,7 @@ def simplify(e: T) -> T:

s = e._first_backend("simplify")
if s is None:
l.debug("Unable to simplify expression")
log.debug("Unable to simplify expression")
return e

# Copy some parameters (that should really go to the Annotation backend)
Expand Down
6 changes: 3 additions & 3 deletions claripy/ast/bool.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
if TYPE_CHECKING:
from .fp import FP

l = logging.getLogger("claripy.ast.bool")
log = logging.getLogger(__name__)


class Bool(Base):
Expand Down Expand Up @@ -166,15 +166,15 @@ def is_true(e, exact=None): # pylint:disable=unused-argument
with suppress(BackendError):
return backends.concrete.is_true(e)

l.debug("Unable to tell the truth-value of this expression")
log.debug("Unable to tell the truth-value of this expression")
return False


def is_false(e, exact=None): # pylint:disable=unused-argument
with suppress(BackendError):
return backends.concrete.is_false(e)

l.debug("Unable to tell the truth-value of this expression")
log.debug("Unable to tell the truth-value of this expression")
return False


Expand Down
6 changes: 3 additions & 3 deletions claripy/ast/bv.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from .bits import Bits
from .bool import Bool, If

l = logging.getLogger("claripy.ast.bv")
log = logging.getLogger(__name__)

_bvv_cache = weakref.WeakValueDictionary()

Expand Down Expand Up @@ -146,7 +146,7 @@ def _from_bytes(like, value): # pylint:disable=unused-argument

@staticmethod
def _from_str(like, value): # pylint:disable=unused-argument
l.warning("BVV value is being coerced from a unicode string, encoding as utf-8")
log.warning("BVV value is being coerced from a unicode string, encoding as utf-8")
value = value.encode("utf-8")
return BVV(value)

Expand Down Expand Up @@ -268,7 +268,7 @@ def BVV(value, size=None, **kwargs) -> BV:

if type(value) in (bytes, bytearray, memoryview, str):
if isinstance(value, str):
l.warning("BVV value is a unicode string, encoding as utf-8")
log.warning("BVV value is a unicode string, encoding as utf-8")
value = value.encode("utf-8")

if size is None:
Expand Down
2 changes: 1 addition & 1 deletion claripy/backends/backend_concrete.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
from claripy.errors import BackendError, UnsatError
from claripy.operations import backend_fp_operations, backend_operations, backend_strings_operations

l = logging.getLogger("claripy.backends.backend_concrete")
log = logging.getLogger(__name__)


class BackendConcrete(Backend):
Expand Down
6 changes: 3 additions & 3 deletions claripy/backends/backend_vsa.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
ValueSet,
)

l = logging.getLogger("claripy.backends.backend_vsa")
log = logging.getLogger(__name__)


def arg_filter(f):
Expand Down Expand Up @@ -404,7 +404,7 @@ def union(self, ast):
ret = converted_0.union(converted_1)

if ret is NotImplemented:
l.debug("Union failed, trying the other way around.")
log.debug("Union failed, trying the other way around.")
ret = converted_1.union(converted_0)

return ret
Expand All @@ -431,7 +431,7 @@ def widen(self, ast):

ret = converted_0.widen(converted_1)
if ret is NotImplemented:
l.debug("Widening failed, trying the other way around.")
log.debug("Widening failed, trying the other way around.")
ret = converted_1.widen(converted_0)

return ret
Expand Down
20 changes: 10 additions & 10 deletions claripy/backends/backend_z3.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
from claripy.fp import RM, FSort
from claripy.operations import backend_fp_operations, backend_operations, backend_strings_operations, bound_ops

l = logging.getLogger("claripy.backends.backend_z3")
log = logging.getLogger(__name__)

# pylint:disable=unidiomatic-typecheck

Expand Down Expand Up @@ -126,7 +126,7 @@ def _z3_decl_name_str(ctx, decl):


def z3_solver_sat(solver, extra_constraints, occasion):
l.debug("Doing a check! (%s)", occasion)
log.debug("Doing a check! (%s)", occasion)

result = solver.check(extra_constraints)

Expand Down Expand Up @@ -290,7 +290,7 @@ def downsize(self):
self._sym_cache.clear()

def _name(self, o): # pylint:disable=unused-argument
l.warning("BackendZ3.name() called. This is weird.")
log.warning("BackendZ3.name() called. This is weird.")
raise BackendError("name is not implemented yet")

def _pop_from_ast_cache(self, _, tpl):
Expand Down Expand Up @@ -382,7 +382,7 @@ def StringV(self, ast):
@condom
def StringS(self, ast):
# Maybe this should be an error? Warning for now to support reliant code
l.warning("Converting claripy StringS' to z3 looses length information.")
log.warning("Converting claripy StringS' to z3 looses length information.")
return z3.String(ast.args[0], ctx=self._context)

#
Expand Down Expand Up @@ -411,7 +411,7 @@ def _convert(self, obj): # pylint:disable=arguments-renamed
return z3.BoolRef(z3.Z3_mk_false(self._context.ref()), self._context)
if isinstance(obj, numbers.Number | str) or (hasattr(obj, "__module__") and obj.__module__ in ("z3", "z3.z3")):
return obj
l.debug("BackendZ3 encountered unexpected type %s", type(obj))
log.debug("BackendZ3 encountered unexpected type %s", type(obj))
raise BackendError(f"unexpected type {type(obj)} encountered in BackendZ3")

def call(self, *args, **kwargs): # pylint;disable=arguments-renamed
Expand Down Expand Up @@ -513,7 +513,7 @@ def _abstract_internal(self, ctx, ast, split_on=None):

if op_name == "UNINTERPRETED":
mystery_name = z3.Z3_get_symbol_string(ctx, z3.Z3_get_decl_name(ctx, decl))
l.error("Mystery operation %s in BackendZ3._abstract_internal. Please report this.", mystery_name)
log.error("Mystery operation %s in BackendZ3._abstract_internal. Please report this.", mystery_name)
elif op_name == "Extract":
hi = z3.Z3_get_decl_int_parameter(ctx, decl, 0)
lo = z3.Z3_get_decl_int_parameter(ctx, decl, 1)
Expand Down Expand Up @@ -547,7 +547,7 @@ def _abstract_internal(self, ctx, ast, split_on=None):
f"Unknown Z3 error in abstraction (result_ty == '{result_ty}'). "
"Update your version of Z3, and, if the problem persists, open a claripy issue."
)
l.error(err)
log.error(err)
raise BackendError(err)

if op_name == "If":
Expand Down Expand Up @@ -777,7 +777,7 @@ def _generic_model(self, z3_model):
def _satisfiable(self, extra_constraints=(), solver=None, model_callback=None):
self.solve_count += 1

l.debug("Doing a check! (satisfiable)")
log.debug("Doing a check! (satisfiable)")
if not z3_solver_sat(solver, extra_constraints, "satisfiable"):
return False

Expand Down Expand Up @@ -864,11 +864,11 @@ def _extrema(self, is_max: bool, expr, extra_constraints, signed, solver, model_
sat = z3_solver_sat(solver, constraints, comment)
constraints.pop()
if sat:
l.debug("... still sat")
log.debug("... still sat")
if model_callback is not None:
model_callback(self._generic_model(solver.model()))
else:
l.debug("... now unsat")
log.debug("... now unsat")
if sat == is_max:
lo = middle
else:
Expand Down
2 changes: 1 addition & 1 deletion claripy/balancer.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from .errors import BackendError, ClaripyBalancerError, ClaripyBalancerUnsatError, ClaripyOperationError
from .operations import commutative_operations, opposites

l = logging.getLogger("claripy.balancer")
l = logging.getLogger(__name__)


class Balancer:
Expand Down
2 changes: 1 addition & 1 deletion claripy/frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from . import ast

l = logging.getLogger("claripy.frontends.frontend")
log = logging.getLogger(__name__)


class Frontend:
Expand Down
2 changes: 1 addition & 1 deletion claripy/frontend_mixins/constraint_expansion_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from claripy.ast.bool import Or
from claripy.ast.bv import SGE, SLE, UGE, ULE

l = logging.getLogger("claripy.frontends.cache_mixin")
log = logging.getLogger(__name__)


class ConstraintExpansionMixin:
Expand Down
34 changes: 17 additions & 17 deletions claripy/frontends/composite_frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from claripy import SolverCompositeChild


l = logging.getLogger("claripy.frontends.composite_frontend")
log = logging.getLogger(__name__)
symbolic_count = itertools.count()


Expand Down Expand Up @@ -139,7 +139,7 @@ def _solver_for_names(self, names: set[str]) -> SolverCompositeChild:
:return: A composite child solver.
"""

l.debug("composite_solver._merged_solver_for() running with %d names", len(names))
log.debug("composite_solver._merged_solver_for() running with %d names", len(names))

# compute a transitive closure for all variable names
all_names = set(names)
Expand All @@ -159,12 +159,12 @@ def _solver_for_names(self, names: set[str]) -> SolverCompositeChild:
solvers = list(solvers)

if len(solvers) == 0:
l.debug("... creating new solver")
log.debug("... creating new solver")
return self._template_frontend.blank_copy()
if len(solvers) == 1:
l.debug("... got one solver")
log.debug("... got one solver")
return solvers[0]
l.debug(".... combining %d solvers", len(solvers))
log.debug(".... combining %d solvers", len(solvers))
return solvers[0].combine(solvers[1:])

def _shared_solvers(self, others):
Expand Down Expand Up @@ -194,8 +194,8 @@ def _split_child(self, s):
if len(ss) == 1:
return [s]

l.debug("... split solver %r into %d parts", s, len(ss))
l.debug("... variable counts: %s", [len(cs.variables) for cs in ss])
log.debug("... split solver %r into %d parts", s, len(ss))
log.debug("... variable counts: %s", [len(cs.variables) for cs in ss])

for ns in ss:
self._owned_solvers.add(ns)
Expand Down Expand Up @@ -259,10 +259,10 @@ def _claim(self, s):

def _add_dependent_constraints(self, names, constraints, invalidate_cache=True, **kwargs):
if not invalidate_cache and len(self._solvers_for_variables(names)) > 1:
l.debug("Ignoring cross-solver helper constraints.")
log.debug("Ignoring cross-solver helper constraints.")
return []

l.debug("Adding %d constraints to %d names", len(constraints), len(names))
log.debug("Adding %d constraints to %d names", len(constraints), len(names))
s = self._claim(self._merged_solver_for(names=names))
added = s.add(constraints, invalidate_cache=invalidate_cache, **kwargs)
self._store_child(s, invalidate_cache=invalidate_cache)
Expand Down Expand Up @@ -306,7 +306,7 @@ def check_satisfiability(self, extra_constraints=(), exact=None):
if self._unsat:
return "UNSAT"

l.debug("%r checking satisfiability...", self)
log.debug("%r checking satisfiability...", self)

if len(extra_constraints) != 0:
extra_solver = self._merged_solver_for(lst=extra_constraints)
Expand Down Expand Up @@ -406,21 +406,21 @@ def simplify(self):

new_constraints = []

l.debug("Simplifying %r with %d solvers", self, len(self._solver_list))
log.debug("Simplifying %r with %d solvers", self, len(self._solver_list))
for s in self._solver_list:
if isinstance(s, SimplifySkipperMixin) and s._simplified:
new_constraints += s.constraints
continue

l.debug("... simplifying child solver %r", s)
log.debug("... simplifying child solver %r", s)
s.simplify()
results = self._split_child(s)
for ns in results:
if isinstance(ns, SimplifySkipperMixin):
ns._simplified = True
new_constraints += s.constraints

l.debug("... after-split, %r has %d solvers", self, len(self._solver_list))
log.debug("... after-split, %r has %d solvers", self, len(self._solver_list))

self.constraints = new_constraints
return new_constraints
Expand Down Expand Up @@ -465,11 +465,11 @@ def merge(self, others, merge_conditions, common_ancestor=None):
if common_ancestor is not None:
return self._merge_with_ancestor(common_ancestor, merge_conditions)

l.debug("Merging %s with %d other solvers.", self, len(others))
log.debug("Merging %s with %d other solvers.", self, len(others))
merged = self.blank_copy()
common_solvers = self._shared_solvers(others)
common_ids = {id(s) for s in common_solvers}
l.debug("... %s common solvers", len(common_solvers))
log.debug("... %s common solvers", len(common_solvers))

for s in common_solvers:
self._owned_solvers.discard(s)
Expand All @@ -481,10 +481,10 @@ def merge(self, others, merge_conditions, common_ancestor=None):

noncommon_solvers = [[s for s in cs._solver_list if id(s) not in common_ids] for cs in [self, *others]]

l.debug("... merging noncommon solvers")
log.debug("... merging noncommon solvers")
combined_noncommons = []
for ns in noncommon_solvers:
l.debug("... %d", len(ns))
log.debug("... %d", len(ns))
if len(ns) == 0:
s = self._template_frontend.blank_copy()
combined_noncommons.append(s)
Expand Down
Loading

0 comments on commit a8a629d

Please sign in to comment.