Class: BlackStack::LocalLogger

Inherits:
BaseLogger show all
Defined in:
lib/locallogger.rb

Constant Summary

Constants inherited from BaseLogger

BaseLogger::NEWLINE

Instance Attribute Summary

Attributes inherited from BaseLogger

#filename, #level, #level_children_lines, #level_open_callers

Instance Method Summary collapse

Methods inherited from BaseLogger

#blank_line, #done, #error, #initialize, #initialize_attributes, #no, #ok, #skip, #yes

Constructor Details

This class inherits a constructor from BlackStack::BaseLogger

Instance Method Details

#log(s, datetime = nil) ⇒ Object



38
39
40
41
42
# File 'lib/locallogger.rb', line 38

def log(s, datetime=nil)
  ltext = super(s, datetime)
  self.write(ltext)
  ltext
end

#logf(s, datetime = nil) ⇒ Object



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

def logf(s, datetime=nil)      
  ltext = super(s, datetime)
  self.write(ltext)
  ltext
end

#logs(s, datetime = nil) ⇒ Object



45
46
47
48
49
# File 'lib/locallogger.rb', line 45

def logs(s, datetime=nil)
  ltext = super(s, datetime)
  self.write(ltext)
  ltext
end

#releaseObject



59
60
61
# File 'lib/locallogger.rb', line 59

def release()
  BlackStack::LocalLoggerFactory.release(self.filename)
end

#resetObject

call the parent class to set the attributes call the save method to store the new attributes into the data file



6
7
8
9
# File 'lib/locallogger.rb', line 6

def reset()
  super
  BlackStack::LocalLoggerFactory::save(self.filename, self)
end

#write(s) ⇒ Object

store the min allowed bytes in the variable min store the max allowed bytes in the variable max get number of bytes of filename and store it the variable n if number of bytes (n) is higer than max, then truncate the first (max-min) bytes in the file. finally, add the text into the variable s at the end of the file.



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/locallogger.rb', line 16

def write(s)
  # store the min allowed bytes in the variable min
  min = Logger.min_size
  # store the max allowed bytes in the variable max
  max = Logger.max_size
  # get number of bytes of filename and store it the variable n
  n = File.exists?(self.filename) ? File.size(self.filename) : 0
  # if number of bytes (n) is higer than max, then truncate the first (max-min) bytes in the file.
  if n > max
    # Read the content of the file
    content = File.read(self.filename)
    # Calculate the number of bytes to truncate
    truncate_bytes = n - (max - min)
    # Truncate the first (max-min) bytes in the file
    truncated_content = content[truncate_bytes..-1]
    # Write the truncated content back to the file
    File.open(self.filename, 'w') { |file| file.write(truncated_content) }
  end
  #
  File.open(self.filename, 'a') { |file| file.write(s) }
end