class LogTools::Exporter

Attributes

end_index[RW]
exporter_name[RW]
filename[RW]
output_folder[RW]
start_index[RW]
stream_name[RW]
stream_type[RW]

Public Class Methods

new() click to toggle source
# File lib/log_tools/exporter.rb, line 95
def initialize
    @filename = "#STREAM_#INDEX.png"
end

Public Instance Methods

export(*logfiles) click to toggle source
# File lib/log_tools/exporter.rb, line 126
def export(*logfiles)
    #create folder if given
    if output_folder && !File.directory?(output_folder)
        Dir.mkdir(output_folder)
    end

    logfiles.flatten!
    logfiles.each do |logfile|
        Exporter.info "exporting #{logfile}"

        file = Pocolog::Logfiles.open(logfile)
        file.streams.each do |stream|
            if(stream_name && !stream_name.include?(stream.name))||(stream_type && !stream_type.include?(stream.type.name))
                #ignore all streams which are not listed if a filter is given
                Exporter.info "ignoring stream #{stream.name} (#{stream.size} samples)"
                next
            end

            #find exporter
            exporter_class = find_exporter(stream,filename,exporter_name)
            if !exporter_class
                Exporter.info "ignoring stream #{stream.name} (#{stream.size} samples): cannot find an exporter for this type"
                next
            end

            exporter = exporter_class.new(stream)
            index = 0
            time = Time.now
            Exporter.info " exporting stream #{stream.name} (#{stream.size} samples): "
            stream.samples.each do |rt,lg,sample|
                if start_index && index < start_index
                    index += 1
                    next
                end
                if (Time.now-time).to_f < 1
                    Exporter.debug "    #{stream.name}.sample #{index+1}/#{stream.size}"
                else
                    time = Time.now
                    Exporter.info "    #{stream.name}.sample #{index+1}/#{stream.size}"
                end

                temp_index = index.to_s
                temp_index = "0"*(stream.size.to_s.size-temp_index.size)+temp_index
                file = generate_filename(self.filename,stream.name,temp_index,rt)
                file = File.join(output_folder,filename) if output_folder && !output_folder.empty?
                file = File.expand_path(file)

                # Undo any custom convertions that typelib might have applied
                sample = Typelib.from_ruby(sample, stream.type)
                exporter.export(sample,file)
                index += 1
                if end_index && end_index > 0 && index > end_index
                    break
                end
            end
        end
    end
end
find_exporter(stream,filename,exporter_name=nil) click to toggle source
# File lib/log_tools/exporter.rb, line 99
def find_exporter(stream,filename,exporter_name=nil)
    suffix = if filename =~ /\.(.*)$/
                $1
             else
                 nil
             end
    ExporterBase.exporters.find do |exp|
        if exporter_name && exp.class.name == exporter_name
            exp
        elsif exp.handle_suffix?(suffix) && exp.handle_type_name?(stream.type.name)
            exp
        end
    end
end
generate_filename(pattern,stream_name,sample_index,rt) click to toggle source
# File lib/log_tools/exporter.rb, line 114
def generate_filename(pattern,stream_name,sample_index,rt)
    name = pattern.gsub("#STREAM",stream_name)
    name = name.gsub(/^\//,'')
    name = name.gsub('/','_')
    name = name.gsub("#INDEX",sample_index.to_s)
    name = if !!(name =~ /#TIME/)
               name.gsub("#TIME",Time.at(rt).strftime("%Y_%m_%d_%H_%M_%S_%L"))
           else
               name
           end
end
stream_name=(streams) click to toggle source
# File lib/log_tools/exporter.rb, line 87
def stream_name=(streams)
    @stream_name = Array(streams)
end
stream_type=(stream_types) click to toggle source
# File lib/log_tools/exporter.rb, line 91
def stream_type=(stream_types)
    @stream_type = Array(stream_types)
end