Exemplo n.º 1
0
def display_log():
    args = _parse_args()
    if args['help']:
        _display_help()
        return
    _display_header()
    log = TrimmingLog('msglog')
    start = 0
    try:
        if args['tail_lines'] is not None or args['follow']:
            tail_lines = 10
            if args['tail_lines'] != None:
                tail_lines = int(args['tail_lines'])
            assert tail_lines >= -1, (
                "tail_lines must be greater than or eaual to -1")
            if tail_lines == -1:
                start = int(log.get_first_record()['_seq'])
            else:
                start = int(log.get_last_record()['_seq']) - tail_lines + 1
        else:
            start = int(log.get_first_record()['_seq'])
    except AssertionError:
        raise # Reraise the original exception.
    except:
        pass
    end = args['head_lines']
    assert end is None or end >= 0, (
        "head_lines must be None or it must be greater than or eaual to 0")
    entries = ()
    try:
        if end is not None:
            end += start
            entries = log[start:end]
        else:
            end = start
            entries = log[start:]
            end += len(entries)
    except ENoData:
        entries = []
        pass
    for entry in entries:
        _display_message(entry, args['color'], args['match'])
    if args['follow']:
        # If the file doesn't exist, there doesn't seem to be an obvious
        # way to gracefully recover at this point.  Just inform the user
        # that the file doesn't exist, and bail.  While we are at it,
        # we can make sure that the file does exist so that the next
        # time we are run, we have half a chance.
        if not os.path.exists(log.filename):
            print 'Error: %s does not exist.  Please restart.' % log.filename
            try:
                fd = open(log.filename, 'a+')
                fd.close()
            except:
                pass
            return
        _initialize_stat(log.filename)
        restart = end
        while 1:
            try:
                entries = log[restart:]
                restart += len(entries)
                for entry in entries:
                    _display_message(entry, args['color'], args['match'])
            except ENoData:
                pass
            # Sleep until either the file has changed, or
            # a certain maximum amount of time has elasped.
            _sleep_until_file_changes_with_timeout(log.filename,
                                                   max_wait_time)