Class: WSLight::SDLogger

Inherits:
Object
  • Object
show all
Defined in:
lib/ws_light/sd_logger.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSDLogger



7
8
9
10
11
12
13
# 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

Instance Attribute Details

#debugObject

Returns the value of attribute debug.



5
6
7
# File 'lib/ws_light/sd_logger.rb', line 5

def debug
  @debug
end

#entriesObject

Returns the value of attribute entries.



5
6
7
# File 'lib/ws_light/sd_logger.rb', line 5

def entries
  @entries
end

#filenameObject

Returns the value of attribute filename.



5
6
7
# File 'lib/ws_light/sd_logger.rb', line 5

def filename
  @filename
end

#intervalObject

Returns the value of attribute interval.



5
6
7
# File 'lib/ws_light/sd_logger.rb', line 5

def interval
  @interval
end

Instance Method Details

#log(text) ⇒ Object



15
16
17
18
19
20
21
22
# 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?Boolean



36
37
38
# File 'lib/ws_light/sd_logger.rb', line 36

def timeout?
  (Time.now - @last_write) > @interval
end

#write_logObject



24
25
26
27
28
29
30
31
32
33
34
# 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