Class: TTK::Logger

Inherits:
Object show all
Includes:
Observable
Defined in:
lib/ttk/logger.rb,
lib/ttk/logger/path.rb,
lib/ttk/logger/severity.rb,
lib/ttk/logger/verbosity.rb,
lib/ttk/logger/to_ttk_log.rb,
lib/ttk/logger/section_node.rb

Defined Under Namespace

Modules: Severity, ToTTKLog Classes: Path, SectionNode, Verbosity

Constant Summary collapse

TYPES =
[ :map, :seq ]
MESSAGES =
[ :new_node, :new_leaf, :up, :close ]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*observers) ⇒ Logger

Returns a new instance of Logger.



26
27
28
29
30
31
32
33
34
35
# File 'lib/ttk/logger.rb', line 26

def initialize ( *observers )
  @path = []
  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
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/ttk/logger.rb', line 160

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

Instance Attribute Details

#section_treeObject

Returns the value of attribute section_tree.



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

def section_tree
  @section_tree
end

#severity_levelObject

Returns the value of attribute severity_level.



37
38
39
# File 'lib/ttk/logger.rb', line 37

def severity_level
  @severity_level
end

#verbosityObject

Returns the value of attribute verbosity.



61
62
63
# File 'lib/ttk/logger.rb', line 61

def verbosity
  @verbosity
end

#verbosity_levelObject

Returns the value of attribute verbosity_level.



71
72
73
# File 'lib/ttk/logger.rb', line 71

def verbosity_level
  @verbosity_level
end

Instance Method Details

#<<(v) ⇒ Object



156
157
158
# File 'lib/ttk/logger.rb', line 156

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

#[]=(k, v) ⇒ Object



150
151
152
153
154
# File 'lib/ttk/logger.rb', line 150

def []= ( k, v )
  new_node(k) do
    v.to_ttk_log(self)
  end
end

#active_section(active, *section_names) ⇒ Object



52
53
54
55
# File 'lib/ttk/logger.rb', line 52

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

#active_section?(section_name) ⇒ Boolean

Returns:

  • (Boolean)


57
58
59
# File 'lib/ttk/logger.rb', line 57

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

#closeObject



224
225
226
# File 'lib/ttk/logger.rb', line 224

def close
  notif(:close)
end

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



82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/ttk/logger.rb', line 82

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



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/ttk/logger.rb', line 177

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

#new_leaf(x) ⇒ Object



146
147
148
# File 'lib/ttk/logger.rb', line 146

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

#new_node(node, type = :map, &block) ⇒ Object

Raises:

  • (ArgumentError)


120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/ttk/logger.rb', line 120

def new_node ( node, type=:map, &block )
  raise ArgumentError, "bad node type: #{type}" unless TYPES.include? type
  node = node.to_s.to_sym unless node.is_a? Symbol or node.is_a? Integer

  begin
    @path << [node, type]
    notif :new_node, @path.dup
  rescue Exception => ex
    up if block_given?
    raise ex
  end

  if block_given?
    begin
      block[]
    ensure
      up
    end
  end
end

#pathObject



194
195
196
# File 'lib/ttk/logger.rb', line 194

def path
  Path.new(@path)
end

#path_sizeObject



198
199
200
# File 'lib/ttk/logger.rb', line 198

def path_size
  @path.size
end

#to_sObject Also known as: inspect



202
203
204
# File 'lib/ttk/logger.rb', line 202

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

#upObject



141
142
143
144
# File 'lib/ttk/logger.rb', line 141

def up
  @path.pop
  notif :up, @path.dup
end

#update(msg, *args) ⇒ Object

FIXME : WARNING

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



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

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