Skip to content
Permalink
Browse files

prettify: properly define our own logger

Previously we partially re-used the `fprettify-logger` tag, which would
have led to problems of missing extra attributes if the corresponding
logger calls would have ever been called (luckily we don't do proper
unittesting) and for the cases of `fprettify-logger` tag re-use we
had to define a line number attribute we didn't have (so, set to 0).

Instead we now proudly and properly define our own logger tag.
  • Loading branch information
dev-zero committed Jan 8, 2020
1 parent 98c53f2 commit cf2ac326d85ff827c428a3723e964c44da7e5dcd
Showing with 184 additions and 196 deletions.
  1. +87 −87 tools/prettify/prettify.py
  2. +97 −109 tools/prettify/prettify_cp2k/normalizeFortranFile.py
@@ -173,12 +173,12 @@ def prettifyFile(
does not close the input file"""
max_pretty_iter = 5

logger = logging.getLogger("fprettify-logger")
logger = logging.getLogger("prettify-logger")

if is_fypp(infile):
logger.warning(
"fypp directives not fully supported, running only fprettify",
extra={"ffilename": filename, "fline": 0},
extra={"ffilename": filename},
)
replace = False
normalize_use = False
@@ -190,80 +190,73 @@ def prettifyFile(
hash_prev = md5(inbuf.getvalue().encode("utf8"))

for _ in range(max_pretty_iter):
try:
if replace:
outbuf = StringIO()
replacer.replaceWords(inbuf, outbuf)
if replace:
outbuf = StringIO()
replacer.replaceWords(inbuf, outbuf)
outbuf.seek(0)
inbuf.close()
inbuf = outbuf

if reformat: # reformat needs to be done first
outbuf = StringIO()
try:
reformat_ffile(
inbuf,
outbuf,
indent_size=indent,
whitespace=whitespace,
orig_filename=filename,
)
except fparse_utils.FprettifyParseException as e:
log_exception(
e, "fprettify could not parse file, file is not prettified"
)
outbuf.close()
inbuf.seek(0)
else:
outbuf.seek(0)
inbuf.close()
inbuf = outbuf

if reformat: # reformat needs to be done first
outbuf = StringIO()
try:
reformat_ffile(
inbuf,
outbuf,
indent_size=indent,
whitespace=whitespace,
orig_filename=filename,
)
except fparse_utils.FprettifyParseException as e:
log_exception(
e, "fprettify could not parse file, file is not prettified"
)
outbuf.close()
inbuf.seek(0)
else:
outbuf.seek(0)
inbuf.close()
inbuf = outbuf

normalize_use_succeeded = True

if normalize_use:
outbuf = StringIO()
try:
normalizeFortranFile.rewriteFortranFile(
inbuf,
outbuf,
indent,
decl_linelength,
decl_offset,
orig_filename=filename,
)
except normalizeFortranFile.InputStreamError as exc:
logger.error(
"normalizeFortranFile could not parse file, file is not normalized",
extra={"ffilename": filename, "fline": 0},
)
outbuf.close()
inbuf.seek(0)
normalize_use_succeeded = False
else:
outbuf.seek(0)
inbuf.close()
inbuf = outbuf

if upcase_keywords and normalize_use_succeeded:
outbuf = StringIO()
upcaseKeywords(inbuf, outbuf, upcase_omp)
normalize_use_succeeded = True

if normalize_use:
outbuf = StringIO()
try:
normalizeFortranFile.rewriteFortranFile(
inbuf,
outbuf,
indent,
decl_linelength,
decl_offset,
orig_filename=filename,
)
except normalizeFortranFile.InputStreamError as exc:
logger.exception(
"normalizeFortranFile could not parse file, file is not normalized",
extra={"ffilename": filename},
)
outbuf.close()
inbuf.seek(0)
normalize_use_succeeded = False
else:
outbuf.seek(0)
inbuf.close()
inbuf = outbuf

hash_new = md5(inbuf.getvalue().encode("utf8"))
if upcase_keywords and normalize_use_succeeded:
outbuf = StringIO()
upcaseKeywords(inbuf, outbuf, upcase_omp)
outbuf.seek(0)
inbuf.close()
inbuf = outbuf

if hash_prev.digest() == hash_new.digest():
return inbuf
hash_new = md5(inbuf.getvalue().encode("utf8"))

hash_prev = hash_new
if hash_prev.digest() == hash_new.digest():
return inbuf

except:
logger.critical(
"error processing file", extra={"ffilename": filename, "fline": 0}
)
raise
hash_prev = hash_new

else:
raise RuntimeError(
@@ -431,6 +424,34 @@ def main(argv):
if args.do_backup and not (args.stdout or args.files == ["stdin"]):
mkdir_p(args.backup_dir)

level = logging.CRITICAL

if args.report_errors:
if args.debug:
level = logging.DEBUG
else:
level = logging.INFO

# the fprettify logger provides filename and line number in case of errors
shandler = logging.StreamHandler()
shandler.setLevel(level)
shandler.setFormatter(
logging.Formatter("%(levelname)s %(ffilename)s:%(fline)s: %(message)s")
)

fprettify_logger = logging.getLogger("fprettify-logger")
fprettify_logger.setLevel(level)
fprettify_logger.addHandler(shandler)

# the prettify_cp2k loggers only provide the filename in their messages
shandler = logging.StreamHandler()
shandler.setLevel(level)
shandler.setFormatter(logging.Formatter("%(levelname)s %(ffilename)s: %(message)s"))

prettify_logger = logging.getLogger("prettify-logger")
prettify_logger.setLevel(level)
prettify_logger.addHandler(shandler)

failure = 0

for filename in args.files:
@@ -439,24 +460,6 @@ def main(argv):
failure += 1
continue

level = logging.CRITICAL

if args.report_errors:
if args.debug:
level = logging.DEBUG
else:
level = logging.INFO

logger = logging.getLogger("fprettify-logger")
logger.setLevel(level)
sh = logging.StreamHandler()
sh.setLevel(level)
formatter = logging.Formatter(
"%(levelname)s %(ffilename)s:%(fline)s: %(message)s"
)
sh.setFormatter(formatter)
logger.addHandler(sh)

try:
prettifyInplace(
filename,
@@ -473,10 +476,7 @@ def main(argv):
replace=args.replace,
)
except:
eprint("-" * 60)
traceback.print_exc(file=sys.stderr)
eprint("-" * 60)
eprint("Processing file '{}'".format(filename))
logger.exception("processing file failed", extra={"ffilename": filename})
failure += 1

return failure > 0

0 comments on commit cf2ac32

Please sign in to comment.
You can’t perform that action at this time.