Class: Uttk::Logger

Inherits:
Object show all
Includes:
Observable
Defined in:
lib/uttk/logger.rb,
lib/uttk/logger/path.rb,
lib/uttk/logger/backend.rb,
lib/uttk/logger/severity.rb,
lib/uttk/logger/verbosity.rb,
lib/uttk/logger/to_uttk_log.rb,
lib/uttk/logger/section_node.rb

Defined Under Namespace

Modules: Severity, ToUttkLog Classes: Backend, BadMethodFormat, DuplicatedPath, Path, SectionNode, Segment, UnsupportedMessage, Upper, Verbosity

Constant Summary collapse

OPTIONS =
[ :ordered, :type ]
MESSAGES =
[ :new_leaf, :close ]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*observers) ⇒ Logger

Returns a new instance of Logger.



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/uttk/logger.rb', line 41

def initialize ( *observers )
  @path = Logger::Path.new
  observers.flatten.each { |obs| add_observer(obs) }
  @sections = []
  @section_tree = SectionNode.new('all')
  @severity_level = Severity::DEBUG
  @nb_log_msg = 0
  @verbosity = Verbosity.new
  @verbosity_level = 0
  @closed = false
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(key, *a, &b) ⇒ Object



220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/uttk/logger.rb', line 220

def method_missing ( key, *a, &b )
  str = key.to_s
  if str =~ /^(.*)=$/
    self[$1.to_sym] = a[0]
  elsif b
    begin
      args = make_log_arguments(str)
      args += a
      self.log(*args, &b)
    rescue BadMethodFormat
      super
    end
  else
    super
  end
end

Instance Attribute Details

#section_treeObject

Returns the value of attribute section_tree.



75
76
77
# File 'lib/uttk/logger.rb', line 75

def section_tree
  @section_tree
end

#severity_levelObject

Returns the value of attribute severity_level.



62
63
64
# File 'lib/uttk/logger.rb', line 62

def severity_level
  @severity_level
end

#verbosityObject

Returns the value of attribute verbosity.



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

def verbosity
  @verbosity
end

#verbosity_levelObject

Returns the value of attribute verbosity_level.



96
97
98
# File 'lib/uttk/logger.rb', line 96

def verbosity_level
  @verbosity_level
end

Class Method Details

.make_notif(n) ⇒ Object



291
292
293
294
295
296
297
# File 'lib/uttk/logger.rb', line 291

def self.make_notif ( n )
  if n.size == 3
    n
  else
    [:new_leaf, n[0].to_logger_path, n[1]]
  end
end

.make_notifs(ns) ⇒ Object



299
300
301
# File 'lib/uttk/logger.rb', line 299

def self.make_notifs ( ns )
  ns.map { |n| make_notif(n) }
end

Instance Method Details

#<<(v) ⇒ Object



213
214
215
# File 'lib/uttk/logger.rb', line 213

def << ( v )
  v.to_uttk_log(self)
end

#[]=(k, v) ⇒ Object



209
210
211
# File 'lib/uttk/logger.rb', line 209

def []= ( k, v )
  v.to_uttk_log_with_key(k, self)
end

#active_section(active, *section_names) ⇒ Object



77
78
79
80
# File 'lib/uttk/logger.rb', line 77

def active_section(active, *section_names)
  @sections = @section_tree.set_active_section(active, *section_names)
  nil
end

#active_section?(section_name) ⇒ Boolean

Returns:

  • (Boolean)


82
83
84
# File 'lib/uttk/logger.rb', line 82

def active_section?(section_name)
  @sections.include?(section_name)
end

#chpath(path) ⇒ Object



58
59
60
# File 'lib/uttk/logger.rb', line 58

def chpath ( path )
  @path = path.dup
end

#closeObject



284
285
286
287
288
289
# File 'lib/uttk/logger.rb', line 284

def close
  return if @closed
  @path = Logger::Path.new unless @path.empty?
  notif :close
  @closed = true
end

#initialize_copy(o) ⇒ Object



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

def initialize_copy ( o )
  super # FIXME share less information
  @path = o.path
end

#log(severity_level = nil, *section_names, &block) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/uttk/logger.rb', line 107

def log(severity_level=nil, *section_names, &block)
  severity_level ||= Severity.higher
  return true if severity_level < @severity_level
  if section_names.empty?
    format_log_message(severity_level, section_names, &block)
  else
    section_names.each do |s|
      if @sections.include?(s)
        format_log_message(severity_level, section_names, &block)
        break
      end
    end
  end
end

#make_log_arguments(str) ⇒ Object



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
# File 'lib/uttk/logger.rb', line 237

def make_log_arguments(str)
  if str =~ /^([A-Za-z0-9]+)(.*)$/
    severity, tail = $1, $2
    args = []
    begin
      args << Severity.const_get(severity.upcase)
    rescue NameError
      raise(NameError, "`#{severity}' - unknown severity level")
    end
    while tail =~ /^_([A-Za-z0-9]+)(.*)$/
      args << $1
      tail = $2
    end
    args
  else
    raise(BadMethodFormat, "`#{str}' - bad format")
  end
end

#new_leaf(x) ⇒ Object



205
206
207
# File 'lib/uttk/logger.rb', line 205

def new_leaf ( x )
  notif :new_leaf, @path.dup, x
end

#new_node(node, options = nil, &block) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/uttk/logger.rb', line 159

def new_node ( node, options=nil, &block )
  unless options.nil?
    options.each_key do |k|
      unless OPTIONS.include? k
        raise ArgumentError, "bad option: #{k}"
      end
    end
  end
  unless node.is_a? Symbol or node.is_a? Integer or node == ''
    node = node.to_s.to_sym
  end

  if block
    up_path = @path.dup
  else
    result = upper
  end

  begin
    @path << Segment.new(node, options)
  rescue Exception => ex
    if block
      secret_up up_path
    else
      result.up
    end
    raise ex
  end

  if block
    begin
      block[]
    ensure
      @path = up_path
    end
  else
    result
  end
end

#pathObject



256
257
258
# File 'lib/uttk/logger.rb', line 256

def path
  @path.dup
end

#path_sizeObject



260
261
262
# File 'lib/uttk/logger.rb', line 260

def path_size
  @path.size
end

#to_sObject Also known as: inspect



264
265
266
# File 'lib/uttk/logger.rb', line 264

def to_s
  "#<#{self.class} path=#{path}>"
end

#update(msg, *args) ⇒ Object

r = Logger.new r.new_node(:root) r.update(:new_node, [:another_path], :foo) # it’s now correct



273
274
275
# File 'lib/uttk/logger.rb', line 273

def update ( msg, *args )
  notif(msg, *args) if MESSAGES.include? msg
end

#upperObject



199
200
201
202
203
# File 'lib/uttk/logger.rb', line 199

def upper
  # The local variable up_path is very important !!!
  up_path = @path.dup
  Upper.new { @path = up_path }
end