Use logging conditional formatting

This commit is contained in:
2019-12-22 06:50:14 -08:00
parent cb222cc6bb
commit 15220fffa6
2 changed files with 17 additions and 19 deletions

View File

@@ -74,7 +74,7 @@ def assemble_file(infile) -> bytes:
def assemble(lines: str) -> bytes:
instruction_map = build_instruction_map()
logger.debug("Instruction map: {}".format(instruction_map))
logger.debug("Instruction map: %s", instruction_map)
byte_offset = 0
instruction_count = 0
@@ -86,7 +86,7 @@ def assemble(lines: str) -> bytes:
return labels[label]
for step in ["SIZE", "CONTENT"]:
logger.debug("Starting step: {}".format(step))
logger.debug("Starting step: %s", step)
for line_num, line in enumerate(lines):
# Remove comments
@@ -94,8 +94,6 @@ def assemble(lines: str) -> bytes:
# Tokenize
tokens = line.split()
logging.info("Line: {}".format(line))
logging.info("Tokens: {}".format(tokens))
if len(tokens) == 0:
continue
@@ -108,15 +106,14 @@ def assemble(lines: str) -> bytes:
if instruction_name[-1] == LABEL_SUFFIX:
if step == 'SIZE':
label = instruction_name[:-1]
logger.debug("Found label '{}' at {}"
.format(label, byte_offset))
logger.debug("Found label '%s' at %s", label, byte_offset)
if label in labels.keys():
raise KeyError("Label '{}' defined at {} and {}"
.format(label, labels[label], line_num))
raise KeyError("Label '%s' defined at %s and %s",
label, labels[label], line_num)
labels[label] = byte_offset
continue
raise KeyError("Unknown instruction \"{}\" on line {}"
.format(instruction_name, line_num))
raise KeyError("Unknown instruction \"%s\" on line %s",
instruction_name, line_num)
if step == 'SIZE':
byte_offset += parse_line_size(instruction, args)
@@ -125,12 +122,11 @@ def assemble(lines: str) -> bytes:
try:
program += parse_line_bytes(instruction, args, label_resolver)
except ValueError:
raise ValueError("Failed to parse line {},\n{}"
.format(line_num, line))
raise ValueError("Failed to parse line %s,\n%s", line_num, line)
if step == 'SIZE':
logger.info("Program size: {} bytes, {} instructions"
.format(byte_offset, instruction_count))
logger.debug("Found labels: {}".format(labels))
logger.info("Program size: %s bytes, %s instructions",
byte_offset, instruction_count)
logger.debug("Found labels: %s", labels)
return program

View File

@@ -6,10 +6,12 @@ import yaml
import pytest
import logging
logger = logging.getLogger(__name__)
if __name__ == "__main__":
logging.basicConfig(format="")
logging.getLogger().setLevel(logging.INFO)
logger = logging.getLogger(__name__)
class AssembleCase(object):
@@ -35,7 +37,7 @@ def get_test_cases(subdir: str):
try:
case = AssembleCase(desc['name'], desc['program'], bytes(desc['expected']))
except TypeError:
logger.exception("Failed to parse yaml: {}".format(desc))
logger.exception("Failed to parse yaml: %s", desc)
cases.append(case)
return cases