# Path: core/conf/logconfig.py
# LOG_FORMAT = '%(asctime)s %(name)s[%(process)d] %(levelname)s %(message)s'
# LOG_LEVELS = {
#     'info': logging.INFO,
#     'warning': logging.WARNING,
#     'error': logging.ERROR,
#     'debug': logging.DEBUG,
# }
# HIDDEN_ALL = ['warnings', 'stdout', 'stderr', 'running']
# HIDDEN_KEEP_STDERR = ['warnings', 'stdout', 'running']
# def set_logging(lvl='info'):


from os.path import exists, expanduser, join
from core.conf.logconfig import logger
    with open(cooja_file, 'w') as f:
        f.write('\n'.join(buffer))
    logger.debug(" > Cooja.java modified" if changed else " > Cooja.java already up-to-date")


def modify_rpl_debug(contiki_dir):
    """
    This function checks and modifies, if necessary, the DEBUG constant in
     [CONTIKI]/core/net/ipv6/uip-ds6-route.c in order to enable mote relationships logging.
     This is required for the script.js used in Cooja simulations to catch relationships
     evolution and to plot the sensor network in an animated GIF.

    :param contiki_dir: Contiki's directory
    :return: None
    """
    pattern = '#define DEBUG DEBUG_NONE'
    for module, descr in zip([('ipv6', 'uip-ds6-route.c'), ('rpl', 'rpl.c')], ["IPv6", "RPL"]):
        filepath = join(contiki_dir, 'core', 'net', *module)
        changed = False
        with open(filepath) as f:
            source = f.read()
        buffer = []
        for line in source.split('\n'):
            if pattern in line:
                buffer.append(line.replace(pattern, '#define DEBUG DEBUG_ANNOTATE'))
                changed = True
            else:
                buffer.append(line)
        with open(filepath, 'w') as f:
            f.write('\n'.join(buffer))

# Path: core/conf/logconfig.py
# LOG_FORMAT = '%(asctime)s %(name)s[%(process)d] %(levelname)s %(message)s'
# LOG_LEVELS = {
#     'info': logging.INFO,
#     'warning': logging.WARNING,
#     'error': logging.ERROR,
#     'debug': logging.DEBUG,
# }
# HIDDEN_ALL = ['warnings', 'stdout', 'stderr', 'running']
# HIDDEN_KEEP_STDERR = ['warnings', 'stdout', 'running']
# def set_logging(lvl='info'):


# Path: core/conf/install.py
from os.path import exists, expanduser, join
from core.conf.logconfig import logger

    with open(cooja_file, 'w') as f:
        f.write('\n'.join(buffer))
    logger.debug(" > Cooja.java modified" if changed else " > Cooja.java already up-to-date")


def modify_rpl_debug(contiki_dir):
    """
    This function checks and modifies, if necessary, the DEBUG constant in
     [CONTIKI]/core/net/ipv6/uip-ds6-route.c in order to enable mote relationships logging.
     This is required for the script.js used in Cooja simulations to catch relationships
     evolution and to plot the sensor network in an animated GIF.

    :param contiki_dir: Contiki's directory
    :return: None
    """
    pattern = '#define DEBUG DEBUG_NONE'
    for module, descr in zip([('ipv6', 'uip-ds6-route.c'), ('rpl', 'rpl.c')], ["IPv6", "RPL"]):
        filepath = join(contiki_dir, 'core', 'net', *module)
        changed = False
        with open(filepath) as f:
            source = f.read()
        buffer = []
        for line in source.split('\n'):
            if pattern in line:
                buffer.append(line.replace(pattern, '#define DEBUG DEBUG_ANNOTATE'))
                changed = True
            else:
                buffer.append(line)
        with open(filepath, 'w') as f:
            f.write('\n'.join(buffer))

        logger.debug((" > {} debug modified" if changed else " > {} debug already up-to-date").format(descr))