Skip to content

Commit d909209

Browse files
committed
Make debug logging suck mildly less
1 parent 4602c99 commit d909209

File tree

1 file changed

+54
-44
lines changed

1 file changed

+54
-44
lines changed

CppHeaderParser/CppHeaderParser.py

Lines changed: 54 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
# http://www.opensource.org/licenses/bsd-license.php
4646
#
4747

48+
from __future__ import print_function
49+
4850

4951
from collections import deque
5052
import os
@@ -69,7 +71,7 @@
6971
# Controls warning_print
7072
print_warnings = 1
7173
# Controls debug_print
72-
debug = 0
74+
debug = 1 if os.environ.get("CPPHEADERPARSER_DEBUG") == "1" else 0
7375
# Controls trace_print
7476
debug_trace = 0
7577

@@ -82,25 +84,31 @@ def raise_exc(e, src_e):
8284
raise e
8385

8486

85-
def error_print(arg):
87+
def error_print(fmt, *args):
8688
if print_errors:
87-
print(("[%4d] %s" % (inspect.currentframe().f_back.f_lineno, arg)))
89+
fmt = "[%4d] " + fmt
90+
args = (inspect.currentframe().f_back.f_lineno,) + args
91+
print(fmt % args)
8892

8993

90-
def warning_print(arg):
94+
def warning_print(fmt, *args):
9195
if print_warnings:
92-
print(("[%4d] %s" % (inspect.currentframe().f_back.f_lineno, arg)))
96+
fmt = "[%4d] " + fmt
97+
args = (inspect.currentframe().f_back.f_lineno,) + args
98+
print(fmt % args)
9399

94100

95-
def debug_print(arg):
101+
def debug_print(fmt, *args):
96102
if debug:
97-
print(("[%4d] %s" % (inspect.currentframe().f_back.f_lineno, arg)))
103+
fmt = "[%4d] " + fmt
104+
args = (inspect.currentframe().f_back.f_lineno,) + args
105+
print(fmt % args)
98106

99107

100-
def trace_print(*arg):
108+
def trace_print(*args):
101109
if debug_trace:
102110
sys.stdout.write("[%s] " % (inspect.currentframe().f_back.f_lineno))
103-
for a in arg:
111+
for a in args:
104112
sys.stdout.write("%s " % a)
105113
sys.stdout.write("\n")
106114

@@ -392,7 +400,7 @@ def _consume_parens(stack):
392400

393401

394402
def _parse_template_decl(stack):
395-
debug_print("_parse_template_decl: %s" % stack)
403+
debug_print("_parse_template_decl: %s", stack)
396404
params = []
397405
param = CppTemplateParam()
398406
i = 0
@@ -492,7 +500,7 @@ def _parse_cppclass_name(c, stack):
492500

493501

494502
def _parse_cpp_base(stack):
495-
debug_print("Parsing base: %s" % stack)
503+
debug_print("Parsing base: %s", stack)
496504
inherits = []
497505
i = 0
498506
sl = len(stack)
@@ -634,8 +642,8 @@ def __init__(self, nameStack, curTemplate, doxygen, location):
634642
self._public_forward_declares = []
635643
self["namespace"] = ""
636644

637-
debug_print("Class: %s" % nameStack)
638-
debug_print("Template: %s" % curTemplate)
645+
debug_print("Class: %s", nameStack)
646+
debug_print("Template: %s", curTemplate)
639647

640648
if len(nameStack) < 2:
641649
nameStack.insert(1, "") # anonymous struct
@@ -911,8 +919,8 @@ def show(self):
911919
return "\n\t\t ".join(r)
912920

913921
def __init__(self, nameStack, curClass, methinfo, curTemplate, doxygen, location):
914-
debug_print("Method: %s" % nameStack)
915-
debug_print("Template: %s" % curTemplate)
922+
debug_print("Method: %s", nameStack)
923+
debug_print("Template: %s", curTemplate)
916924

