forked from mchck/programmer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.rb
48 lines (41 loc) · 956 Bytes
/
log.rb
1
2
3
4
5
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
class Log
@@levels = Hash.new(0)
def self.level(*lvls)
case lvls.count
when 2
sub = lvls.shift
@@levels[sub.to_sym] = Integer(lvls.first)
when 1
@@levels.default = Integer(lvls.first)
end
end
def self.[](sub)
@@levels.fetch(sub.to_sym, nil)
end
def self.[]=(sub, lvl)
@@levels[sub.to_sym] = Integer(lvl)
end
def self.log(subsys, level)
if level <= @@levels[subsys]
args = yield
args = [args] unless args.respond_to? :join
spc = " " * (level - 1)
$stderr.puts '%s%s: %s' % [spc, subsys.to_s.upcase, args.join(' ')]
end
end
def self.hexary(ary)
ary = [ary] unless ary.respond_to? :each
ary.map do |e|
"%08x" % e
end.join(", ")
end
if ENV["DEBUG"]
ENV["DEBUG"].split(/[,;]/).each do |d|
lvls = d.split(/[:=]/, 2)
self.level(*lvls)
end
end
end
def Log(subsys, level, &block)
Log.log(subsys, level, &block)
end