hq-log-monitor-client 0.1.3 → 0.1.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -34,11 +34,17 @@ class Script
34
34
  { :name => :config,
35
35
  :required => true },
36
36
 
37
+ { :name => :debug,
38
+ :boolean => true,
39
+ :required => false },
40
+
37
41
  ]
38
42
 
39
43
  @args.empty? \
40
44
  or raise "Extra args on command line"
41
45
 
46
+ @debug = @opts[:debug]
47
+
42
48
  end
43
49
 
44
50
  def read_config
@@ -104,6 +110,8 @@ class Script
104
110
  @service_elems.each do
105
111
  |service_elem|
106
112
 
113
+ debug "==== service #{service_elem["name"]}"
114
+
107
115
  fileset_elems = service_elem.find("fileset").to_a
108
116
 
109
117
  fileset_elems.each do
@@ -137,6 +145,8 @@ class Script
137
145
  file_names.each do
138
146
  |file_name|
139
147
 
148
+ debug "-- file #{file_name}"
149
+
140
150
  file_mtime = File.mtime file_name
141
151
  file_size = File.size file_name
142
152
 
@@ -147,7 +157,14 @@ class Script
147
157
  if cache_file &&
148
158
  file_mtime == cache_file[:mtime] &&
149
159
  file_size == cache_file[:size]
160
+
161
+ debug "mtime %s and size %s match, skip" % [
162
+ file_mtime,
163
+ file_size,
164
+ ]
165
+
150
166
  next
167
+
151
168
  end
152
169
 
153
170
  # scan the file for matching lines
@@ -162,7 +179,7 @@ class Script
162
179
  file_io,
163
180
  max_before + max_after + 1
164
181
 
165
- file_hash = 0
182
+ file_digest = Digest::MD5.new
166
183
 
167
184
  # check if the file has changed
168
185
 
@@ -170,46 +187,80 @@ class Script
170
187
 
171
188
  if file_size < cache_file[:size]
172
189
 
190
+ debug \
191
+ "size %s is less than cache size %s" % [
192
+ file_size,
193
+ cache_file[:size],
194
+ ]
195
+
173
196
  changed = true
174
197
 
175
198
  else
176
199
 
200
+ debug "scanning start of file"
201
+
177
202
  changed = false
178
203
 
179
204
  cache_file[:lines].times do
205
+ |line_num|
180
206
 
181
207
  line = file_reader.gets
182
208
 
183
209
  unless line
210
+
211
+ debug \
212
+ "only read %s lines, previously " \
213
+ "read %s" % [
214
+ line_num,
215
+ cache_file[:lines],
216
+ ]
217
+
184
218
  changed = true
185
219
  break
220
+
186
221
  end
187
222
 
188
- file_hash = [ file_hash, line.hash ].hash
223
+ file_digest.update line
189
224
 
190
225
  end
191
226
 
227
+ file_hash = file_digest.to_s
228
+
192
229
  if file_hash != cache_file[:hash]
230
+
231
+ debug \
232
+ "hash %s does not match previous " \
233
+ "value %s" % [
234
+ file_hash,
235
+ cache_file[:hash]
236
+ ]
237
+
193
238
  changed = true
239
+
194
240
  end
195
241
 
196
242
  end
197
243
 
198
- end
244
+ # go back to start if it changed
199
245
 
200
- # go back to start if it changed
246
+ if changed
247
+ debug "seeking to start"
248
+ file_io.seek 0
249
+ file_reader.reset
250
+ file_digest = Digest::MD5.new
251
+ else
252
+ debug "start of file not changed"
253
+ end
201
254
 
202
- if changed
203
- file_io.seek 0
204
- file_reader.reset
205
- file_hash = 0
206
255
  end
207
256
 
208
257
  # scan the new part of the file
209
258
 
259
+ debug "searching for events"
260
+
210
261
  while line = file_reader.gets
211
262
 
212
- file_hash = [ file_hash, line.hash ].hash
263
+ file_digest.update line
213
264
 
214
265
  # check for a match
215
266
 
@@ -261,6 +312,19 @@ class Script
261
312
 
262
313
  # save the file's current info in the cache
263
314
 
315
+ file_hash = file_digest.to_s
316
+
317
+ file_lines = file_reader.next_line_number
318
+
319
+ debug \
320
+ "updating cache mtime %s, size %s, lines %s, " \
321
+ "hash %s" % [
322
+ file_mtime,
323
+ file_size,
324
+ file_lines,
325
+ file_hash,
326
+ ]
327
+
264
328
  @cache[:files][file_name] = {
265
329
  mtime: file_mtime,
266
330
  size: file_size,
@@ -394,6 +458,11 @@ class Script
394
458
 
395
459
  end
396
460
 
461
+ def debug message
462
+ return unless @debug
463
+ $stderr.puts message
464
+ end
465
+
397
466
  end
398
467
  end
399
468
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hq-log-monitor-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-05-08 00:00:00.000000000 Z
12
+ date: 2013-05-09 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: hq-tools