<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def run(log_name, path, debug=False, mail=None, timeout=0):
 """ Run a subscriber and pass the messages to the logbook setup. Stays alive as long as the pubsub instance listen to something. :param log_name: the channel to listen to :param path: the path where the log files will be written :param debug: True if you want to save the debug messages too :param mail: Path to the config file for the mails """

    global pubsub
    global channel
    channel = log_name
    if use_tcp_socket:
        r = redis.StrictRedis(host=hostname, port=port)
    else:
        r = redis.StrictRedis(unix_socket_path=unix_socket)
    pubsub = r.pubsub()
    pubsub.psubscribe(channel + '.*')

    if timeout != 0:
        deadline = time.time() + timeout

    logger = Logger(channel)
    if mail is not None:
        mail_setup(mail)
    if os.path.exists(path) and not os.path.isdir(path):
        raise Exception("The path you want to use to save the file is invalid (not a directory).")
    if not os.path.exists(path):
        os.mkdir(path)
    with setup(channel, path, debug):
        while True:
            msg = pubsub.get_message()
            if msg:
                if msg['type'] == 'pmessage':
                    level = msg['channel'].decode('utf-8').split('.')[1]
                    message = msg['data']
                    try:
                        message = message.decode('utf-8')
                    except:
                        pass
                    logger.log(level, message)
            time.sleep(0.01)
            if timeout > 0:
                if time.time() >= deadline:
                    break