Skip to content

Commit 469ca43

Browse files
replace prints with logger.info
1 parent dc2b611 commit 469ca43

File tree

7 files changed

+90
-72
lines changed

7 files changed

+90
-72
lines changed

pythonbpf/binary_ops.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import ast
22
from llvmlite import ir
3+
from logging import Logger
4+
import logging
35

6+
logger: Logger = logging.getLogger(__name__)
47

58
def recursive_dereferencer(var, builder):
69
"""dereference until primitive type comes out"""
@@ -17,7 +20,7 @@ def recursive_dereferencer(var, builder):
1720

1821

1922
def handle_binary_op(rval, module, builder, var_name, local_sym_tab, map_sym_tab, func):
20-
print(module)
23+
logger.info(f"module {module}")
2124
left = rval.left
2225
right = rval.right
2326
op = rval.op
@@ -43,7 +46,7 @@ def handle_binary_op(rval, module, builder, var_name, local_sym_tab, map_sym_tab
4346
else:
4447
raise SyntaxError("Unsupported right operand type")
4548

46-
print(f"left is {left}, right is {right}, op is {op}")
49+
logger.info(f"left is {left}, right is {right}, op is {op}")
4750

4851
if isinstance(op, ast.Add):
4952
builder.store(builder.add(left, right), local_sym_tab[var_name].var)

pythonbpf/codegen.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@
1212
from pathlib import Path
1313
from pylibbpf import BpfProgram
1414
import tempfile
15+
from logging import Logger
16+
import logging
17+
18+
logger: Logger = logging.getLogger(__name__)
1519

1620
VERSION = "v0.1.3"
1721

@@ -30,11 +34,11 @@ def find_bpf_chunks(tree):
3034

3135
def processor(source_code, filename, module):
3236
tree = ast.parse(source_code, filename)
33-
print(ast.dump(tree, indent=4))
37+
logger.debug(ast.dump(tree, indent=4))
3438

3539
bpf_chunks = find_bpf_chunks(tree)
3640
for func_node in bpf_chunks:
37-
print(f"Found BPF function/struct: {func_node.name}")
41+
logger.info(f"Found BPF function/struct: {func_node.name}")
3842

3943
structs_sym_tab = structs_proc(tree, module, bpf_chunks)
4044
map_sym_tab = maps_proc(tree, module, bpf_chunks)
@@ -121,7 +125,7 @@ def compile_to_ir(filename: str, output: str):
121125

122126
module.add_named_metadata("llvm.ident", [f"PythonBPF {VERSION}"])
123127

124-
print(f"IR written to {output}")
128+
logger.info(f"IR written to {output}")
125129
with open(output, "w") as f:
126130
f.write(f'source_filename = "{filename}"\n')
127131
f.write(str(module))
@@ -157,7 +161,7 @@ def compile() -> bool:
157161
and success
158162
)
159163

160-
print(f"Object written to {o_file}")
164+
logger.info(f"Object written to {o_file}")
161165
return success
162166

163167

pythonbpf/expr_pass.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import ast
22
from llvmlite import ir
3+
from logging import Logger
4+
import logging
35

6+
logger: Logger = logging.getLogger(__name__)
47

