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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
# File 'lib/colortail/application.rb', line 10
def run!(*arguments)
require 'fcntl'
opt = ColorTail::Options.new(arguments)
filelist = opt[:files]
options = opt[:options]
if opt[:invalid_argument]
$stderr.puts opt[:invalid_argument]
options[:help] = true
end
if options[:help]
$stderr.puts version()
$stderr.puts opt.opts
return 1
end
if options[:version]
$stderr.puts version()
return 1
end
begin
if File.exists?(options[:conf])
config = ColorTail::Configuration.new(options[:conf])
match_group = config.load_opts(options[:group])
else
match_group = Array.new
match_group.push( 'default' => [] )
end
if options[:list]
puts "The following match groups are available through your config files:"
if config
config.display_match_groups()
else
puts " * default"
end
return 1
end
logger = ColorTail::Colorize.new()
if match_group.class == Array
match_group.each do |matcher|
logger.add_color_matcher( matcher )
end
else
logger.add_color_matcher( match_group )
end
threads = Array.new
$stdin.fcntl(Fcntl::F_SETFL,Fcntl::O_NONBLOCK)
begin
threads[0] = Thread.new {
begin
$stdin.each_line do |line|
if options[:filename_prefix]
logger.log( nil, line, "STDIN: " )
else
logger.log( nil, line )
end
end
rescue Errno::EAGAIN
threads.delete(0)
end
}.run
if filelist.size > 0
@files_exist = false
files = Hash.new
filelist.each do |file|
filename = String.new
group = String.new
filename,group = file.split('#')
if group.nil?
files[filename] = options[:group]
else
files[filename] = group
end
if File.exists?(filename)
@files_exist = true
else
$stderr.puts("#{filename} does not exist, skipping...")
filelist.delete(file)
end
end
if !@files_exist and (threads.class == Array.class and threads.size <= 2)
$stderr.puts "Please check to make sure the files exist ..."
$stderr.puts opt.opts
return 1
end
files.each_pair do |filename, grouping|
threads.push Thread.new {
if config.group_exists?(grouping)
file_logger = ColorTail::Colorize.new()
match_group = config.load_opts(grouping)
if match_group.class == Array
match_group.each do |matcher|
file_logger.add_color_matcher( matcher )
end
else
file_logger.add_color_matcher( match_group )
end
else
file_logger = logger
end
tailer = ColorTail::TailFile.new( filename )
tailer.interval = 10
tailer.backward( 10 )
tailer.tail do |line|
if options[:filename_prefix]
file_logger.log( filename, line, "#{filename}: ")
else
file_logger.log( filename, line )
end
end
}.run
end
end
end
threads.each do |thread|
thread.join
end
rescue Interrupt
thread_cleanup(threads) if defined? threads
rescue NoInputError
$stderr.puts "Please enter a file to tail..."
$stderr.puts opt.opts
return 1
end
return 0
end
|