class WSLight::SDLogger

Provides a logger which writes only in long intervals, thus reducing write access to the cd card (out data is not that crucial)

Attributes

debug[RW]
entries[RW]
filename[RW]
interval[RW]

Public Class Methods

new() click to toggle source
# File lib/ws_light/sd_logger.rb, line 7
def initialize
  @filename = '/var/log/motion.log'
  @interval = 1800 # log interval in seconds
  @entries = []
  @last_write = Time.now
  @debug = false
end

Public Instance Methods

log(text) click to toggle source
# File lib/ws_light/sd_logger.rb, line 15
def log(text)
  puts Time.now.to_s + ' -> ' + text if @debug
  entries << {
    text: text,
    time: Time.now
  }
  write_log if timeout?
end
timeout?() click to toggle source
# File lib/ws_light/sd_logger.rb, line 36
def timeout?
  (Time.now - @last_write) > @interval
end
write_log() click to toggle source
# File lib/ws_light/sd_logger.rb, line 24
def write_log
  return if @entries.empty?

  file = File.open(@filename, File.exist?(@filename) ? 'a' : 'w')
  @entries.each do |entry|
    file.puts(entry[:time].to_s + ', ' + entry[:text])
  end
  file.close
  @entries = []
  @last_write = Time.now
end