mirror of
https://github.com/varun-r-mallya/Python-BPF.git
synced 2025-12-31 21:06:25 +00:00
add logging level control
This commit is contained in:
@ -48,7 +48,11 @@ def processor(source_code, filename, module):
|
|||||||
globals_processing(tree, 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:
|
with open(filename) as f:
|
||||||
source = f.read()
|
source = f.read()
|
||||||
|
|
||||||
@ -134,7 +138,7 @@ def compile_to_ir(filename: str, output: str):
|
|||||||
return output
|
return output
|
||||||
|
|
||||||
|
|
||||||
def compile() -> bool:
|
def compile(loglevel=logging.WARNING) -> bool:
|
||||||
# Look one level up the stack to the caller of this function
|
# Look one level up the stack to the caller of this function
|
||||||
caller_frame = inspect.stack()[1]
|
caller_frame = inspect.stack()[1]
|
||||||
caller_file = Path(caller_frame.filename).resolve()
|
caller_file = Path(caller_frame.filename).resolve()
|
||||||
@ -143,7 +147,7 @@ def compile() -> bool:
|
|||||||
o_file = caller_file.with_suffix(".o")
|
o_file = caller_file.with_suffix(".o")
|
||||||
|
|
||||||
success = True
|
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(
|
success = bool(
|
||||||
subprocess.run(
|
subprocess.run(
|
||||||
@ -165,7 +169,7 @@ def compile() -> bool:
|
|||||||
return success
|
return success
|
||||||
|
|
||||||
|
|
||||||
def BPF() -> BpfProgram:
|
def BPF(loglevel=logging.WARNING) -> BpfProgram:
|
||||||
caller_frame = inspect.stack()[1]
|
caller_frame = inspect.stack()[1]
|
||||||
src = inspect.getsource(caller_frame.frame)
|
src = inspect.getsource(caller_frame.frame)
|
||||||
with tempfile.NamedTemporaryFile(
|
with tempfile.NamedTemporaryFile(
|
||||||
@ -178,7 +182,7 @@ def BPF() -> BpfProgram:
|
|||||||
f.write(src)
|
f.write(src)
|
||||||
f.flush()
|
f.flush()
|
||||||
source = f.name
|
source = f.name
|
||||||
compile_to_ir(source, str(inter.name))
|
compile_to_ir(source, str(inter.name), loglevel=loglevel)
|
||||||
subprocess.run(
|
subprocess.run(
|
||||||
[
|
[
|
||||||
"llc",
|
"llc",
|
||||||
|
|||||||
@ -1,7 +1,7 @@
|
|||||||
from pythonbpf import bpf, map, struct, section, bpfglobal, compile, compile_to_ir, BPF
|
from pythonbpf import bpf, map, struct, section, bpfglobal, compile, compile_to_ir, BPF
|
||||||
from pythonbpf.helper import ktime, pid
|
from pythonbpf.helper import ktime, pid
|
||||||
from pythonbpf.maps import PerfEventArray
|
from pythonbpf.maps import PerfEventArray
|
||||||
|
import logging
|
||||||
from ctypes import c_void_p, c_int32, c_uint64
|
from ctypes import c_void_p, c_int32, c_uint64
|
||||||
|
|
||||||
|
|
||||||
@ -42,8 +42,8 @@ def LICENSE() -> str:
|
|||||||
return "GPL"
|
return "GPL"
|
||||||
|
|
||||||
|
|
||||||
compile()
|
|
||||||
compile_to_ir("perf_buffer_map.py", "perf_buffer_map.ll")
|
compile_to_ir("perf_buffer_map.py", "perf_buffer_map.ll")
|
||||||
|
compile(loglevel=logging.INFO)
|
||||||
b = BPF()
|
b = BPF()
|
||||||
b.load_and_attach()
|
b.load_and_attach()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user