class MapTool::Log

Usage

Attributes

dir[RW]
match[RW]

Public Class Methods

new(argv) click to toggle source

初始化

Calls superclass method
# File lib/maptool/log/log.rb, line 33
def initialize(argv)
    super
    self.dir = argv.option('dir')
    self.match = argv.option('match')
end
options() click to toggle source
Calls superclass method MapTool::Command::options
# File lib/maptool/log/log.rb, line 24
def self.options
    [['--dir', '文件夹地址'],
    ['--match', '匹配的字符串']] + super
end

Public Instance Methods

run() click to toggle source
# File lib/maptool/log/log.rb, line 52
def run
    Dir.chdir(dir) {
        log = ""
        File.delete(LOG_FILE) if File.exist?(LOG_FILE)
        for path in Dir["**/*.log"] do
            puts "解析 #{path}"
            File.open(path, "r") { |file|
                while line = file.gets
                    if line.include?(match)
                        log << line
                    end
                end
            }
        end
        File.write(LOG_FILE, log)
        `open #{LOG_FILE}`
    }
end
validate!() click to toggle source

businrss

Calls superclass method
# File lib/maptool/log/log.rb, line 40
        def validate!
            super
            unless self.dir && Dir.exist?(dir)  {
                puts "dir:#{dir} 文件夹不存在".red
                self.banner!
            }
            unless self.match && self.match.length > 0 {
                puts "match 为空".red
                self.banner!
            }
        end
        
        def run
            Dir.chdir(dir) {
                log = ""
                File.delete(LOG_FILE) if File.exist?(LOG_FILE)
                for path in Dir["**/*.log"] do
                    puts "解析 #{path}"
                    File.open(path, "r") { |file|
                        while line = file.gets
                            if line.include?(match)
                                log << line
                            end
                        end
                    }
                end
                File.write(LOG_FILE, log)
                `open #{LOG_FILE}`
            }
        end
        
    end

end