Ejemplo n.º 1
0
def create_base_logger(config=None, parallel=None):
    """Setup base logging configuration, also handling remote logging.

    Correctly sets up for local, multiprocessing and distributed runs.
    Creates subscribers for non-local runs that will be references from
    local logging.
    """
    if parallel is None: parallel = {}
    parallel_type = parallel.get("type", "local")
    cores = parallel.get("cores", 1)
    if parallel_type == "ipython":
        ips = [
            ip for ip in socket.gethostbyname_ex(socket.gethostname())[2]
            if not ip.startswith("127.0.0")
        ]
        if not ips:
            sys.stderr.write(
                "Cannot resolve a local IP address that isn't 127.0.0. "
                "Your machines might not have a local IP address "
                "assigned or are not able to resolve it.\n")
            sys.exit(1)
        uri = "tcp://%s" % ips[0]
        subscriber = logbook_zmqpush.ZeroMQPullSubscriber()
        mport = subscriber.socket.bind_to_random_port(uri)
        wport_uri = "%s:%s" % (uri, mport)
        parallel["log_queue"] = wport_uri
        subscriber.dispatch_in_background(_create_log_handler(config, True))
    elif cores > 1:
        subscriber = IOSafeMultiProcessingSubscriber(mpq)
        subscriber.dispatch_in_background(_create_log_handler(config))
    else:
        # Do not need to setup anything for local logging
        pass
    return parallel
Ejemplo n.º 2
0
def create_base_logger(config=None, parallel=None):
    """Setup base logging configuration, also handling remote logging.

    Correctly sets up for local, multiprocessing and distributed runs.
    Creates subscribers for non-local runs that will be references from
    local logging.

    Retrieves IP address using tips from http://stackoverflow.com/a/1267524/252589
    """
    if parallel is None: parallel = {}
    parallel_type = parallel.get("type", "local")
    cores = parallel.get("cores", 1)
    if parallel_type == "ipython":
        from bcbio.log import logbook_zmqpush
        fqdn_ip = socket.gethostbyname(socket.getfqdn())
        ips = [fqdn_ip] if (fqdn_ip and not fqdn_ip.startswith("127.")) else []
        if not ips:
            ips = [
                ip for ip in socket.gethostbyname_ex(socket.gethostname())[2]
                if not ip.startswith("127.")
            ]
        if not ips:
            ips += [
                (s.connect(('8.8.8.8', 53)), s.getsockname()[0], s.close())[1]
                for s in [socket.socket(socket.AF_INET, socket.SOCK_DGRAM)]
            ]
        if not ips:
            sys.stderr.write(
                "Cannot resolve a local IP address that isn't 127.x.x.x "
                "Your machines might not have a local IP address "
                "assigned or are not able to resolve it.\n")
            sys.exit(1)
        uri = "tcp://%s" % ips[0]
        subscriber = logbook_zmqpush.ZeroMQPullSubscriber()
        mport = subscriber.socket.bind_to_random_port(uri)
        wport_uri = "%s:%s" % (uri, mport)
        parallel["log_queue"] = wport_uri
        subscriber.dispatch_in_background(_create_log_handler(config, True))
    elif cores > 1:
        subscriber = IOSafeMultiProcessingSubscriber(mpq)
        subscriber.dispatch_in_background(_create_log_handler(config))
    else:
        # Do not need to setup anything for local logging
        pass
    return parallel