917925
if doxygen:
918926
self["doxygen"] = doxygen
@@ -994,10 +1002,10 @@ def __init__(self, nameStack, curClass, methinfo, curTemplate, doxygen, location
9941002

9951003
paramsStack = self._params_helper1(nameStack)
9961004

997-
debug_print("curTemplate: %s" % curTemplate)
1005+
debug_print("curTemplate: %s", curTemplate)
9981006
if curTemplate:
9991007
self["template"] = curTemplate
1000-
debug_print("SET self['template'] to `%s`" % self["template"])
1008+
debug_print("SET self['template'] to `%s`", self["template"])
10011009

10021010
params = []
10031011
# See if there is a doxygen comment for the variable
@@ -1118,7 +1126,7 @@ class CppVariable(_CppVariable):
11181126
Vars = []
11191127

11201128
def __init__(self, nameStack, doxygen, location, **kwargs):
1121-
debug_print("trace %s" % nameStack)
1129+
debug_print("trace %s", nameStack)
11221130
if len(nameStack) and nameStack[0] == "extern":
11231131
self["extern"] = True
11241132
del nameStack[0]
@@ -1130,7 +1138,7 @@ def __init__(self, nameStack, doxygen, location, **kwargs):
11301138
arrayStack = nameStack[nameStack.index("[") :]
11311139
if nameStack.count("[") > 1:
11321140
debug_print("Multi dimensional array")
1133-
debug_print("arrayStack=%s" % arrayStack)
1141+
debug_print("arrayStack=%s", arrayStack)
11341142
nums = [x for x in arrayStack if x.isdigit()]
11351143
# Calculate size by multiplying all dimensions
11361144
p = 1
@@ -1153,7 +1161,7 @@ def __init__(self, nameStack, doxygen, location, **kwargs):
11531161
if doxygen:
11541162
self["doxygen"] = doxygen
11551163

1156-
debug_print("Variable: %s" % nameStack)
1164+
debug_print("Variable: %s", nameStack)
11571165

11581166
set_location_info(self, location)
11591167
self["function_pointer"] = 0
@@ -1163,7 +1171,7 @@ def __init__(self, nameStack, doxygen, location, **kwargs):
11631171
self["type"] = nameStack[0]
11641172
self["name"] = ""
11651173
else:
1166-
error_print(_stack_)
1174+
error_print("%s", _stack_)
11671175
assert 0
11681176

11691177
elif is_function_pointer_stack(nameStack): # function pointer
@@ -1596,7 +1604,7 @@ def finalize_vars(self):
15961604
var["fundamental"] = True
15971605

15981606
elif var["parent"]:
1599-
warning_print("WARN unresolved %s" % _tag)
1607+
warning_print("WARN unresolved %s", _tag)
16001608
var["ctypes_type"] = "ctypes.c_void_p"
16011609
var["unresolved"] = True
16021610

@@ -1710,7 +1718,7 @@ def finalize_vars(self):
17101718
elif tag.startswith(
17111719
"_"
17121720
): # assume starting with underscore is not important for wrapping
1713-
warning_print("WARN unresolved %s" % _tag)
1721+
warning_print("WARN unresolved %s", _tag)
17141722
var["ctypes_type"] = "ctypes.c_void_p"
17151723
var["unresolved"] = True
17161724

@@ -1803,9 +1811,7 @@ def finalize_vars(self):
18031811
trace_print("Adding #include %s" % macro)
18041812
self.includes.append(re.split("[\t ]+", macro, 1)[1].strip())
18051813
else:
1806-
debug_print(
1807-
"Cant detect what to do with precomp macro '%s'" % macro
1808-
)
1814+
debug_print("Cant detect what to do with precomp macro '%s'", macro)
18091815
except:
18101816
pass
18111817
self._precomp_macro_buf = None
@@ -1887,7 +1893,7 @@ def finalize(self):
18871893
klass = self.classes[b]
18881894
meth["returns_class"] = a + "::" + b
18891895
elif "<" in b and ">" in b:
1890-
warning_print("WARN-can not return template: %s" % b)
1896+
warning_print("WARN-can not return template: %s", b)
18911897
meth["returns_unknown"] = True
18921898
elif b in self.global_enums:
18931899
enum = self.global_enums[b]
@@ -2250,8 +2256,9 @@ def _evaluate_property_stack(self, clearStack=True, addToVar=None):
22502256
):
22512257
self.nameStack.insert(0, filteredParseHistory[-1]["item"]["name"])
22522258
debug_print(
2253-
"DEANONYMOIZING %s to type '%s'"
2254-
% (self.nameStack[1], self.nameStack[0])
2259+
"DEANONYMOIZING %s to type '%s'",
2260+
self.nameStack[1],
2261+
self.nameStack[0],
22552262
)
22562263
if "," in self.nameStack: # Maybe we have a variable list
22572264
# Figure out what part is the variable separator but remember templates of function pointer
@@ -2333,7 +2340,7 @@ def _evaluate_class_stack(self):
23332340
else: # struct
23342341
self.curAccessSpecifier = "public"
23352342
debug_print(
2336-
"curAccessSpecifier changed/defaulted to %s" % self.curAccessSpecifier
2343+
"curAccessSpecifier changed/defaulted to %s", self.curAccessSpecifier
23372344
)
23382345
if self.nameStack[0] == "union":
23392346
newClass = CppUnion(
@@ -2518,7 +2525,7 @@ def __init__(self, headerFileName, argType="file", encoding=None, **kwargs):
25182525
self.accessSpecifierStack = []
25192526
self.accessSpecifierScratch = []
25202527
debug_print(
2521-
"curAccessSpecifier changed/defaulted to %s" % self.curAccessSpecifier
2528+
"curAccessSpecifier changed/defaulted to %s", self.curAccessSpecifier
25222529
)
25232530
self.initextra()
25242531
# Old namestacks for a given level
@@ -2600,7 +2607,7 @@ def __init__(self, headerFileName, argType="file", encoding=None, **kwargs):
26002607
if locEnd:
26012608
# Strip it out but keep the linecount the same so line numbers are right
26022609
match_str = headerFileStr[locStart:locEnd]
2603-
debug_print("Striping out '%s'" % match_str)
2610+
debug_print("Striping out '%s'", match_str)
26042611
num_newlines = len([a for a in match_str if a == "\n"])
26052612
headerFileStr = headerFileStr.replace(
26062613
headerFileStr[locStart:locEnd], "\n" * num_newlines
@@ -2637,7 +2644,7 @@ def __init__(self, headerFileName, argType="file", encoding=None, **kwargs):
26372644
):
26382645
self.anon_union_counter[1] -= 1
26392646
tok.value = TagStr(tok.value, location=tok.location)
2640-
# debug_print("TOK: %s"%tok)
2647+
# debug_print("TOK: %s", tok)
26412648
if tok.type == "NAME":
26422649
if tok.value in self.IGNORE_NAMES:
26432650
continue
@@ -2662,7 +2669,7 @@ def __init__(self, headerFileName, argType="file", encoding=None, **kwargs):
26622669
self.stack.append(tok.value)
26632670

26642671
if tok.type in ("PRECOMP_MACRO", "PRECOMP_MACRO_CONT"):
2665-
debug_print("PRECOMP: %s" % tok)
2672+
debug_print("PRECOMP: %s", tok)
26662673
self._precomp_macro_buf.append(tok.value)
26672674
self.stack = []
26682675
self.nameStack = []
@@ -2723,7 +2730,7 @@ def __init__(self, headerFileName, argType="file", encoding=None, **kwargs):
27232730
# self.stack = []; print 'BRACE DEPTH', self.braceDepth, 'NS', len(self.nameSpaces)
27242731
if self.curClass:
27252732
debug_print(
2726-
"CURBD %s" % self._classes_brace_level[self.curClass]
2733+
"CURBD %s", self._classes_brace_level[self.curClass]
27272734
)
27282735

27292736
if (self.braceDepth == 0) or (
@@ -2746,7 +2753,7 @@ def __init__(self, headerFileName, argType="file", encoding=None, **kwargs):
27462753
pass
27472754
elif tok.type in _namestack_str_tokens:
27482755
if tok.value in ignoreSymbols:
2749-
debug_print("Ignore symbol %s" % tok.value)
2756+
debug_print("Ignore symbol %s", tok.value)
27502757
elif tok.value == "class":
27512758
self.nameStack.append(tok.value)
27522759
elif tok.value in supportedAccessSpecifier:
@@ -2764,8 +2771,8 @@ def __init__(self, headerFileName, argType="file", encoding=None, **kwargs):
27642771
self.curAccessSpecifier = tok.value
27652772
self.accessSpecifierScratch.append(tok.value)
27662773
debug_print(
2767-
"curAccessSpecifier updated to %s"
2768-
% self.curAccessSpecifier
2774+
"curAccessSpecifier updated to %s",
2775+
self.curAccessSpecifier,
27692776
)
27702777
self.stack = []
27712778
else:
@@ -2831,7 +2838,8 @@ def __init__(self, headerFileName, argType="file", encoding=None, **kwargs):
28312838
)
28322839
else:
28332840
msg = "Error parsing %s%s\nError around: %s" % (
2834-
self.headerFileName, context,
2841+
self.headerFileName,
2842+
context,
28352843
" ".join(self.nameStack),
28362844
)
28372845

@@ -2956,8 +2964,10 @@ def _evaluate_stack(self, token=None):
29562964
nameStackCopy = self.nameStack[:]
29572965

29582966
debug_print(
2959-
"Evaluating stack %s\n BraceDepth: %s (called from %d)"
2960-
% (self.nameStack, self.braceDepth, inspect.currentframe().f_back.f_lineno)
2967+
"Evaluating stack %s\n BraceDepth: %s (called from %d)",
2968+
self.nameStack,
2969+
self.braceDepth,
2970+
inspect.currentframe().f_back.f_lineno,
29612971
)
29622972

29632973
# Handle special case of overloading operator ()
@@ -2968,9 +2978,9 @@ def _evaluate_stack(self, token=None):
29682978
self.nameStack[operator_index] = "operator()"
29692979

29702980
if len(self.curClass):
2971-
debug_print("%s (%s) " % (self.curClass, self.curAccessSpecifier))
2981+
debug_print("%s (%s) ", self.curClass, self.curAccessSpecifier)
29722982
else:
2973-
debug_print("<anonymous> (%s) " % self.curAccessSpecifier)
2983+
debug_print("<anonymous> (%s) ", self.curAccessSpecifier)
29742984

29752985
# Filter special case of array with casting in it
29762986
try:
@@ -2981,7 +2991,7 @@ def _evaluate_stack(self, token=None):
29812991
self.nameStack = (
29822992
self.nameStack[: bracePos + 1] + self.nameStack[endParen + 1 :]
29832993
)
2984-
debug_print("Filtered namestack to=%s" % self.nameStack)
2994+
debug_print("Filtered namestack to=%s", self.nameStack)
29852995
except:
29862996
pass
29872997

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy