Module: Bio::Log::CLI

Defined in:
lib/bio/log/loggercli.rb

Class Method Summary collapse

Class Method Details

.configure(logname = nil) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/bio/log/loggercli.rb', line 75

def CLI::configure logname = nil
  include Bio::Log

  type = LoggerPlusGlobal.instance.outputter_type
  trace = LoggerPlusGlobal.instance.trace
  trace ||= {}
  default = {}
  default = trace[:default] if trace[:default]
  trace[logname] ||= {} if logname
  trace.each do | name, opts |
    # p [name, opts]
    next if name == :default
    logger_type = type
    logger_type = default[:outputter_name] if default[:outputter_name]
    logger_type = opts[:outputter_name] if opts[:outputter_name]
    logger = LoggerPlus[name]
    raise "Unknown logger <#{name}>" if logger == nil
    logger.outputters = 
      case logger_type
        when 'stderr', :stderr then logger.outputters = Outputter.stderr
        when nil, 'stdout', :stdout then logger.outputters = Outputter.stdout
        else 
          # p [name, logger_type]
          FileOutputter.new(name, logger_type[:file])
      end
    set_levels(logger, default) if default
    set_levels(logger, opts)
    filter = default[:filter]
    filter = opts[:filter] if opts[:filter]
    if filter
      # p filter
      filter2 = "logger.filter { |level,sub_level,msg| #{filter} }" 
      # p filter2
      eval(filter2)
    end
  end
end

.logger(name) ⇒ Object

Parse and store global logger type



16
17
18
19
20
21
22
23
# File 'lib/bio/log/loggercli.rb', line 16

def CLI::logger name
  LoggerPlusGlobal.instance.outputter_type = case name
    when 'stderr' then :stderr
    when 'stdout' then :stdout
    else 
      {:file => { :filename => name }}
  end
end

.trace(s) ⇒ Object

Parse and store trace options



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
# File 'lib/bio/log/loggercli.rb', line 26

def CLI::trace s
  level = nil
  sub_level = nil
  filter = nil

  opts = {}
  # ---- split fields
  a = s.split(':')
  if a.last =~ /^=(\d+)$/
    # ---- set exact filter
    filter = "sub_level==#{$1}"
    a.pop
  elsif a.last =~ /^=/
    # ---- set filter
    filter = $'
    a.pop
  elsif a.last =~ /^\d+$/
    # ---- the last field is the sub level
    sub_level = a.pop.to_i
    # ---- The fore-last field is the level
    level = a.pop.downcase
  else
    level = a.pop.downcase
  end
  # ---- If there is another field it contains logger name(s)
  #      otherwise it is a global
  outputter = 
    if a.size == 2 
      a.shift
    end
  if a.size == 0
    a = [:default]  # global
  else
    a = a[0].split(',') # one or more logger name(s)
  end
  # ---- update every logger
  a.each do | type |
    opts[type] = {}
    opts[type][:level] = level if level
    opts[type][:sub_level] = sub_level if sub_level
    opts[type][:filter] = filter if filter
    opts[type][:outputter_name] = outputter if outputter
    # p [type,opts[type]]
  end
  # ---- Set the globals
  LoggerPlusGlobal.instance.trace ||= {}
  LoggerPlusGlobal.instance.trace = LoggerPlusGlobal.instance.trace.merge(opts)
end