class Pocolog::DisplaySpec

Public Instance Methods

display_fields(v, ops) click to toggle source
# File bin/pocolog, line 233
def display_fields(v, ops)
    ops = ops.dup

    while !ops.empty?
        op = ops.shift

        if op == [:deference]
            result = []
            v.size.times do |i|
                result << display_fields(v.raw_get(i), ops)
            end
            return result

        elsif op[0] == :deference
            v = v.raw_get(op[1])
        else
            v = v.raw_get(op[1])
        end
    end

    if v.respond_to?(:to_csv)
        v.to_csv
    elsif v.respond_to?(:join)
        v.join(" ")
    else
        v.to_s
    end
end
eval_expr(string) click to toggle source
# File bin/pocolog, line 262
def eval_expr(string)
    Typelib.load_typelib_plugins
    samples.stream.reload_registry
    eval "self.expr = Proc.new { |sample| #{string} }"
end
execute() click to toggle source
# File bin/pocolog, line 268
def execute
    stream = samples.stream

    if !expr && fields
        field_operations = fields.map do |name|
            ops = []
            name.split('.').each do |subname|
                if match = /(.*)((?:\[[^\]]*\])+)$/.match(subname)
                    if !match[1].empty?
                        ops << [:raw_get, match[1]]
                    end
                    do_deference = match[2]
                    while !do_deference.empty?
                        match = /^\[([^\]]*)\]/.match(do_deference)
                        if match[1].empty?
                            ops << [:deference]
                        else
                            ops << [:deference, Integer(match[1])]
                        end
                        do_deference = match.post_match
                    end
                else
                    ops << [:raw_get, subname]
                end
            end
            [name, ops]
        end

        header_names = field_operations.map do |name, field_ops| 
            type = field_ops.inject(stream.type) do |type, op|
                if op[0] == :deference
                    type.deference
                else
                    if op.first == :raw_get
                        type[*op[1..-1]]
                    else
                        type.send(*op)
                    end
                end
            end
            subnames = type.to_csv
            name + subnames.gsub(/ \./, " #{name}.")
        end
        puts header_names.join(" ")

    elsif !expr
        header = stream.type.to_csv(stream.name)
        field_names = header.split(' ')
        if remove_prefix
            field_names.map! { |name| name.gsub!(remove_prefix, '') if name }
        end
        if filter || filter_out
            field_indexes = field_names.enum_with_index.map do |field, index|
                if filter_out && filter_out === field then nil
                elsif filter && !(filter === field) then nil
                else index
                end
            end.compact
            puts field_names.values_at(*field_indexes).join(" ")
        else
            puts field_names.join(' ')
        end
    end

    samples.raw_each do |rt, lg, data|
        if time
            if user_readable_time
                if samples.use_rt
                    print rt.strftime("%H:%M:%S.%6N ")
                else
                    print lg.strftime("%H:%M:%S.%6N ")
                end
            else
                if samples.use_rt
                    print "%i.%06i " % [rt.tv_sec, rt.tv_usec]
                else
                    print "%i.%06i " % [lg.tv_sec, lg.tv_usec]
                end
            end
        end

        if expr
            puts expr.call(Typelib.to_ruby(data))
        elsif fields
            values = field_operations.map do |_, field_ops|
                display_fields(data, field_ops)
            end
            puts values.join(" ")
        else
            data = display_fields(data, [])

            if field_indexes
                puts data.split(' ').values_at(*field_indexes).join(' ')
            else
                puts data
            end
        end
    end
end