Class: FileLogger

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

Constant Summary collapse

VERSION =
"0.0.1"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = "./STDOUT.log") ⇒ FileLogger

Returns a new instance of FileLogger.



6
7
8
9
10
11
12
13
14
# File 'lib/file_logger.rb', line 6

def initialize(path="./STDOUT.log")
  @counter = 0
  @interval = 1000
  @mode = "a"
  @stdout_limit = -1
  @strict_mode = false
  self.open(path)
  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



34
35
36
37
38
39
# File 'lib/file_logger.rb', line 34

def clean
  return if @file.nil?
  STDERR.puts "Delete '#{@path}'"
  File.delete(@path)
  @file = File.open(@path,@mode)
end

#closeObject



48
49
50
51
# File 'lib/file_logger.rb', line 48

def close
  @file.close
  @counter = 0
end

#file_checkObject



64
65
66
67
68
69
70
# File 'lib/file_logger.rb', line 64

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



29
30
31
32
# File 'lib/file_logger.rb', line 29

def get
  save
  return @path
end

#io_puts(string, io = nil) ⇒ Object



53
54
55
56
# File 'lib/file_logger.rb', line 53

def io_puts(string,io=nil)
  return unless io
  io.puts string.to_s[0..@stdout_limit]
end

#open(path = "./STDOUT.log") ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/file_logger.rb', line 16

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



58
59
60
61
62
# File 'lib/file_logger.rb', line 58

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


79
80
81
82
83
84
# File 'lib/file_logger.rb', line 79

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

#puts(string, io = nil) ⇒ Object



72
73
74
75
76
77
# File 'lib/file_logger.rb', line 72

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

#saveObject



42
43
44
45
46
# File 'lib/file_logger.rb', line 42

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