58
def eval_expr(
69
func,
@@ -11,22 +14,22 @@ def eval_expr(
1114
map_sym_tab,
1215
structs_sym_tab=None,
1316
):
14-
print(f"Evaluating expression: {ast.dump(expr)}")
17+
logger.info(f"Evaluating expression: {ast.dump(expr)}")
1518
if isinstance(expr, ast.Name):
1619
if expr.id in local_sym_tab:
1720
var = local_sym_tab[expr.id].var
1821
val = builder.load(var)
1922
return val, local_sym_tab[expr.id].ir_type # return value and type
2023
else:
21-
print(f"Undefined variable {expr.id}")
24+
logger.info(f"Undefined variable {expr.id}")
2225
return None
2326
elif isinstance(expr, ast.Constant):
2427
if isinstance(expr.value, int):
2528
return ir.Constant(ir.IntType(64), expr.value), ir.IntType(64)
2629
elif isinstance(expr.value, bool):
2730
return ir.Constant(ir.IntType(1), int(expr.value)), ir.IntType(1)
2831
else:
29-
print("Unsupported constant type")
32+
logger.info("Unsupported constant type")
3033
return None
3134
elif isinstance(expr, ast.Call):
3235
# delayed import to avoid circular dependency
@@ -35,26 +38,26 @@ def eval_expr(
3538
if isinstance(expr.func, ast.Name):
3639
# check deref
3740
if expr.func.id == "deref":
38-
print(f"Handling deref {ast.dump(expr)}")
41+
logger.info(f"Handling deref {ast.dump(expr)}")
3942
if len(expr.args) != 1:
40-
print("deref takes exactly one argument")
43+
logger.info("deref takes exactly one argument")
4144
return None
4245
arg = expr.args[0]
4346
if (
4447
isinstance(arg, ast.Call)
4548
and isinstance(arg.func, ast.Name)
4649
and arg.func.id == "deref"
4750
):
48-
print("Multiple deref not supported")
51+
logger.info("Multiple deref not supported")
4952
return None
5053
if isinstance(arg, ast.Name):
5154
if arg.id in local_sym_tab:
5255
arg = local_sym_tab[arg.id].var
5356
else:
54-
print(f"Undefined variable {arg.id}")
57+
logger.info(f"Undefined variable {arg.id}")
5558
return None
5659
if arg is None:
57-
print("Failed to evaluate deref argument")
60+
logger.info("Failed to evaluate deref argument")
5861
return None
5962
# Since we are handling only name case, directly take type from sym tab
6063
val = builder.load(arg)
@@ -72,7 +75,7 @@ def eval_expr(
7275
structs_sym_tab,
7376
)
7477
elif isinstance(expr.func, ast.Attribute):
75-
print(f"Handling method call: {ast.dump(expr.func)}")
78+
logger.info(f"Handling method call: {ast.dump(expr.func)}")
7679
if isinstance(expr.func.value, ast.Call) and isinstance(
7780
expr.func.value.func, ast.Name
7881
):
@@ -107,15 +110,15 @@ def eval_expr(
107110
attr_name = expr.attr
108111
if var_name in local_sym_tab:
109112
var_ptr, var_type, var_metadata = local_sym_tab[var_name]
110-
print(f"Loading attribute {attr_name} from variable {var_name}")
111-
print(f"Variable type: {var_type}, Variable ptr: {var_ptr}")
113+
logger.info(f"Loading attribute {attr_name} from variable {var_name}")
114+
logger.info(f"Variable type: {var_type}, Variable ptr: {var_ptr}")
112115
metadata = structs_sym_tab[var_metadata]
113116
if attr_name in metadata.fields:
114117
gep = metadata.gep(builder, var_ptr, attr_name)
115118
val = builder.load(gep)
116119
field_type = metadata.field_type(attr_name)
117120
return val, field_type
118-
print("Unsupported expression evaluation")
121+
logger.info("Unsupported expression evaluation")
119122
return None
120123

121124

@@ -129,7 +132,7 @@ def handle_expr(
129132
structs_sym_tab,
130133
):
131134
"""Handle expression statements in the function body."""
132-
print(f"Handling expression: {ast.dump(expr)}")
135+
logger.info(f"Handling expression: {ast.dump(expr)}")
133136
call = expr.value
134137
if isinstance(call, ast.Call):
135138
eval_expr(
@@ -142,4 +145,4 @@ def handle_expr(
142145
structs_sym_tab,
143146
)
144147
else:
145-
print("Unsupported expression type")
148+
logger.info("Unsupported expression type")

0 commit comments

Comments
 (0)