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.
375 376 377 378 379 |
# File 'lib/zold/commands/node.rb', line 375 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
396 397 398 399 400 401 402 403 404 405 406 |
# File 'lib/zold/commands/node.rb', line 396 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
381 382 383 384 385 386 387 388 389 390 391 392 393 394 |
# File 'lib/zold/commands/node.rb', line 381 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 |