logpos 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/README.txt +1 -1
  2. data/lib/logpos.rb +15 -13
  3. metadata +26 -11
data/README.txt CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  == DESCRIPTION:
6
6
 
7
- Use binary search to seek a position in logs, see http://mvj3.github.com/2011/09/17/use-binary-search-to-seek-a-position-in-rails-logs
7
+ Use binary search to seek a position in logs.
8
8
 
9
9
  == FEATURES/PROBLEMS:
10
10
 
@@ -1,5 +1,5 @@
1
1
  class Logpos
2
- VERSION = '1.0.1'
2
+ VERSION = '1.0.2'
3
3
 
4
4
  begin; require 'chronic'; rescue LoadError; end
5
5
  TIME_PARSER_CLASS = if defined? Chronic
@@ -15,21 +15,22 @@ class Logpos
15
15
  @time_parser = proc {|line| line.match(/^Started/) && TIME_PARSER_CLASS.parse(line.split(/for [0-9\.]* at /)[-1]) }
16
16
  end
17
17
 
18
- def self.seek_pos_before log_path, lastest_visited_at
19
- Logpos.new.seek_pos_before log_path, lastest_visited_at
18
+ def self.seek_pos_before file_path, lastest_visited_at
19
+ Logpos.new.seek_pos_before file_path, lastest_visited_at
20
20
  end
21
21
 
22
- def seek_pos_before log_path, lastest_visited_at
22
+ def seek_pos_before file_path, lastest_visited_at
23
23
  ta = TripleArray.new lastest_visited_at
24
24
 
25
- log = File.open(log_path)
26
- pos_start, pos_end = 0, File.size(log)
25
+ @file ||= File.open(file_path)
26
+ @file.reopen(file_path)
27
+ pos_start, pos_end = 0, File.size(@file)
27
28
  pos_mid = pos_end / 2
28
29
  time = Time.at(0)
29
30
  between = -1.0/0
30
31
 
31
32
  loop do
32
- pos_mid, time = seek_log_pos(log, pos_mid)
33
+ pos_mid, time = seek_log_pos(@file, pos_mid)
33
34
  break if time.nil? || (pos_mid == pos_end)
34
35
 
35
36
  time_valid = lastest_visited_at >= time
@@ -45,20 +46,21 @@ class Logpos
45
46
  pos_mid = (pos_mid + pos_start) / 2
46
47
  end
47
48
  end
49
+ @file.close
48
50
 
49
51
  return ta.oldest.pos
50
52
  end
51
53
 
52
54
  private
53
- def seek_log_pos log, pos
54
- log.seek pos
55
- log_size = File.size(log)
55
+ def seek_log_pos file, pos
56
+ file.seek pos
57
+ file_size = File.size(file)
56
58
 
57
59
  loop do
58
- line = log.gets.to_s.strip!.to_s
59
- return log_size if log.pos >= log_size
60
+ line = file.gets.to_s.strip!.to_s
61
+ return file_size if file.pos >= file_size
60
62
  next if (time = @time_parser.call(line)).nil?
61
- return [log.pos, time]
63
+ return [file.pos, time]
62
64
  end
63
65
  end
64
66
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: logpos
3
3
  version: !ruby/object:Gem::Version
4
- hash: 21
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
8
  - 0
9
- - 1
10
- version: 1.0.1
9
+ - 2
10
+ version: 1.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - mvj3
@@ -15,24 +15,39 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-09-17 00:00:00 Z
18
+ date: 2012-03-27 00:00:00 Z
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
- name: hoe
21
+ name: rdoc
22
22
  prerelease: false
23
23
  requirement: &id001 !ruby/object:Gem::Requirement
24
24
  none: false
25
25
  requirements:
26
26
  - - ~>
27
27
  - !ruby/object:Gem::Version
28
- hash: 27
28
+ hash: 19
29
29
  segments:
30
- - 2
31
- - 12
32
- version: "2.12"
30
+ - 3
31
+ - 10
32
+ version: "3.10"
33
33
  type: :development
34
34
  version_requirements: *id001
35
- description: Use binary search to seek a position in logs, see http://mvj3.github.com/2011/09/17/use-binary-search-to-seek-a-position-in-rails-logs
35
+ - !ruby/object:Gem::Dependency
36
+ name: hoe
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ hash: 31
44
+ segments:
45
+ - 2
46
+ - 14
47
+ version: "2.14"
48
+ type: :development
49
+ version_requirements: *id002
50
+ description: Use binary search to seek a position in logs.
36
51
  email:
37
52
  - mvjome@gmail.com
38
53
  executables:
@@ -86,6 +101,6 @@ rubyforge_project: logpos
86
101
  rubygems_version: 1.8.10
87
102
  signing_key:
88
103
  specification_version: 3
89
- summary: Use binary search to seek a position in logs, see http://mvj3.github.com/2011/09/17/use-binary-search-to-seek-a-position-in-rails-logs
104
+ summary: Use binary search to seek a position in logs.
90
105
  test_files:
91
106
  - test/test_logpos.rb