logpos 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.autotest ADDED
@@ -0,0 +1,23 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'autotest/restart'
4
+
5
+ # Autotest.add_hook :initialize do |at|
6
+ # at.extra_files << "../some/external/dependency.rb"
7
+ #
8
+ # at.libs << ":../some/external"
9
+ #
10
+ # at.add_exception 'vendor'
11
+ #
12
+ # at.add_mapping(/dependency.rb/) do |f, _|
13
+ # at.files_matching(/test_.*rb$/)
14
+ # end
15
+ #
16
+ # %w(TestA TestB).each do |klass|
17
+ # at.extra_class_map[klass] = "test/test_misc.rb"
18
+ # end
19
+ # end
20
+
21
+ # Autotest.add_hook :run_command do |at|
22
+ # system "rake build"
23
+ # end
data/.gemtest ADDED
File without changes
data/History.txt ADDED
@@ -0,0 +1,6 @@
1
+ === 1.0.0 / 2011-08-16
2
+
3
+ * 1 major enhancement
4
+
5
+ * Birthday!
6
+
data/Manifest.txt ADDED
@@ -0,0 +1,8 @@
1
+ .autotest
2
+ History.txt
3
+ Manifest.txt
4
+ README.txt
5
+ Rakefile
6
+ bin/logpos
7
+ lib/logpos.rb
8
+ test/test_logpos.rb
data/README.txt ADDED
@@ -0,0 +1,61 @@
1
+ = logpos
2
+
3
+ * http://github.com/mvj3/logpos
4
+
5
+ == DESCRIPTION:
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
8
+
9
+ == FEATURES/PROBLEMS:
10
+
11
+ * Custome Time format
12
+
13
+ == SYNOPSIS:
14
+
15
+ require 'logpos'
16
+
17
+ pos = Logpos.seek_pos_before log_path, time
18
+ # or
19
+ lg = Logpos.new
20
+ lg.time_parser = proc {|line| line.match(/^Started/) && TIME_PARSER_CLASS.parse(line.split(/for [0-9\.]* at /)[-1]) }
21
+ pos = lg.seek_pos_before log_path, time
22
+
23
+ == REQUIREMENTS:
24
+
25
+ == INSTALL:
26
+
27
+ * [sudo] gem install logpos
28
+
29
+ == DEVELOPERS:
30
+
31
+ After checking out the source, run:
32
+
33
+ $ rake newb
34
+
35
+ This task will install any missing dependencies, run the tests/specs,
36
+ and generate the RDoc.
37
+
38
+ == LICENSE:
39
+
40
+ (The MIT License)
41
+
42
+ Copyright (c) 2011 eoe, Inc.
43
+
44
+ Permission is hereby granted, free of charge, to any person obtaining
45
+ a copy of this software and associated documentation files (the
46
+ 'Software'), to deal in the Software without restriction, including
47
+ without limitation the rights to use, copy, modify, merge, publish,
48
+ distribute, sublicense, and/or sell copies of the Software, and to
49
+ permit persons to whom the Software is furnished to do so, subject to
50
+ the following conditions:
51
+
52
+ The above copyright notice and this permission notice shall be
53
+ included in all copies or substantial portions of the Software.
54
+
55
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
56
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
57
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
58
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
59
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
60
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
61
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,19 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'rubygems'
4
+ require 'hoe'
5
+
6
+ # Hoe.plugin :compiler
7
+ # Hoe.plugin :gem_prelude_sucks
8
+ # Hoe.plugin :inline
9
+ # Hoe.plugin :racc
10
+ # Hoe.plugin :rubyforge
11
+
12
+ Hoe.spec 'logpos' do
13
+ developer('mvj3', 'mvjome@gmail.com')
14
+
15
+ require 'logpos'
16
+ version = Logpos::VERSION
17
+ end
18
+
19
+ # vim: syntax=ruby
data/bin/logpos ADDED
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env ruby
2
+ require 'logpos'
data/lib/logpos.rb ADDED
@@ -0,0 +1,88 @@
1
+ class Logpos
2
+ VERSION = '1.0.1'
3
+
4
+ begin; require 'chronic'; rescue LoadError; end
5
+ TIME_PARSER_CLASS = if defined? Chronic
6
+ Chronic
7
+ else
8
+ require 'active_support'
9
+ Time
10
+ end
11
+
12
+
13
+ attr_accessor :time_parser
14
+ def initialize
15
+ @time_parser = proc {|line| line.match(/^Started/) && TIME_PARSER_CLASS.parse(line.split(/for [0-9\.]* at /)[-1]) }
16
+ end
17
+
18
+ def self.seek_pos_before log_path, lastest_visited_at
19
+ Logpos.new.seek_pos_before log_path, lastest_visited_at
20
+ end
21
+
22
+ def seek_pos_before log_path, lastest_visited_at
23
+ ta = TripleArray.new lastest_visited_at
24
+
25
+ log = File.open(log_path)
26
+ pos_start, pos_end = 0, File.size(log)
27
+ pos_mid = pos_end / 2
28
+ time = Time.at(0)
29
+ between = -1.0/0
30
+
31
+ loop do
32
+ pos_mid, time = seek_log_pos(log, pos_mid)
33
+ break if time.nil? || (pos_mid == pos_end)
34
+
35
+ time_valid = lastest_visited_at >= time
36
+ break if time_valid && ((lastest_visited_at - time) >= between.abs)
37
+ between = lastest_visited_at - time
38
+ break if time_valid && ta.push(pos_mid, time).nil?
39
+
40
+ if between > 0
41
+ pos_start = pos_mid
42
+ pos_mid = (pos_mid + pos_end) / 2
43
+ elsif between < 0
44
+ pos_end = pos_mid
45
+ pos_mid = (pos_mid + pos_start) / 2
46
+ end
47
+ end
48
+
49
+ return ta.oldest.pos
50
+ end
51
+
52
+ private
53
+ def seek_log_pos log, pos
54
+ log.seek pos
55
+ log_size = File.size(log)
56
+
57
+ loop do
58
+ line = log.gets.to_s.strip!.to_s
59
+ return log_size if log.pos >= log_size
60
+ next if (time = @time_parser.call(line)).nil?
61
+ return [log.pos, time]
62
+ end
63
+ end
64
+
65
+ class TripleArray
66
+ TP = Struct.new(:pos, :time)
67
+ def initialize lastest_visited_at
68
+ @lastest_visited_at = lastest_visited_at
69
+ @array = Array.new(3, TP.new(0, Time.at(0)))
70
+ end
71
+
72
+ def push time, pos
73
+ return nil if include? time
74
+
75
+ @array[0..1] = @array[1..2]
76
+ @array[2] = TP.new(time, pos)
77
+ @array
78
+ end
79
+
80
+ def include? time
81
+ !!@array.detect {|tp| tp.time == time }
82
+ end
83
+
84
+ def oldest
85
+ @array.select {|a| a.time <= @lastest_visited_at }.sort {|a, b| a.time <=> b.time}[-1]
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,8 @@
1
+ require "test/unit"
2
+ require "logpos"
3
+
4
+ class TestLogpos < Test::Unit::TestCase
5
+ def test_sanity
6
+ flunk "write tests or I will kneecap you"
7
+ end
8
+ end
metadata ADDED
@@ -0,0 +1,91 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: logpos
3
+ version: !ruby/object:Gem::Version
4
+ hash: 21
5
+ prerelease:
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 1
10
+ version: 1.0.1
11
+ platform: ruby
12
+ authors:
13
+ - mvj3
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-09-17 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: hoe
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ hash: 27
29
+ segments:
30
+ - 2
31
+ - 12
32
+ version: "2.12"
33
+ type: :development
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
36
+ email:
37
+ - mvjome@gmail.com
38
+ executables:
39
+ - logpos
40
+ extensions: []
41
+
42
+ extra_rdoc_files:
43
+ - History.txt
44
+ - Manifest.txt
45
+ - README.txt
46
+ files:
47
+ - .autotest
48
+ - History.txt
49
+ - Manifest.txt
50
+ - README.txt
51
+ - Rakefile
52
+ - bin/logpos
53
+ - lib/logpos.rb
54
+ - test/test_logpos.rb
55
+ - .gemtest
56
+ homepage: http://github.com/mvj3/logpos
57
+ licenses: []
58
+
59
+ post_install_message:
60
+ rdoc_options:
61
+ - --main
62
+ - README.txt
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ hash: 3
71
+ segments:
72
+ - 0
73
+ version: "0"
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ hash: 3
80
+ segments:
81
+ - 0
82
+ version: "0"
83
+ requirements: []
84
+
85
+ rubyforge_project: logpos
86
+ rubygems_version: 1.8.10
87
+ signing_key:
88
+ 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
90
+ test_files:
91
+ - test/test_logpos.rb