Class: FileLogger

Inherits:
Object
  • Object
show all
Defined in:
lib/file_logger.rb,
lib/file_logger/version.rb

Constant Summary collapse

VERSION =
"0.0.2"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = "./STDOUT.log", autoclose: true) ⇒ FileLogger

Returns a new instance of FileLogger.



6
7
8
9
10
11
12
13
14
15
16
# 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

Instance Attribute Details

#intervalObject

Returns the value of attribute interval.



4
5
6
# File 'lib/file_logger.rb', line 4

def interval
  @interval
end

#stdout_limitObject

Returns the value of attribute stdout_limit.



4
5
6
# File 'lib/file_logger.rb', line 4

def stdout_limit
  @stdout_limit
end

#strict_modeObject

Returns the value of attribute strict_mode.



4
5
6
# File 'lib/file_logger.rb', line 4

def strict_mode
  @strict_mode
end

Instance Method Details

#cleanObject Also known as: delete



36
37
38
39
40
41
# 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

#closeObject



50
51
52
53
# File 'lib/file_logger.rb', line 50

def close
  @file.close
  @counter = 0
end

#file_checkObject



66
67
68
69
70
71
72
# 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

#getObject



31
32
33
34
# File 'lib/file_logger.rb', line 31

def get
  save
  return @path
end

#io_puts(string, io = nil) ⇒ Object



55
56
57
58
# 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") ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# 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_tempObject



60
61
62
63
64
# 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


81
82
83
84
85
86
# File 'lib/file_logger.rb', line 81

def print(string, io=nil)
  file_check
  @file.print(string.to_s)
  io_puts(string,io)
  count
end

#puts(string, io = nil) ⇒ Object



74
75
76
77
78
79
# 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

#saveObject



44
45
46
47
48
# File 'lib/file_logger.rb', line 44

def save
  @file.close
  @file = File.open(@path,'a')
  @counter = 0
end