Skip to content

Commit 9a1b42b

Browse files
committed
refactor: use lazy string evaluation for logs
1 parent e1b2400 commit 9a1b42b

File tree

2 files changed

+25
-19
lines changed

2 files changed

+25
-19
lines changed

fortls/langserver.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ def serve_initialize(self, request: dict):
210210

211211
# Initialize workspace
212212
self.workspace_init()
213-
log.info(f"fortls - Fortran Language Server {__version__} Initialized")
213+
log.info("fortls - Fortran Language Server %s Initialized", __version__)
214214
#
215215
server_capabilities = {
216216
"completionProvider": {
@@ -1391,7 +1391,7 @@ def update_workspace_file(
13911391
return False, "File does not exist" # Error during load
13921392
err_string, file_changed = file_obj.load_from_disk()
13931393
if err_string:
1394-
log.error(f"{err_string} : {filepath}")
1394+
log.error("%s : %s", err_string, filepath)
13951395
return False, err_string # Error during file read
13961396
if not file_changed:
13971397
return False, None

fortls/parsers/internal/parser.py

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1677,11 +1677,11 @@ def parse(
16771677
message = f"Unexpected end of scope at line {error[0]}"
16781678
else:
16791679
message = "Unexpected end statement: No open scopes"
1680-
log.debug(f"{error[1]}: {message}")
1680+
log.debug("%s: %s", error[1], message)
16811681
if len(file_ast.parse_errors) > 0:
16821682
log.debug("\n=== Parsing Errors ===\n")
16831683
for error in file_ast.parse_errors:
1684-
log.debug(f"{error['range']}: {error['message']}")
1684+
log.debug("%s: %s", error["range"], error["message"])
16851685
return file_ast
16861686

16871687
def parse_imp_dim(self, line: str):
@@ -1919,7 +1919,7 @@ def add_line_comment(file_ast: FortranAST, docs: list[str]):
19191919
# Handle dangling comments from previous line
19201920
if docs:
19211921
file_ast.add_doc(format(docs))
1922-
log.debug(f"{format(docs)} !!! Doc string - Line:{ln}")
1922+
log.debug("%s !!! Doc string - Line:%d", format(docs), ln)
19231923
docs[:] = [] # empty the documentation stack
19241924

19251925
# Check for comments in line
@@ -1940,7 +1940,7 @@ def add_line_comment(file_ast: FortranAST, docs: list[str]):
19401940
if len("".join(docs)) > 0:
19411941
file_ast.add_doc(format(docs), forward=predocmark)
19421942
for i, doc_line in enumerate(docs):
1943-
log.debug(f"{doc_line} !!! Doc string - Line:{_ln + i}")
1943+
log.debug("%s !!! Doc string - Line:%d", doc_line, _ln + i)
19441944
docs[:] = []
19451945
return ln
19461946

@@ -2143,10 +2143,10 @@ def append_multiline_macro(def_value: str | tuple, line: str):
21432143
if if_start:
21442144
if is_path:
21452145
pp_stack.append([-1, -1])
2146-
log.debug(f"{line.strip()} !!! Conditional TRUE({i + 1})")
2146+
log.debug("%s !!! Conditional TRUE(%d)", line.strip(), i + 1)
21472147
else:
21482148
pp_stack.append([i + 1, -1])
2149-
log.debug(f"{line.strip()} !!! Conditional FALSE({i + 1})")
2149+
log.debug("%s !!! Conditional FALSE(%d)", line.strip(), i + 1)
21502150
continue
21512151
if len(pp_stack) == 0:
21522152
continue
@@ -2195,19 +2195,19 @@ def append_multiline_macro(def_value: str | tuple, line: str):
21952195
pp_stack_group.pop()
21962196
if pp_stack[-1][0] < 0:
21972197
pp_stack.pop()
2198-
log.debug(f"{line.strip()} !!! Conditional TRUE/END({i + 1})")
2198+
log.debug("%s !!! Conditional TRUE/END(%d)", line.strip(), i + 1)
21992199
continue
22002200
if pp_stack[-1][1] < 0:
22012201
pp_stack[-1][1] = i + 1
2202-
log.debug(f"{line.strip()} !!! Conditional FALSE/END({i + 1})")
2202+
log.debug("%s !!! Conditional FALSE/END(%d)", line.strip(), i + 1)
22032203
pp_skips.append(pp_stack.pop())
22042204
if debug:
22052205
if inc_start:
2206-
log.debug(f"{line.strip()} !!! Conditional TRUE({i + 1})")
2206+
log.debug("%s !!! Conditional TRUE(%d)", line.strip(), i + 1)
22072207
elif exc_start:
2208-
log.debug(f"{line.strip()} !!! Conditional FALSE({i + 1})")
2208+
log.debug("%s !!! Conditional FALSE(%d)", line.strip(), i + 1)
22092209
elif exc_continue:
2210-
log.debug(f"{line.strip()} !!! Conditional EXCLUDED({i + 1})")
2210+
log.debug("%s !!! Conditional EXCLUDED(%d)", line.strip(), i + 1)
22112211
continue
22122212
# Handle variable/macro definitions files
22132213
match = FRegex.PP_DEF.match(line)
@@ -2242,12 +2242,12 @@ def append_multiline_macro(def_value: str | tuple, line: str):
22422242
defs_tmp[def_name] = def_value
22432243
elif (match.group(1) == "undef") and (def_name in defs_tmp):
22442244
defs_tmp.pop(def_name, None)
2245-
log.debug(f"{line.strip()} !!! Define statement({i + 1})")
2245+
log.debug("%s !!! Define statement(%d)", line.strip(), i + 1)
22462246
continue
22472247
# Handle include files
22482248
match = FRegex.PP_INCLUDE.match(line)
22492249
if (match is not None) and ((len(pp_stack) == 0) or (pp_stack[-1][0] < 0)):
2250-
log.debug(f"{line.strip()} !!! Include statement({i + 1})")
2250+
log.debug("%s !!! Include statement(%d)", line.strip(), i + 1)
22512251
include_filename = match.group(1).replace('"', "")
22522252
include_path = None
22532253
# Intentionally keep this as a list and not a set. There are cases
@@ -2263,7 +2263,7 @@ def append_multiline_macro(def_value: str | tuple, line: str):
22632263
include_file = FortranFile(include_path)
22642264
err_string, _ = include_file.load_from_disk()
22652265
if err_string is None:
2266-
log.debug(f'\n!!! Parsing include file "{include_path}"')
2266+
log.debug("\n!!! Parsing include file '%s'", include_path)
22672267
_, _, _, defs_tmp = preprocess_file(
22682268
include_file.contents_split,
22692269
file_path=include_path,
@@ -2274,13 +2274,15 @@ def append_multiline_macro(def_value: str | tuple, line: str):
22742274
log.debug("!!! Completed parsing include file\n")
22752275

22762276
else:
2277-
log.debug(f"!!! Failed to parse include file: {err_string}")
2277+
log.debug("!!! Failed to parse include file: %s", err_string)
22782278

22792279
except:
22802280
log.debug("!!! Failed to parse include file: exception")
22812281

22822282
else:
2283-
log.debug(f"{line.strip()} !!! Could not locate include file ({i + 1})")
2283+
log.debug(
2284+
"%s !!! Could not locate include file (%d)", line.strip(), i + 1
2285+
)
22842286

22852287
# Substitute (if any) read in preprocessor macros
22862288
for def_tmp, value in defs_tmp.items():
@@ -2302,7 +2304,11 @@ def append_multiline_macro(def_value: str | tuple, line: str):
23022304
line_new, nsubs = def_regex.subn(value, line)
23032305
if nsubs > 0:
23042306
log.debug(
2305-
f"{line.strip()} !!! Macro sub({i + 1}) '{def_tmp}' -> {value}"
2307+
"%s !!! Macro sub(%d) '%s' -> '%s'",
2308+
line.strip(),
2309+
i + 1,
2310+
def_tmp,
2311+
value,
23062312
)
23072313
line = line_new
23082314
output_file.append(line)

0 commit comments

Comments
 (0)