예제 #1
0
    def get_width(field):
        if field.type in ('INT16', 'UINT16', 'TCP_PORT', 'UDP_PORT'):
            return 5
        if field.type in ('INT32', 'UINT32'):
            return 10
        if field.type in ('INT64', 'UINT64', 'RELATIVE_TIME'):
            return 20
        if field.type in ('FLOAT', 'DOUBLE'):
            return 20
        if field.type == 'BOOLEAN':
            return 5
        if field.type == 'IPv4':
            return 15
        if field.type in ('STRING', 'SHORT_STRING'):
            # TODO should this be smaller?
            return 64
        if field.type == 'ABSOLUTE_TIME':
            return max_width(timeformat)

        #XXX
        logging.warn('uh oh do not know width of %s' % field.type)
        return 10
예제 #2
0
def print_data(legend, stream, timeformat='%Y/%m/%d %H:%M:%S.%f',
               include_sample_times=True,
               widths=None, limit=None, line_prefix=''):
    """
    Print the data of a given view output to stdout.
    
    `widths` is an optional list of integers, specifying how
    many characters wide each column should be.  If it is not
    specified, reasonable defaults are chosen.

    If `limit` is specified, only the first `limit` rows are printed.

    `line_prefix` is a string that is printed at the start of every line.
    """

    labels = []
    if include_sample_times:
        labels.append('Time')
    labels += [f.name for f in legend]

    def get_width(field):
        if field.type in ('INT16', 'UINT16', 'TCP_PORT', 'UDP_PORT'):
            return 5
        if field.type in ('INT32', 'UINT32'):
            return 10
        if field.type in ('INT64', 'UINT64', 'RELATIVE_TIME'):
            return 20
        if field.type in ('FLOAT', 'DOUBLE'):
            return 20
        if field.type == 'BOOLEAN':
            return 5
        if field.type == 'IPv4':
            return 15
        if field.type in ('STRING', 'SHORT_STRING'):
            # TODO should this be smaller?
            return 64
        if field.type == 'ABSOLUTE_TIME':
            return max_width(timeformat)

        #XXX
        logging.warn('uh oh do not know width of %s' % field.type)
        return 10

    if widths is None:
        widths = []
        if include_sample_times:
            widths.append(max_width(timeformat))

        widths += [get_width(field) for field in legend]

    justified = [label.ljust(widths[i]) for i, label in enumerate(labels)]
    print line_prefix + '  '.join(justified)

    total = 0
    for s in stream:
        if 'gap_start' in s or 'gap_end' in s:
            continue

        base = [s.t.strftime(timeformat).ljust(widths[0])]
        for vec in s.vals:
            fields = base + [str(v).ljust(widths[i + 1]) for i, v in enumerate(vec)]

            print line_prefix + '  '.join(fields)

        total += 1
        if total == limit:
            return