
from org.transcrypt.stubs.browser import __pragma__, __envir__

import logging
from utils import TestHandler, resetLogging


def logger_basics(test):

    resetLogging()

    logger = logging.getLogger("tester")
    test.check(logger.name)
    test.check(logger.level)
    test.check(logger.hasHandlers())

    # level set methods
    test.check(logger.getEffectiveLevel())
    logger.setLevel(10)
    test.check(logger.level)
    testLevel = "USERDEFLEVEL"
    test.check(test.expectException(lambda : logger.setLevel(testLevel)))
    test.check(logging.getLevelName(testLevel))
    for i in range(0,50,5):
        test.check(logging.getLevelName(i))

    logging.addLevelName(35, testLevel)
    test.check(logging.getLevelName(testLevel))
    for i in range(0,50,5):
        test.check(logging.getLevelName(i))

    for i in range(0,50,5):
        test.check(logger.isEnabledFor(i))


    hdlr = TestHandler(test, 30)
    fmt = logging.Formatter(style="{")
    hdlr.setFormatter(fmt)
    logger.addHandler(hdlr)

    test.check(logger.hasHandlers())

    logger.debug("This is a debug message")
    logger.info("This is an info message")
    logger.warning("This is a warning message")
    logger.error("This is an error message")
    logger.critical("The house is on fire")

    logger.setLevel(0)

    # @note - Transcrypt only has the '.format()' method for
    #    string formatting but python's logging module has a
    #    fixed mode using the old-style % formating concept
    #    this is obviously non-optimal from a testing perspective
    #    as well as form a interop perspective...

    if __envir__.executor_name == __envir__.transpiler_name:
        logger.debug("This is a debug msg {}", 1)
    else:
        logger.debug("This is a debug msg %d", 1)

    if __envir__.executor_name == __envir__.transpiler_name:
        logger.info("This is an info message: {}", "blarg")
    else:
        logger.info("This is an info message: %s", "blarg")

    if __envir__.executor_name == __envir__.transpiler_name:
        logger.warning("This is a {} warning message in the {}", "blue", "barn")
    else:
        logger.warning("This is a %s warning message in the %s", "blue", "barn")

    if __envir__.executor_name == __envir__.transpiler_name:
        logger.error("This is an error message: {} {} {}", 3, "23", 4)
    else:
        logger.error("This is an error message: %d %s %d", 3, "23", 4)

    logger.critical("The house is on fire")

    # Test the handler level change
    hdlr.setLevel(30)
    logger.debug("This is a debug msg {}", 1)
    logger.info("This is an info message: {}", "blarg")