class FileLogger

Constants

VERSION

Attributes

interval[RW]
stdout_limit[RW]
strict_mode[RW]

Public Class Methods

new(path="./STDOUT.log", autoclose: true) click to toggle source
# File lib/file_logger.rb, line 6
def initialize(path="./STDOUT.log", autoclose: true)
  @counter = 0
  @interval = 1000
  @mode = "a"
  @stdout_limit = -1
  @strict_mode = false
  self.open(path)

  ObjectSpace.define_finalizer(self, proc{ close }) if autoclose
  puts "---- #{Time.now} ----\n"
end

Public Instance Methods

clean() click to toggle source
# File lib/file_logger.rb, line 36
def clean
  return if @file.nil?
  STDERR.puts "Delete '#{@path}'"
  File.delete(@path)
  @file = File.open(@path,@mode)
end
Also aliased as: delete
close() click to toggle source
# File lib/file_logger.rb, line 50
def close
  @file.close
  @counter = 0
end
delete()
Alias for: clean
file_check() click to toggle source
# File lib/file_logger.rb, line 66
def file_check
  if @file.nil?
    raise '[FileLogger] File does not opened!' if @strict_mode
    STDERR.puts Color.red "file does not opened!"
    open_temp
  end
end
get() click to toggle source
# File lib/file_logger.rb, line 31
def get
  save
  return @path
end
io_puts(string,io=nil) click to toggle source
# File lib/file_logger.rb, line 55
def io_puts(string,io=nil)
  return unless io
  io.puts string.to_s[0..@stdout_limit]
end
open(path="./STDOUT.log") click to toggle source
# File lib/file_logger.rb, line 18
def open(path="./STDOUT.log")
  if path.is_a?(IO)
    @mode = "w"
    @path = './STDOUT.log'
    STDERR.puts 'STDOUT.log / Temporary standard output.'
  else
    @path = path
    @path = "./STDOUT.log" if path.nil?
  end
  @file = File.open(@path,@mode)
  STDERR.puts "FileLogger#Opening '#{@path}'"
end
open_temp() click to toggle source
# File lib/file_logger.rb, line 60
def open_temp
  path = 'temporary'+Time.new.strftime("%Y%m%d%H%M%S").to_s + '.log' 
  STDERR.puts Color.red "opening #{path}"
  open(path)
end
print(string, io=nil) click to toggle source
puts(string,io=nil) click to toggle source
# File lib/file_logger.rb, line 74
def puts(string,io=nil)
  file_check
  @file.puts(string.to_s)
  io_puts(string,io)
  count
end
save() click to toggle source
# File lib/file_logger.rb, line 44
def save
  @file.close
  @file = File.open(@path,'a')
  @counter = 0
end

Private Instance Methods

count() click to toggle source
# File lib/file_logger.rb, line 89
def count
  save if (@counter += 1) == interval
end