Module: ODDB::LogFile

Defined in:
src/util/logfile.rb,
test/test_util/logfile.rb

Constant Summary

LOG_ROOT =
File.expand_path('../data/log', File.dirname(__FILE__))

Instance Method Summary (collapse)

Instance Method Details

- (Object) append(key, line, time = Time.local)



11
12
13
14
15
16
17
# File 'src/util/logfile.rb', line 11

def append(key, line, time=Time.local)
  file = filename(key, time)
  dir = File.dirname(file)
  FileUtils.mkdir_p(dir)
  timestr = time.strftime('%Y-%m-%d %H:%M:%S %Z')
  File.open(file, 'a') { |fh| fh << [timestr, line, "\n"].join }
end

- (Object) filename(key, time)



18
19
20
21
22
23
24
25
# File 'src/util/logfile.rb', line 18

def filename(key, time)
  path = [
    key,
    time.year,
    sprintf('%02i', time.month) + '.log',
  ].join('/')
  File.expand_path(path, LOG_ROOT)
end

- (Object) read(key, time)



26
27
28
29
30
31
32
# File 'src/util/logfile.rb', line 26

def read(key, time)
  begin
    File.read(filename(key, time))
  rescue(StandardError)
    ''
  end
end