Class: Zold::Node::NohupLog

Inherits:
Object
  • Object
show all
Defined in:
lib/zold/commands/node.rb

Overview

Log facility for nohup

Instance Method Summary collapse

Constructor Details

#initialize(file, max) ⇒ NohupLog

Returns a new instance of NohupLog.



345
346
347
348
349
# File 'lib/zold/commands/node.rb', line 345

def initialize(file, max)
  @file = file
  raise "Truncation size is too small (#{max}), should be over 10Kb" if max < 10 * 1024
  @max = max
end

Instance Method Details

#copy(source, target, start = 0) ⇒ Object



366
367
368
369
370
371
372
373
374
375
376
# File 'lib/zold/commands/node.rb', line 366

def copy(source, target, start = 0)
  total = 0
  File.open(target, 'w') do |t|
    File.open(source, 'r').each do |line|
      next unless total >= start
      t.print(line)
      total += 1
    end
  end
  total
end


351
352
353
354
355
356
357
358
359
360
361
362
363
364
# File 'lib/zold/commands/node.rb', line 351

def print(data)
  File.open(@file, 'a') { |f| f.print(data) }
  return if File.size(@file) < @max
  temp = Tempfile.new
  total = copy(@file, temp)
  unit = File.size(@file) / total
  tail = total - @max / (2 * unit)
  copy(temp, @file, tail)
  File.delete(temp)
  File.open(@file, 'a') do |f|
    f.print("The file was truncated, because it was over the quota of #{@max} bytes, \
#{tail} lines left out of #{total}, average line length was #{unit} bytes\n\n")
  end
end