Class: Log
- Inherits:
-
Object
- Object
- Log
- Defined in:
- lib/logmerge.rb
Instance Attribute Summary collapse
-
#file ⇒ Object
Returns the value of attribute file.
-
#finished ⇒ Object
Returns the value of attribute finished.
-
#line ⇒ Object
Returns the value of attribute line.
-
#next ⇒ Object
Returns the value of attribute next.
-
#next_timestamp ⇒ Object
Returns the value of attribute next_timestamp.
-
#offet ⇒ Object
Returns the value of attribute offet.
-
#tag ⇒ Object
Returns the value of attribute tag.
-
#timestamp ⇒ Object
Returns the value of attribute timestamp.
Instance Method Summary collapse
- #applyTimestamp(current, timestamp) ⇒ Object
- #buffer ⇒ Object
-
#initialize(path, autotag = false) ⇒ Log
constructor
A new instance of Log.
- #parseTimestamp(current) ⇒ Object
- #take ⇒ Object
Constructor Details
#initialize(path, autotag = false) ⇒ Log
Returns a new instance of Log.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/logmerge.rb', line 39 def initialize(path, autotag=false) @offset = 0 if path.include?(':') parts = path.split(':') path = parts[0] @offset = parts[1].to_i if parts.size > 2 @tag = parts[2] end end if @tag.nil? && autotag @tag = File.basename(path) end @file = File.open(path) @finished = false buffer end |
Instance Attribute Details
#file ⇒ Object
Returns the value of attribute file.
38 39 40 |
# File 'lib/logmerge.rb', line 38 def file @file end |
#finished ⇒ Object
Returns the value of attribute finished.
38 39 40 |
# File 'lib/logmerge.rb', line 38 def finished @finished end |
#line ⇒ Object
Returns the value of attribute line.
38 39 40 |
# File 'lib/logmerge.rb', line 38 def line @line end |
#next ⇒ Object
Returns the value of attribute next.
38 39 40 |
# File 'lib/logmerge.rb', line 38 def next @next end |
#next_timestamp ⇒ Object
Returns the value of attribute next_timestamp.
38 39 40 |
# File 'lib/logmerge.rb', line 38 def @next_timestamp end |
#offet ⇒ Object
Returns the value of attribute offet.
38 39 40 |
# File 'lib/logmerge.rb', line 38 def offet @offet end |
#tag ⇒ Object
Returns the value of attribute tag.
38 39 40 |
# File 'lib/logmerge.rb', line 38 def tag @tag end |
#timestamp ⇒ Object
Returns the value of attribute timestamp.
38 39 40 |
# File 'lib/logmerge.rb', line 38 def @timestamp end |
Instance Method Details
#applyTimestamp(current, timestamp) ⇒ Object
79 80 81 82 83 84 85 86 87 |
# File 'lib/logmerge.rb', line 79 def applyTimestamp(current, ) if current =~ /^\[\d\d\d\d\/\d\d\/\d\d \d\d:\d\d:\d\d\.\d\d\d\]/ current[1...24] = .to_s elsif current =~ /^\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d\.\d\d\d[+-]\d\d\d\d/ current[0...28] = "[#{.to_s}]" end current.prepend("[#{@tag}]") if @tag && current end |
#buffer ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
# File 'lib/logmerge.rb', line 89 def buffer @count ||= 0 # puts "buffer: #{@count}" # puts "file buffer: #{file.buf}" @count = @count + 1 # TODO: add support for multiline log statements # basically read until next timestamp found including all lines as an entry @line = [] @timestamp = nil = 0 while @timestamp.nil? do current = file.readline.strip # puts "current: #{current}" @timestamp = parseTimestamp(current) # puts "timestamp: #{@timestamp}" @line.push(current) = + 1 if @timestamp.nil? end applyTimestamp(line[], @timestamp) if line.size > = nil while .nil? do current = file.readline.strip # puts "nextcurrent: #{current}" = parseTimestamp(current) # puts "next_timestamp: #{next_timestamp}" file.unreadline(current + "\n") && break unless .nil? @line.push(current) end rescue EOFError @finished = true if @line.empty? end |
#parseTimestamp(current) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/logmerge.rb', line 57 def parseTimestamp(current) # puts "parsingTimestamp: #{current}" # would be great to generalize this more ts = nil if current =~ /^\[\d\d\d\d\/\d\d\/\d\d \d\d:\d\d:\d\d\.\d\d\d\]/ # agent logs ts_part = current[1...24] # puts "ts_part: #{ts_part}" ts = DateTime.parse(ts_part) ts = ts + @offset.seconds elsif current =~ /^\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d\.\d\d\d[+-]\d\d\d\d/ #mms logs ts_part = current[0...28] # puts "ts_part: #{ts_part}" ts = DateTime.parse(current[0...28]) ts = ts + @offset.seconds end ts rescue Date::Error nil end |
#take ⇒ Object
123 124 125 126 127 |
# File 'lib/logmerge.rb', line 123 def take ret = line buffer ret end |