Class: RSMP::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/rsmp/logger.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(settings = {}) ⇒ Logger

Returns a new instance of Logger.


6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/rsmp/logger.rb', line 6

def initialize settings={}
  defaults = {
    'active'=>false,
    'path'=>nil,
    'stream'=>nil,
    'author'=>false,
    'color'=>true,
    'site_id'=>true,
    'component'=>false,
    'level'=>false,
    'ip'=>false,
    'port'=>false,
    'index'=>false,
    'timestamp'=>true,
    'json'=>false,
    'debug'=>false,
    'statistics'=>false,
    'hide_ip_and_port' => false
  }
  if settings
    @settings = defaults.merge settings
  else
    @settings = defaults
  end

  @muted = {}

  setup_output_destination
end

Instance Attribute Details

#settingsObject

Returns the value of attribute settings.


4
5
6
# File 'lib/rsmp/logger.rb', line 4

def settings
  @settings
end

Class Method Details

.shorten_message_id(m_id, length = 4) ⇒ Object


123
124
125
126
127
128
129
# File 'lib/rsmp/logger.rb', line 123

def self.shorten_message_id m_id, length=4
  if m_id
    m_id[0..length-1].ljust(length)
  else
    ' '*length
  end
end

Instance Method Details

#build_output(item) ⇒ Object


140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/rsmp/logger.rb', line 140

def build_output item
  parts = []
  parts << item[:index].to_s.ljust(7) if @settings["index"] == true
  parts << item[:author].to_s.ljust(13) if @settings["author"] == true
  parts << Clock.to_s(item[:timestamp]).ljust(24) unless @settings["timestamp"] == false
  parts << item[:ip].to_s.ljust(22) unless @settings["ip"] == false
  parts << item[:port].to_s.ljust(8) unless @settings["port"] == false
  parts << item[:site_id].to_s.ljust(13) unless @settings["site_id"] == false
  parts << item[:component_id].to_s.ljust(18) unless @settings["component"] == false
  
  directions = {in:"-->",out:"<--"}
  parts << directions[item[:direction]].to_s.ljust(4) unless @settings["direction"] == false

  parts << item[:level].to_s.capitalize.ljust(7) unless @settings["level"] == false

  
  unless @settings["id"] == false
    length = 4
    if item[:message]
      parts << Logger.shorten_message_id(item[:message].m_id,length)+' '
    else
      parts << " "*(length+1)
    end
  end
  parts << item[:str].to_s.strip unless @settings["text"] == false
  parts << item[:message].json unless @settings["json"] == false || item[:message] == nil

  if item[:exception]
    parts << "#{item[:exception].class.to_s}\n"
    parts << item[:exception].backtrace.join("\n")
  end

  out = parts.join(' ').chomp(' ')
  out
end

#colorize(level, str) ⇒ Object


88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/rsmp/logger.rb', line 88

def colorize level, str
  if @settings["color"] == false || @settings["color"] == nil
    str
  elsif @settings["color"] == true || @settings["color"].is_a?(Hash)
    colors = {
      'info' => 'white',
      'log' => 'light_blue',
      'test' => 'light_magenta',
      'statistics' => 'light_black',
      'not_acknowledged' => 'cyan',
      'warning' => 'light_yellow',
      'error' => 'red',
      'debug' => 'light_black'
    }
    colors.merge! @settings["color"] if @settings["color"].is_a?(Hash)
    if colors[level.to_s]
      str.colorize colors[level.to_s].to_sym
    else
      str
    end
  else
    if level == :nack || level == :warning || level == :error
      str.colorize(@settings["color"]).bold
    else
      str.colorize @settings["color"]
    end
  end
end

#dump(archive, force: false, num: nil) ⇒ Object


131
132
133
134
135
136
137
138
# File 'lib/rsmp/logger.rb', line 131

def dump archive, force:false, num:nil
  num ||= archive.items.size
  log = archive.items.last(num).map do |item|
    str = build_output item
    str = colorize item[:level], str
  end
  log.join("\n")
end

#log(item, force: false) ⇒ Object


117
118
119
120
121
# File 'lib/rsmp/logger.rb', line 117

def log item, force:false
  if output?(item, force)
    output item[:level], build_output(item) 
  end
end

#mute(ip, port) ⇒ Object


46
47
48
# File 'lib/rsmp/logger.rb', line 46

def mute ip, port
  @muted["#{ip}:#{port}"] = true
end

#output(level, str) ⇒ Object


81
82
83
84
85
86
# File 'lib/rsmp/logger.rb', line 81

def output level, str
  return if str.empty? || /^\s+$/.match(str)
  str = colorize level, str
  @stream.puts str
  @stream.flush
end

#output?(item, force = false) ⇒ Boolean

Returns:

  • (Boolean)

58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/rsmp/logger.rb', line 58

def output? item, force=false
  return false if item[:ip] && item[:port] && @muted["#{item[:ip]}:#{item[:port]}"]
  return false if @settings["active"] == false && force != true
  return false if @settings["info"] == false && item[:level] == :info
  return false if @settings["debug"] != true && item[:level] == :debug
  return false if @settings["statistics"] != true && item[:level] == :statistics
  return false if @settings["test"] != true && item[:level] == :test

  if item[:message]
    type = item[:message].type
    ack = type == "MessageAck" || type == "MessageNotAck"
    if @settings["watchdogs"] == false
      return false if type == "Watchdog"
      if ack
        return false if item[:message].original && item[:message].original.type == "Watchdog"
      end
    end
    return false if ack && @settings["acknowledgements"] == false && 
      [:not_acknowledged,:warning,:error].include?(item[:level]) == false
  end
  true
end

#setup_output_destinationObject


36
37
38
39
40
41
42
43
44
# File 'lib/rsmp/logger.rb', line 36

def setup_output_destination
  if @settings['stream']
    @stream = @settings['stream']
  elsif @settings['path']
    @stream = File.open(@settings['path'],'a')  # appending
  else
    @stream = $stdout
  end
end

#unmute(ip, port) ⇒ Object


50
51
52
# File 'lib/rsmp/logger.rb', line 50

def unmute ip, port
  @muted.delete "#{ip}:#{port}"
end

#unmute_allObject


54
55
56
# File 'lib/rsmp/logger.rb', line 54

def unmute_all
  @muted = {}
end