forked from questdb/questdb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfind_unterminated_logs.py
More file actions
448 lines (362 loc) · 12.4 KB
/
find_unterminated_logs.py
File metadata and controls
448 lines (362 loc) · 12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
#!/usr/bin/env python3
"""
Find QuestDB log statements not properly terminated with .$() or .I$().
QuestDB's async logger requires every log chain to be flushed by calling
.$() or .I$() at the end:
LOG.info().$("message").$(); // OK
LOG.info().$("key=").$(value).I$(); // OK (.I$() appends ']' then flushes)
LOG.info().$("message"); // BAD - never flushed!
Log statements may span multiple lines:
LOG.info().$("retrying plan [q=`").$(queryModel)
.$("`, fd=").$(executionContext.getRequestFd())
.I$(); // OK
Usage:
python3 find_unterminated_logs.py [directory] [--no-tests]
directory -- root to scan (default: current directory)
--no-tests -- skip test source trees
"""
import os
import re
import sys
# All log-level methods on the Log interface that return a LogRecord.
LOG_LEVELS = (
"advisory", "advisoryW",
"critical",
"debug", "debugW",
"error", "errorW",
"info", "infoW",
"xDebugW", "xInfoW",
"xadvisory", "xcritical", "xdebug", "xerror", "xinfo",
)
_levels_alt = "|".join(LOG_LEVELS)
# Matches any call to a log-level method: .info() .errorW() .advisory() etc.
# This catches LOG.info(), log.info(), bootstrap.getLog().info(), and similar.
LOG_START_RE = re.compile(
rf"\.(?:{_levels_alt})\(\)"
)
# Detects LogRecord variable captures (these terminate separately).
LOG_RECORD_CAPTURE_RE = re.compile(
r"(?:LogRecord|final\s+LogRecord|var)\s+\w+\s*="
)
def _is_comment_line(stripped):
"""Return True if the stripped line is purely a comment."""
return (
stripped.startswith("//")
or stripped.startswith("*")
or stripped.startswith("/*")
)
def _find_semicolon(lines, start_line, start_col):
"""
Starting from (start_line, start_col), find the first `;` that is
outside of string/char literals, comments, and nested brace blocks
(lambdas, anonymous classes).
Returns (line_index, col_index) or (None, None).
"""
in_string = False
in_char = False
in_block_comment = False
in_text_block = False
escaped = False
brace_depth = 0
# Don't search forever — 60 lines should be more than enough for any
# log statement.
end = min(start_line + 60, len(lines))
for i in range(start_line, end):
line = lines[i]
j = start_col if i == start_line else 0
while j < len(line):
ch = line[j]
# ---- escaped character inside literal ----
if escaped:
escaped = False
j += 1
continue
if (in_string or in_char) and ch == "\\":
escaped = True
j += 1
continue
# ---- block comment ----
if in_block_comment:
if ch == "*" and j + 1 < len(line) and line[j + 1] == "/":
in_block_comment = False
j += 2
continue
j += 1
continue
# ---- text block (triple-quoted string) ----
if in_text_block:
if ch == '"' and j + 2 < len(line) and line[j + 1] == '"' and line[j + 2] == '"':
in_text_block = False
j += 3
continue
j += 1
continue
# ---- inside ordinary string ----
if in_string:
if ch == '"':
in_string = False
j += 1
continue
# ---- inside char literal ----
if in_char:
if ch == "'":
in_char = False
j += 1
continue
# ---- detect starts of literals / comments ----
if ch == "/" and j + 1 < len(line):
nxt = line[j + 1]
if nxt == "/":
break # rest of line is a line-comment
if nxt == "*":
in_block_comment = True
j += 2
continue
if ch == '"':
if j + 2 < len(line) and line[j + 1] == '"' and line[j + 2] == '"':
in_text_block = True
j += 3
continue
in_string = True
j += 1
continue
if ch == "'":
in_char = True
j += 1
continue
# ---- track braces (lambdas, anonymous classes) ----
if ch == "{":
brace_depth += 1
j += 1
continue
if ch == "}":
brace_depth -= 1
j += 1
continue
# ---- the semicolon we are looking for ----
if ch == ";" and brace_depth == 0:
return i, j
j += 1
return None, None
def _extract(lines, sl, sc, el, ec):
"""Extract text from (sl, sc) to (el, ec) inclusive."""
if sl == el:
return lines[sl][sc : ec + 1]
parts = [lines[sl][sc:]]
for i in range(sl + 1, el):
parts.append(lines[i])
parts.append(lines[el][: ec + 1])
return "".join(parts)
def _has_proper_terminator(statement):
"""
Return True if *statement* (including the trailing `;`) is properly
terminated with .$() or .I$().
Handles the case where `.` and `$()` are separated by whitespace
across lines, e.g.:
.$(']').
$();
"""
s = statement.rstrip()
if s.endswith(";"):
s = s[:-1].rstrip()
# Collapse whitespace so `. \\n $()` becomes `.$()`.
collapsed = re.sub(r"\s+", "", s)
return collapsed.endswith(".$()") or collapsed.endswith(".I$()")
def _prefix_is_in_comment_or_string(line, match_start):
"""
Quick heuristic: return True if the LOG match is likely inside a
string literal or trailing comment.
"""
prefix = line[:match_start]
# Inside a string? (odd number of unescaped quotes before the match)
if prefix.count('"') % 2 == 1:
return True
# After a line comment?
# Find the last // that is not inside a string.
idx = 0
in_str = False
while idx < len(prefix) - 1:
if prefix[idx] == '"':
in_str = not in_str
elif not in_str and prefix[idx] == "/" and prefix[idx + 1] == "/":
return True
idx += 1
return False
def _is_logrecord_assignment(line, match_start):
"""
Return True if the log-level call is being assigned to a variable,
either in a declaration (`LogRecord rec = LOG.info()`) or a plain
assignment (`rec = LOG.info()`).
"""
prefix = line[:match_start].rstrip()
# Look for `=` (but not ==, !=, <=, >=) anywhere in the prefix.
if re.search(r"(?<![=!<>])=(?!=)", prefix):
return True
return False
def _is_return_statement(line, match_start):
"""Return True if the log-level call appears in a return statement."""
prefix = line[:match_start].strip()
return prefix.startswith("return ") or prefix == "return"
def _is_nested_in_call(stmt):
"""
Return True if the extracted statement has more closing parens than
opening parens, meaning the LOG.level() is nested inside a larger
expression (e.g. a method argument).
"""
depth = 0
in_string = False
in_char = False
escaped = False
in_text_block = False
i = 0
while i < len(stmt):
ch = stmt[i]
if escaped:
escaped = False
i += 1
continue
if (in_string or in_char) and ch == "\\":
escaped = True
i += 1
continue
if in_text_block:
if ch == '"' and i + 2 < len(stmt) and stmt[i + 1] == '"' and stmt[i + 2] == '"':
in_text_block = False
i += 3
continue
i += 1
continue
if in_string:
if ch == '"':
in_string = False
i += 1
continue
if in_char:
if ch == "'":
in_char = False
i += 1
continue
if ch == '"':
if i + 2 < len(stmt) and stmt[i + 1] == '"' and stmt[i + 2] == '"':
in_text_block = True
i += 3
continue
in_string = True
i += 1
continue
if ch == "'":
in_char = True
i += 1
continue
if ch == "(":
depth += 1
elif ch == ")":
depth -= 1
if depth < 0:
return True
i += 1
return False
def scan_file(filepath):
"""Return a list of issue dicts for unterminated log statements."""
try:
with open(filepath, "r", encoding="utf-8", errors="replace") as f:
lines = f.readlines()
except Exception:
return []
issues = []
i = 0
while i < len(lines):
line = lines[i]
stripped = line.strip()
if _is_comment_line(stripped):
i += 1
continue
m = LOG_START_RE.search(line)
if not m:
i += 1
continue
if _prefix_is_in_comment_or_string(line, m.start()):
i += 1
continue
# Skip LogRecord variable declarations on this line.
if LOG_RECORD_CAPTURE_RE.search(line):
end_line, _ = _find_semicolon(lines, i, m.start())
i = (end_line + 1) if end_line is not None else (i + 1)
continue
# Skip assignments to already-declared variables: `rec = LOG.info()`.
if _is_logrecord_assignment(line, m.start()):
end_line, _ = _find_semicolon(lines, i, m.start())
i = (end_line + 1) if end_line is not None else (i + 1)
continue
# Skip return statements (methods that return a LogRecord).
if _is_return_statement(line, m.start()):
end_line, _ = _find_semicolon(lines, i, m.start())
i = (end_line + 1) if end_line is not None else (i + 1)
continue
end_line, end_col = _find_semicolon(lines, i, m.start())
if end_line is None:
issues.append({
"file": filepath,
"line": i + 1,
"end_line": i + 1,
"statement": stripped,
})
i += 1
continue
stmt = _extract(lines, i, m.start(), end_line, end_col)
# Skip isEnabled() guard checks — not actual log output.
if ".isEnabled()" in stmt:
i = end_line + 1
continue
# Skip if the LOG.level() is nested inside a method call
# (e.g. passed as argument to another function).
if _is_nested_in_call(stmt):
i = end_line + 1
continue
if not _has_proper_terminator(stmt):
issues.append({
"file": filepath,
"line": i + 1,
"end_line": end_line + 1,
"statement": stmt.strip(),
})
i = end_line + 1
return issues
def main():
search_dir = sys.argv[1] if len(sys.argv) > 1 else "."
exclude_tests = "--no-tests" in sys.argv
# Collect --exclude=<pattern> arguments.
excludes = []
for arg in sys.argv:
if arg.startswith("--exclude="):
excludes.append(arg[len("--exclude="):])
all_issues = []
for root, dirs, files in os.walk(search_dir):
if exclude_tests and ("/test/" in root or "/test-" in root):
continue
for fname in sorted(files):
if not fname.endswith(".java"):
continue
filepath = os.path.join(root, fname)
if any(ex in filepath for ex in excludes):
continue
issues = scan_file(filepath)
all_issues.extend(issues)
if not all_issues:
print("No unterminated log statements found.")
return
print(f"Found {len(all_issues)} unterminated log statement(s):\n")
for issue in all_issues:
rel = os.path.relpath(issue["file"], search_dir)
loc = f"{rel}:{issue['line']}"
if issue["end_line"] != issue["line"]:
loc += f"-{issue['end_line']}"
print(loc)
stmt = issue["statement"].replace("\n", "\n ")
if len(stmt) > 300:
stmt = stmt[:300] + " ..."
print(f" {stmt}")
print()
sys.exit(1)
if __name__ == "__main__":
main()