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.



490
491
492
493
494
# File 'lib/zold/commands/node.rb', line 490

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



511
512
513
514
515
516
517
518
519
520
521
# File 'lib/zold/commands/node.rb', line 511

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


496
497
498
499
500
501
502
503
504
505
506
507
508
509
# File 'lib/zold/commands/node.rb', line 496

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