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.



453
454
455
456
457
# File 'lib/zold/commands/node.rb', line 453

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



474
475
476
477
478
479
480
481
482
483
484
# File 'lib/zold/commands/node.rb', line 474

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


459
460
461
462
463
464
465
466
467
468
469
470
471
472
# File 'lib/zold/commands/node.rb', line 459

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