Class: ASpaceLogger

Inherits:
Logger
  • Object
show all
Defined in:
common/aspace_logger.rb

Instance Method Summary collapse

Constructor Details

#initialize(logdev) ⇒ ASpaceLogger

Returns a new instance of ASpaceLogger.



6
7
8
9
10
# File 'common/aspace_logger.rb', line 6

def initialize(logdev)
  @backlog = Atomic.new([])
  @recording = Atomic.new(false)
  super(logdev)
end

Instance Method Details

#add(severity, message = nil, progname = nil, &block) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'common/aspace_logger.rb', line 12

def add(severity, message = nil, progname = nil, &block)
  if @recording.value == true

    if !message
      if block
        message = block.call
      else
        # O_o
        #
        # https://ruby-doc.org/stdlib-2.1.0/libdoc/logger/rdoc/Logger.html#method-i-add
        # "Program name string. Can be omitted. Treated as a message if no message and block are given."
        message = progname
        progname = nil
      end
    end

    add_to_backlog(format_message(format_severity(severity), Time.now, progname, message))
  end

  super(severity, message, progname, &block)
end

#add_to_backlog(formatted_messsage) ⇒ Object



34
35
36
37
38
39
40
41
# File 'common/aspace_logger.rb', line 34

def add_to_backlog( formatted_messsage )
  if @backlog.value.length > 100
    flush_backlog
    stop_recording
  else
    @backlog.update { |bl| bl << formatted_messsage }
  end
end

#backlogObject



43
44
45
# File 'common/aspace_logger.rb', line 43

def backlog
  @backlog.value.join("")
end

#backlog_and_flushObject



61
62
63
64
65
66
# File 'common/aspace_logger.rb', line 61

def backlog_and_flush
  backlog_cache = backlog
  flush_backlog
  start_recording
  backlog_cache
end

#flush_backlogObject



47
48
49
# File 'common/aspace_logger.rb', line 47

def flush_backlog
  @backlog.update { |bl| bl = [] }
end

#start_recordingObject



51
52
53
54
# File 'common/aspace_logger.rb', line 51

def start_recording
  return if @recording.value == true
  @recording.update { |r| r = true }
end

#stop_recordingObject



56
57
58
59
# File 'common/aspace_logger.rb', line 56

def stop_recording
  return unless @recording.value == true
  @recording.update { |r| r = false }
end