From 89b0a0741983e70f991ee8de0673b8295b44334f Mon Sep 17 00:00:00 2001 From: varun-r-mallya Date: Thu, 2 Oct 2025 17:57:37 +0530 Subject: [PATCH] add logging level control --- pythonbpf/codegen.py | 14 +++++++++----- tests/passing_tests/perf_buffer_map.py | 4 ++-- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/pythonbpf/codegen.py b/pythonbpf/codegen.py index 2056682..cc9e7bf 100644 --- a/pythonbpf/codegen.py +++ b/pythonbpf/codegen.py @@ -48,7 +48,11 @@ def processor(source_code, filename, module): globals_processing(tree, module) -def compile_to_ir(filename: str, output: str): +def compile_to_ir(filename: str, output: str, loglevel=logging.WARNING): + logging.basicConfig( + level=loglevel, + format="%(asctime)s [%(levelname)s] %(name)s: %(message)s" + ) with open(filename) as f: source = f.read() @@ -134,7 +138,7 @@ def compile_to_ir(filename: str, output: str): return output -def compile() -> bool: +def compile(loglevel=logging.WARNING) -> bool: # Look one level up the stack to the caller of this function caller_frame = inspect.stack()[1] caller_file = Path(caller_frame.filename).resolve() @@ -143,7 +147,7 @@ def compile() -> bool: o_file = caller_file.with_suffix(".o") success = True - success = compile_to_ir(str(caller_file), str(ll_file)) and success + success = compile_to_ir(str(caller_file), str(ll_file), loglevel=loglevel) and success success = bool( subprocess.run( @@ -165,7 +169,7 @@ def compile() -> bool: return success -def BPF() -> BpfProgram: +def BPF(loglevel=logging.WARNING) -> BpfProgram: caller_frame = inspect.stack()[1] src = inspect.getsource(caller_frame.frame) with tempfile.NamedTemporaryFile( @@ -178,7 +182,7 @@ def BPF() -> BpfProgram: f.write(src) f.flush() source = f.name - compile_to_ir(source, str(inter.name)) + compile_to_ir(source, str(inter.name), loglevel=loglevel) subprocess.run( [ "llc", diff --git a/tests/passing_tests/perf_buffer_map.py b/tests/passing_tests/perf_buffer_map.py index 0a3e5d1..560212f 100644 --- a/tests/passing_tests/perf_buffer_map.py +++ b/tests/passing_tests/perf_buffer_map.py @@ -1,7 +1,7 @@ from pythonbpf import bpf, map, struct, section, bpfglobal, compile, compile_to_ir, BPF from pythonbpf.helper import ktime, pid from pythonbpf.maps import PerfEventArray - +import logging from ctypes import c_void_p, c_int32, c_uint64 @@ -42,8 +42,8 @@ def LICENSE() -> str: return "GPL" -compile() compile_to_ir("perf_buffer_map.py", "perf_buffer_map.ll") +compile(loglevel=logging.INFO) b = BPF() b.load_and_attach()