Class: WSLight::SDLogger
- Inherits:
-
Object
- Object
- WSLight::SDLogger
- 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
-
#debug ⇒ Object
Returns the value of attribute debug.
-
#entries ⇒ Object
Returns the value of attribute entries.
-
#filename ⇒ Object
Returns the value of attribute filename.
-
#interval ⇒ Object
Returns the value of attribute interval.
Instance Method Summary collapse
-
#initialize ⇒ SDLogger
constructor
A new instance of SDLogger.
- #log(text) ⇒ Object
- #timeout? ⇒ Boolean
- #write_log ⇒ Object
Constructor Details
#initialize ⇒ SDLogger
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
#debug ⇒ Object
Returns the value of attribute debug.
5 6 7 |
# File 'lib/ws_light/sd_logger.rb', line 5 def debug @debug end |
#entries ⇒ Object
Returns the value of attribute entries.
5 6 7 |
# File 'lib/ws_light/sd_logger.rb', line 5 def entries @entries end |
#filename ⇒ Object
Returns the value of attribute filename.
5 6 7 |
# File 'lib/ws_light/sd_logger.rb', line 5 def filename @filename end |
#interval ⇒ Object
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_log ⇒ Object
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 |