Module: Log

Extended by:
Term::ANSIColor
Defined in:
lib/rbbt/util/log.rb,
lib/rbbt/util/log/progress.rb,
lib/rbbt/util/log/progress/util.rb,
lib/rbbt/util/log/progress/report.rb

Defined Under Namespace

Classes: ProgressBar

Constant Summary collapse

SEVERITY_COLOR =

.collect{|e| “033[#{e}”}

[reset, cyan, green, magenta, blue, yellow, red]
HIGHLIGHT =
"\033[1m"
LOG_MUTEX =
Mutex.new
LAST =
"log"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.logfileObject

Returns the value of attribute logfile.



33
34
35
# File 'lib/rbbt/util/log.rb', line 33

def logfile
  @logfile
end

.nocolorObject

Returns the value of attribute nocolor.



33
34
35
# File 'lib/rbbt/util/log.rb', line 33

def nocolor
  @nocolor
end

.severityObject

Returns the value of attribute severity.



33
34
35
# File 'lib/rbbt/util/log.rb', line 33

def severity
  @severity
end

.tty_sizeObject

Returns the value of attribute tty_size.



33
34
35
# File 'lib/rbbt/util/log.rb', line 33

def tty_size
  @tty_size
end

Class Method Details

.clear_line(out = STDOUT) ⇒ Object



82
83
84
# File 'lib/rbbt/util/log.rb', line 82

def self.clear_line(out = STDOUT)
  out.puts Log.return_line << " " * (Log.tty_size || 80) << Log.return_line unless nocolor
end

.color(severity, str = nil, reset = false) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/rbbt/util/log.rb', line 66

def self.color(severity, str = nil, reset = false)
  return str || "" if nocolor 
  color = reset ? Term::ANSIColor.reset : ""
  color << SEVERITY_COLOR[severity] if Fixnum === severity
  color << Term::ANSIColor.send(severity) if Symbol === severity and Term::ANSIColor.respond_to? severity 
  if str.nil?
    color
  else
    color + str.to_s + self.color(0)
  end
end

.debug(message = nil, &block) ⇒ Object



153
154
155
# File 'lib/rbbt/util/log.rb', line 153

def self.debug(message = nil, &block)
  log(message, DEBUG, &block)
end

.error(message = nil, &block) ⇒ Object



177
178
179
# File 'lib/rbbt/util/log.rb', line 177

def self.error(message = nil, &block)
  log(message, ERROR, &block)
end

.exception(e) ⇒ Object



181
182
183
184
185
186
# File 'lib/rbbt/util/log.rb', line 181

def self.exception(e)
  stack = caller
  error("#{Log.color :error, "EXCEPTION:"} " << stack.first)
  error([e.class.to_s, e.message].compact * ": ")
  error("BACKTRACE:\n" + e.backtrace * "\n") 
end

.get_level(level) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rbbt/util/log.rb', line 17

def self.get_level(level)
  case level
  when Fixnum
    level
  when String
    begin
      Log.const_get(level.upcase)
    rescue
      Log.exception $!
    end
  when Symbol
    get_level(level.to_s)
  end || 0
end

.high(message = nil, &block) ⇒ Object



165
166
167
# File 'lib/rbbt/util/log.rb', line 165

def self.high(message = nil, &block)
  log(message, HIGH, &block)
end

.highlight(str = nil) ⇒ Object



86
87
88
89
90
91
92
93
94
# File 'lib/rbbt/util/log.rb', line 86

def self.highlight(str = nil)
  if str.nil?
    return "" if nocolor
    HIGHLIGHT
  else
    return str if nocolor
    HIGHLIGHT + str + color(0)
  end
end

.info(message = nil, &block) ⇒ Object



169
170
171
# File 'lib/rbbt/util/log.rb', line 169

def self.info(message = nil, &block)
  log(message, INFO, &block)
end

.log(message = nil, severity = MEDIUM, &block) ⇒ Object



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/rbbt/util/log.rb', line 98

def self.log(message = nil, severity = MEDIUM, &block)
  return if severity < self.severity 
  message ||= block.call if block_given?
  return if message.nil?

  time = Time.now.strftime("%m/%d/%y-%H:%M:%S")

  sev_str = severity.to_s

  prefix = time << "[" << color(severity) << sev_str << color(0)<<"]"
  message = "" << highlight << message << color(0) if severity >= INFO
  str = prefix << " " << message

  LOG_MUTEX.synchronize do
    STDERR.puts str
    Log::LAST.replace "log"
    logfile.puts str unless logfile.nil?
    nil
  end
end

.log_obj_fingerprint(obj, level, file = $stdout) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/rbbt/util/log.rb', line 136

def self.log_obj_fingerprint(obj, level, file = $stdout)
  stack = caller

  line = nil
  while line.nil? or line =~ /util\/log\.rb/ and stack.any?
    line = stack.shift 
  end
  line ||= caller.first

  level = Log.get_level level
  name = Log::SEVERITY_NAMES[level] + ": "
  Log.log Log.color(level, name, true) << line, level
  Log.log "", level
  Log.log Log.color(level, "=> ", true) << Misc.fingerprint(obj), level
  Log.log "", level
end

.log_obj_inspect(obj, level, file = $stdout) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/rbbt/util/log.rb', line 119

def self.log_obj_inspect(obj, level, file = $stdout)
  stack = caller

  line = nil
  while line.nil? or line =~ /util\/log\.rb/ and stack.any?
    line = stack.shift 
  end
  line ||= caller.first

  level = Log.get_level level
  name = Log::SEVERITY_NAMES[level] + ": "
  Log.log Log.color(level, name, true) << line, level
  Log.log "", level
  Log.log Log.color(level, "=> ", true) << obj.inspect, level
  Log.log "", level
end

.low(message = nil, &block) ⇒ Object



157
158
159
# File 'lib/rbbt/util/log.rb', line 157

def self.low(message = nil, &block)
  log(message, LOW, &block)
end

.medium(message = nil, &block) ⇒ Object



161
162
163
# File 'lib/rbbt/util/log.rb', line 161

def self.medium(message = nil, &block)
  log(message, MEDIUM, &block)
end

.reset_colorObject



62
63
64
# File 'lib/rbbt/util/log.rb', line 62

def self.reset_color
  reset
end

.return_lineObject



78
79
80
# File 'lib/rbbt/util/log.rb', line 78

def self.return_line
  nocolor ? "" : "\033[1A"
end

.uncolor(str) ⇒ Object



58
59
60
# File 'lib/rbbt/util/log.rb', line 58

def self.uncolor(str)
  Term::ANSIColor.uncolor(str)
end

.warn(message = nil, &block) ⇒ Object



173
174
175
# File 'lib/rbbt/util/log.rb', line 173

def self.warn(message = nil, &block)
  log(message, WARN, &block)
end

.with_severity(level) ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'lib/rbbt/util/log.rb', line 39

def self.with_severity(level)
  orig = Log.severity
  begin
    Log.severity = level
    yield
  ensure
    Log.severity = orig
  end
end