logm 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/logmerge.rb +84 -18
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b37bad66d2bc88ce8a74a9f3aeba6bb3c61798e9e9288f08c9c4a5c4c2c856f2
4
- data.tar.gz: b2fb66145e027580f43bbd527e41a52085b93014cc3719edca2e6ba5980c58f6
3
+ metadata.gz: e8268e85c780fcb505648e7f36ef915b0b82a253907148a2f95485a690bef294
4
+ data.tar.gz: 90042d98654e802e6c694de035297c120c2fbc74c65a0d69f95a330d3927a312
5
5
  SHA512:
6
- metadata.gz: 0af23c512635daaac90546647f6d8fca90d8d60cbb261ec849c1c64a1e0b558ec5ae8861c2df4ed97ff78e929d128b71f5d135434a2c39b6961025b5f300ecfb
7
- data.tar.gz: f1c446c3462c3cc8ee961ca8fec576e2f23a82c7d996c78323bb297d7c4da5724ac58b066a8a9bfaccbcd583e26c3577febfb8289d2758ee1f7d1099642d412b
6
+ metadata.gz: e6966a36051254dee8b55b0196250b4d682f8e136f8da76525dbab6a045953873915d541523827e74f8d6e6b7a4c0760f0758a4da94d1f2fa5ecedb953bfa340
7
+ data.tar.gz: 3d163efc23ee1868c288074ab553dbc2622fbc8b17b1401213eea97b6e7e77e4e9eddba2988de6274164931b8a2932f3698562fd118cc9f18fd143b49db3e439
@@ -2,6 +2,8 @@ require 'colored'
2
2
  require 'date'
3
3
  require 'optparse'
4
4
 
5
+
6
+
5
7
  class Numeric
6
8
  def minutes; self/1440.0 end
7
9
  alias :minute :minutes
@@ -13,8 +15,27 @@ class Numeric
13
15
  alias :millisecond :milliseconds
14
16
  end
15
17
 
18
+ class File
19
+ alias :old_initialize :initialize
20
+ alias :old_readline :readline
21
+ def initialize (*args)
22
+ @buf=[]
23
+ old_initialize(*args)
24
+ end
25
+ def unreadline (str)
26
+ @buf.push(str)
27
+ end
28
+ def readline (*args)
29
+ @buf.empty? ? old_readline(*args) : @buf.pop
30
+ end
31
+
32
+ def buf
33
+ @buf
34
+ end
35
+ end
36
+
16
37
  class Log
17
- attr_accessor :file, :line, :offet, :tag, :timestamp, :finished
38
+ attr_accessor :file, :line, :next, :offet, :tag, :timestamp, :next_timestamp, :finished
18
39
  def initialize(path)
19
40
  @offset = 0
20
41
  if path.include?(':')
@@ -30,34 +51,74 @@ class Log
30
51
  buffer
31
52
  end
32
53
 
33
- def parseTimestamp(line)
54
+ def parseTimestamp(current)
55
+ # puts "parsingTimestamp: #{current}"
34
56
  # would be great to generalize this more
35
- if line[0] == '['
57
+ ts = nil
58
+ if current =~ /^\[\d\d\d\d\/\d\d\/\d\d \d\d:\d\d:\d\d\.\d\d\d\]/
36
59
  # agent logs
37
- ts = DateTime.parse(line[1...24])
60
+ ts_part = current[1...24]
61
+ # puts "ts_part: #{ts_part}"
62
+ ts = DateTime.parse(ts_part)
38
63
  ts = ts + @offset.seconds
39
- line[1...24] = ts.to_s
40
- else
64
+ elsif current =~ /^\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d\.\d\d\d[+-]\d\d\d\d/
41
65
  #mms logs
42
- ts = DateTime.parse(line[0...28])
66
+ ts_part = current[0...28]
67
+ # puts "ts_part: #{ts_part}"
68
+ ts = DateTime.parse(current[0...28])
43
69
  ts = ts + @offset.seconds
44
- line[0...28] = "[#{ts.to_s}]"
45
70
  end
46
71
  ts
72
+ rescue Date::Error
73
+ nil
74
+ end
75
+
76
+ def applyTimestamp(current, timestamp)
77
+ if current =~ /^\[\d\d\d\d\/\d\d\/\d\d \d\d:\d\d:\d\d\.\d\d\d\]/
78
+ current[1...24] = timestamp.to_s
79
+ elsif current =~ /^\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d\.\d\d\d[+-]\d\d\d\d/
80
+ current[0...28] = "[#{timestamp.to_s}]"
81
+ end
82
+ current
83
+ end
84
+
85
+ def append(current, timestamp=nil)
86
+ current.prepend("[#{@tag}]") if @tag && timestamp
87
+ @line.push(current)
47
88
  end
48
89
 
49
90
  def buffer
50
- begin
51
- @line = file.readline.strip
52
- @timestamp = parseTimestamp(line)
53
- line.prepend("[#{@tag}]") if @tag
54
- rescue Date::Error
55
- @timestamp = nil
91
+ @count ||= 0
92
+ # puts "buffer: #{@count}"
93
+ # puts "file buffer: #{file.buf}"
94
+ @count = @count + 1
95
+ # TODO: add support for multiline log statements
96
+ # basically read until next timestamp found including all lines as an entry
97
+ @line = []
98
+ @timestamp = nil
99
+ timestamp_index = 0
100
+ while @timestamp.nil? do
101
+ current = file.readline.strip
102
+ # puts "current: #{current}"
103
+ @timestamp = parseTimestamp(current)
104
+ # puts "timestamp: #{@timestamp}"
105
+ append(current, @timestamp)
106
+ timestamp_index = timestamp_index + 1 if @timestamp.nil?
107
+ end
108
+
109
+ applyTimestamp(line[timestamp_index], @timestamp) if line.size > timestamp_index
110
+
111
+ next_timestamp = nil
112
+ while next_timestamp.nil? do
113
+ current = file.readline.strip
114
+ # puts "nextcurrent: #{current}"
115
+ next_timestamp = parseTimestamp(current)
116
+ # puts "next_timestamp: #{next_timestamp}"
117
+ file.unreadline(current + "\n") && break unless next_timestamp.nil?
118
+ append(current)
56
119
  end
57
120
  rescue EOFError
58
- @line = nil
59
- @finished = true
60
- @timestamp = nil
121
+ @finished = true if @line.empty?
61
122
  end
62
123
 
63
124
  def take
@@ -88,7 +149,12 @@ class LogMerge
88
149
  index = logs.index(min_log)
89
150
  color_index = index % colors.size
90
151
  color = colors[color_index]
91
- puts Colored.colorize(min_log.take, foreground: color)
152
+ loglines = min_log.take
153
+ puts applyColor(loglines, color).join("\n")
92
154
  end
93
155
  end
156
+
157
+ def applyColor(loglines, color)
158
+ loglines.map{|l| Colored.colorize(l, foreground: color)}
159
+ end
94
160
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: logm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Vertenten