Skip to content

refactor: use lazy string evaluation for logs #372

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions fortls/langserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ def serve_initialize(self, request: dict):

# Initialize workspace
self.workspace_init()
log.info(f"fortls - Fortran Language Server {__version__} Initialized")
log.info("fortls - Fortran Language Server %s Initialized", __version__)
#
server_capabilities = {
"completionProvider": {
Expand Down Expand Up @@ -1391,7 +1391,7 @@ def update_workspace_file(
return False, "File does not exist" # Error during load
err_string, file_changed = file_obj.load_from_disk()
if err_string:
log.error(f"{err_string} : {filepath}")
log.error("%s : %s", err_string, filepath)
return False, err_string # Error during file read
if not file_changed:
return False, None
Expand Down
40 changes: 23 additions & 17 deletions fortls/parsers/internal/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -1677,11 +1677,11 @@ def parse(
message = f"Unexpected end of scope at line {error[0]}"
else:
message = "Unexpected end statement: No open scopes"
log.debug(f"{error[1]}: {message}")
log.debug("%s: %s", error[1], message)
if len(file_ast.parse_errors) > 0:
log.debug("\n=== Parsing Errors ===\n")
for error in file_ast.parse_errors:
log.debug(f"{error['range']}: {error['message']}")
log.debug("%s: %s", error["range"], error["message"])
return file_ast

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

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

Expand Down Expand Up @@ -2143,10 +2143,10 @@ def append_multiline_macro(def_value: str | tuple, line: str):
if if_start:
if is_path:
pp_stack.append([-1, -1])
log.debug(f"{line.strip()} !!! Conditional TRUE({i + 1})")
log.debug("%s !!! Conditional TRUE(%d)", line.strip(), i + 1)
else:
pp_stack.append([i + 1, -1])
log.debug(f"{line.strip()} !!! Conditional FALSE({i + 1})")
log.debug("%s !!! Conditional FALSE(%d)", line.strip(), i + 1)
continue
if len(pp_stack) == 0:
continue
Expand Down Expand Up @@ -2195,19 +2195,19 @@ def append_multiline_macro(def_value: str | tuple, line: str):
pp_stack_group.pop()
if pp_stack[-1][0] < 0:
pp_stack.pop()
log.debug(f"{line.strip()} !!! Conditional TRUE/END({i + 1})")
log.debug("%s !!! Conditional TRUE/END(%d)", line.strip(), i + 1)
continue
if pp_stack[-1][1] < 0:
pp_stack[-1][1] = i + 1
log.debug(f"{line.strip()} !!! Conditional FALSE/END({i + 1})")
log.debug("%s !!! Conditional FALSE/END(%d)", line.strip(), i + 1)
pp_skips.append(pp_stack.pop())
if debug:
if inc_start:
log.debug(f"{line.strip()} !!! Conditional TRUE({i + 1})")
log.debug("%s !!! Conditional TRUE(%d)", line.strip(), i + 1)
elif exc_start:
log.debug(f"{line.strip()} !!! Conditional FALSE({i + 1})")
log.debug("%s !!! Conditional FALSE(%d)", line.strip(), i + 1)
elif exc_continue:
log.debug(f"{line.strip()} !!! Conditional EXCLUDED({i + 1})")
log.debug("%s !!! Conditional EXCLUDED(%d)", line.strip(), i + 1)
continue
# Handle variable/macro definitions files
match = FRegex.PP_DEF.match(line)
Expand Down Expand Up @@ -2242,12 +2242,12 @@ def append_multiline_macro(def_value: str | tuple, line: str):
defs_tmp[def_name] = def_value
elif (match.group(1) == "undef") and (def_name in defs_tmp):
defs_tmp.pop(def_name, None)
log.debug(f"{line.strip()} !!! Define statement({i + 1})")
log.debug("%s !!! Define statement(%d)", line.strip(), i + 1)
continue
# Handle include files
match = FRegex.PP_INCLUDE.match(line)
if (match is not None) and ((len(pp_stack) == 0) or (pp_stack[-1][0] < 0)):
log.debug(f"{line.strip()} !!! Include statement({i + 1})")
log.debug("%s !!! Include statement(%d)", line.strip(), i + 1)
include_filename = match.group(1).replace('"', "")
include_path = None
# Intentionally keep this as a list and not a set. There are cases
Expand All @@ -2263,7 +2263,7 @@ def append_multiline_macro(def_value: str | tuple, line: str):
include_file = FortranFile(include_path)
err_string, _ = include_file.load_from_disk()
if err_string is None:
log.debug(f'\n!!! Parsing include file "{include_path}"')
log.debug("\n!!! Parsing include file '%s'", include_path)
_, _, _, defs_tmp = preprocess_file(
include_file.contents_split,
file_path=include_path,
Expand All @@ -2274,13 +2274,15 @@ def append_multiline_macro(def_value: str | tuple, line: str):
log.debug("!!! Completed parsing include file\n")

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

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

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

# Substitute (if any) read in preprocessor macros
for def_tmp, value in defs_tmp.items():
Expand All @@ -2302,7 +2304,11 @@ def append_multiline_macro(def_value: str | tuple, line: str):
line_new, nsubs = def_regex.subn(value, line)
if nsubs > 0:
log.debug(
f"{line.strip()} !!! Macro sub({i + 1}) '{def_tmp}' -> {value}"
"%s !!! Macro sub(%d) '%s' -> '%s'",
line.strip(),
i + 1,
def_tmp,
value,
)
line = line_new
output_file.append(line)
Expand Down