Skip to content

Commit 89b0a07

Browse files
add logging level control
1 parent 469ca43 commit 89b0a07

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

pythonbpf/codegen.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,11 @@ def processor(source_code, filename, module):
4848
globals_processing(tree, module)
4949

5050

51-
def compile_to_ir(filename: str, output: str):
51+
def compile_to_ir(filename: str, output: str, loglevel=logging.WARNING):
52+
logging.basicConfig(
53+
level=loglevel,
54+
format="%(asctime)s [%(levelname)s] %(name)s: %(message)s"
55+
)
5256
with open(filename) as f:
5357
source = f.read()
5458

@@ -134,7 +138,7 @@ def compile_to_ir(filename: str, output: str):
134138
return output
135139

136140

137-
def compile() -> bool:
141+
def compile(loglevel=logging.WARNING) -> bool:
138142
# Look one level up the stack to the caller of this function
139143
caller_frame = inspect.stack()[1]
140144
caller_file = Path(caller_frame.filename).resolve()
@@ -143,7 +147,7 @@ def compile() -> bool:
143147
o_file = caller_file.with_suffix(".o")
144148

145149
success = True
146-
success = compile_to_ir(str(caller_file), str(ll_file)) and success
150+
success = compile_to_ir(str(caller_file), str(ll_file), loglevel=loglevel) and success
147151

148152
success = bool(
149153
subprocess.run(
@@ -165,7 +169,7 @@ def compile() -> bool:
165169
return success
166170

167171

168-
def BPF() -> BpfProgram:
172+
def BPF(loglevel=logging.WARNING) -> BpfProgram:
169173
caller_frame = inspect.stack()[1]
170174
src = inspect.getsource(caller_frame.frame)
171175
with tempfile.NamedTemporaryFile(
@@ -178,7 +182,7 @@ def BPF() -> BpfProgram:
178182
f.write(src)
179183
f.flush()
180184
source = f.name
181-
compile_to_ir(source, str(inter.name))
185+
compile_to_ir(source, str(inter.name), loglevel=loglevel)
182186
subprocess.run(
183187
[
184188
"llc",

tests/passing_tests/perf_buffer_map.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from pythonbpf import bpf, map, struct, section, bpfglobal, compile, compile_to_ir, BPF
22
from pythonbpf.helper import ktime, pid
33
from pythonbpf.maps import PerfEventArray
4-
4+
import logging
55
from ctypes import c_void_p, c_int32, c_uint64
66

77

@@ -42,8 +42,8 @@ def LICENSE() -> str:
4242
return "GPL"
4343

4444

45-
compile()
4645
compile_to_ir("perf_buffer_map.py", "perf_buffer_map.ll")
46+
compile(loglevel=logging.INFO)
4747
b = BPF()
4848
b.load_and_attach()
4949

0 commit comments

Comments
 (0)