#
# Logging assistance. This provides a logging facility for the rest of
# the Z-Machine. As a Z-Machine is inherently I/O intensive, dumb screen
# dumping is no longer adequate. This logging facility, based on
# python's logging module, provides file logging.
#

import logging

# Top-level initialization
logging.getLogger().setLevel(logging.DEBUG)

# Create the logging objects regardless.  If debugmode is False, then
# they won't actually do anything when used.
mainlog_handler = logging.FileHandler("debug.log", "a")
mainlog_handler.setLevel(logging.DEBUG)
mainlog_handler.setFormatter(logging.Formatter("%(asctime)s: %(message)s"))
logging.getLogger("mainlog").addHandler(mainlog_handler)

# We'll store the disassembly in a separate file, for better
# readability.
disasm_handler = logging.FileHandler("disasm.log", "a")
disasm_handler.setLevel(logging.DEBUG)
disasm_handler.setFormatter(logging.Formatter("%(message)s"))
logging.getLogger("disasm").addHandler(disasm_handler)

mainlog = logging.getLogger("mainlog")
mainlog.info("*** Log reopened ***")
disasm = logging.getLogger("disasm")
disasm.info("*** Log reopened ***")


# Pubilc routines used by other modules
def set_debug(state):
    if state:
        logging.getLogger().setLevel(logging.DEBUG)
    else:
        logging.getLogger().setLevel(logging.CRITICAL)


def log(msg):
    mainlog.debug(msg)


def log_disasm(pc, opcode_type, opcode_num, opcode_name, args):
    disasm.debug(f"{pc:06x}  {opcode_type}:{opcode_num:02x} {opcode_name} {args}")
