Class: Zold::Node::NohupLog
- Inherits:
-
Object
- Object
- Zold::Node::NohupLog
- Defined in:
- lib/zold/commands/node.rb
Overview
Log facility for nohup
Instance Method Summary collapse
- #copy(source, target, start = 0) ⇒ Object
-
#initialize(file, max) ⇒ NohupLog
constructor
A new instance of NohupLog.
- #print(data) ⇒ Object
Constructor Details
#initialize(file, max) ⇒ NohupLog
Returns a new instance of NohupLog.
486 487 488 489 490 |
# File 'lib/zold/commands/node.rb', line 486 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
507 508 509 510 511 512 513 514 515 516 517 |
# File 'lib/zold/commands/node.rb', line 507 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 |
#print(data) ⇒ Object
492 493 494 495 496 497 498 499 500 501 502 503 504 505 |
# File 'lib/zold/commands/node.rb', line 492 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 |