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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/rsmp/logger.rb', line 6

def initialize settings={}
  defaults = {
    'active'=>true,
    'path'=>nil,
    'stream'=>nil,
    'color'=>true,
    'debug'=>false,
    'statistics'=>false,
    'hide_ip_and_port' => false,
    'acknowledgements' => false,
    'watchdogs' => false,
    'alarms' => true,
    'json'=>false,
    'tabs'=>'-',

    'prefix'=>false,
    'index'=>false,
    'author'=>false,
    'timestamp'=>true,
    'ip'=>false,
    'port'=>false,
    'site_id'=>true,
    'component'=>true,
    'direction'=>false,
    'level'=>false,
    'id'=>true,
    'text'=>true,
  }

  default_lengths = {
    'index'=>7,
    'author'=>13,
    'timestamp'=>24,
    'ip'=>22,
    'port'=>5,
    'site_id'=>19,
    'component'=>19,
    'direction'=>3,
    'level'=>7,
    'id'=>4,
  }

  @ignorable = {
    'versions' => ['Version'],
    'statuses' => ['StatusRequest','StatusSubscribe','StatusUnsubscribe','StatusResponse','StatusUpdate'],
    'commands' => ['CommandRequest','CommandResponse'],
    'watchdogs' => 'Watchdog',
    'alarms' => ['Alarm'],
    'aggregated_status' => ['AggregatedStatus','AggregatedStatusRequest']
  }

  if settings
    @settings = defaults.merge settings
  else
    @settings = defaults
  end

  # copy default length for items that are set to true
  @settings = @settings.map do |key,value|
    if value == true && default_lengths[key]
      [ key, default_lengths[key] ]
    else
      [ key, value ]
    end
  end.to_h

  @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



166
167
168
169
170
171
172
# File 'lib/rsmp/logger.rb', line 166

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



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/rsmp/logger.rb', line 197

def build_output item
  parts = []
  build_part( parts, item, :prefix ) { @settings['prefix'] if @settings['prefix'] != false}
  build_part( parts, item, :index )
  build_part( parts, item, :author )
  build_part( parts, item, :timestamp ) { |part| Clock.to_s part }
  build_part( parts, item, :ip )
  build_part( parts, item, :port )
  build_part( parts, item, :site_id )
  build_part( parts, item, :component )
  build_part( parts, item, :direction ) { |part| {in:"In",out:"Out"}[part] }
  build_part( parts, item, :level ) { |part| part.capitalize }
  build_part( parts, item, :id ) { Logger.shorten_message_id(item[:message].m_id,4) if item[:message] }
  build_part( parts, item, :text )
  build_part( parts, item, :json ) { item[:message].json if item[:message] }
  build_part( parts, item, :exception ) { |e| [e.class,e.backtrace].flatten.join("\n") }

  parts.join('  ').chomp(@settings['tabs'].to_s).rstrip
end

#build_part(parts, item, key, &block) ⇒ Object



183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/rsmp/logger.rb', line 183

def build_part parts, item, key, &block
  skey = key.to_s
  return unless @settings[skey]

  part = item[key]
  part = yield part if block
  part = part.to_s
  part = part.ljust @settings[skey] if @settings[skey].is_a?(Integer)

  # replace the first char with a dash if string is all whitespace
  part = @settings['tabs'].ljust(part.length) if @settings['tabs'] && part !~ /\S/
  parts << part
end

#colorize(level, str) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/rsmp/logger.rb', line 131

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



174
175
176
177
178
179
180
181
# File 'lib/rsmp/logger.rb', line 174

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



160
161
162
163
164
# File 'lib/rsmp/logger.rb', line 160

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

#mute(ip, port) ⇒ Object



86
87
88
# File 'lib/rsmp/logger.rb', line 86

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

#output(level, str) ⇒ Object



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

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)


98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/rsmp/logger.rb', line 98

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")
    @ignorable.each_pair do |key,types|
      ignore = [types].flatten
      if @settings[key] == false
        return false if ignore.include?(type)
        if ack
          return false if item[:message].original && ignore.include?(item[:message].original.type)
        end
      end
    end
    return false if ack && @settings["acknowledgements"] == false &&
      [:not_acknowledged,:warning,:error].include?(item[:level]) == false
  end
  true
end

#setup_output_destinationObject



76
77
78
79
80
81
82
83
84
# File 'lib/rsmp/logger.rb', line 76

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



90
91
92
# File 'lib/rsmp/logger.rb', line 90

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

#unmute_allObject



94
95
96
# File 'lib/rsmp/logger.rb', line 94

def unmute_all
  @muted = {}
end