File: log_file.rb

package info (click to toggle)
ruby-process-daemon 1.0.1-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 196 kB
  • sloc: ruby: 761; makefile: 7
file content (118 lines) | stat: -rw-r--r-- 3,240 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
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
# Copyright, 2014, by Samuel G. D. Williams. <http://www.codeotaku.com>
# 
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

module Process
	class Daemon
		# This is a special file instance which provides the ability to read a log file from the end backwards.
		class LogFile < File
			# Yields the lines of a log file in reverse order, once the yield statement returns true, stops, and returns the lines in order.
			def tail_log
				lines = []

				seek_end
				
				reverse_each_line do |line|
					lines << line
					
					break if block_given? and yield line
				end

				return lines.reverse
			end
			
			private
			
			# Seek to the end of the file
			def seek_end(offset = 0)
				seek(offset, IO::SEEK_END)
			end
	
			# Read a chunk of data and then move the file pointer backwards.
			#
			# Calling this function multiple times will return new data and traverse the file backwards.
			#
			def read_reverse(length)
				offset = tell

				if offset == 0
					return nil
				end

				start = [0, offset-length].max

				seek(start, IO::SEEK_SET)

				buf = read(offset-start)

				seek(start, IO::SEEK_SET)

				return buf
			end

			REVERSE_BUFFER_SIZE = 128

			# This function is very similar to gets but it works in reverse.
			#
			# You can use it to efficiently read a file line by line backwards.
			# 
			# It returns nil when there are no more lines.
			def reverse_gets(sep_string=$/)
				end_pos = tell

				offset = nil
				buf = ""

				while offset == nil
					chunk = read_reverse(REVERSE_BUFFER_SIZE)

					return (buf == "" ? nil : buf) if chunk == nil

					buf = chunk + buf

					# Don't consider the last newline.
					offset = buf.rindex(sep_string, -(sep_string.length + 1))
				end

				# Don't include newline:
				offset += 1

				line = buf[offset...buf.size]

				seek((end_pos - buf.size) + offset, IO::SEEK_SET)

				return line
			end

			# Similar to each_line but works in reverse. Don't forget to call
			# seek_end before you start!
			def reverse_each_line(sep_string=$/, &block)
				return to_enum(:reverse_each_line) unless block_given?
		
				line = reverse_gets(sep_string)

				while line != nil
					yield line

					line = reverse_gets(sep_string)
				end
			end
		end
	end
end