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

LOG_MUTEX =

ToDo: Iā€™m not sure if using a Mutex here really gives troubles in CPU concurrency LOG_MUTEX = MockMutex.new

Mutex.new
SEVERITY_COLOR =

.collect{|e| ā€œ033[#{e}ā€}

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

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.logfileObject

Returns the value of attribute logfile.



67
68
69
# File 'lib/rbbt/util/log.rb', line 67

def logfile
  @logfile
end

.nocolorObject

Returns the value of attribute nocolor.



67
68
69
# File 'lib/rbbt/util/log.rb', line 67

def nocolor
  @nocolor
end

.severityObject

Returns the value of attribute severity.



67
68
69
# File 'lib/rbbt/util/log.rb', line 67

def severity
  @severity
end

.tty_sizeObject

Returns the value of attribute tty_size.



67
68
69
# File 'lib/rbbt/util/log.rb', line 67

def tty_size
  @tty_size
end

Class Method Details

.clear_line(out = STDOUT) ⇒ Object



125
126
127
# File 'lib/rbbt/util/log.rb', line 125

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



109
110
111
112
113
114
115
116
117
118
119
# File 'lib/rbbt/util/log.rb', line 109

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

.color_stack(stack) ⇒ Object



227
228
229
230
231
232
233
234
# File 'lib/rbbt/util/log.rb', line 227

def self.color_stack(stack)
  stack.collect do |line|
    line = line.sub('`',"'")
    color = :green if line =~ /workflow/
    color = :blue if line =~ /rbbt-/
    Log.color color, line
  end
end

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



187
188
189
# File 'lib/rbbt/util/log.rb', line 187

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

.deprecated(m) ⇒ Object



221
222
223
224
225
# File 'lib/rbbt/util/log.rb', line 221

def self.deprecated(m)
  stack = caller
  warn("DEPRECATED: " << Log.last_caller(caller))
  warn("* " << (m || "").to_s)
end

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



211
212
213
# File 'lib/rbbt/util/log.rb', line 211

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

.exception(e) ⇒ Object



215
216
217
218
219
# File 'lib/rbbt/util/log.rb', line 215

def self.exception(e)
  stack = caller
  error([e.class.to_s, e.message].compact * ": " )
  error("BACKTRACE: " << Log.last_caller(caller) << "\n" + color_stack(e.backtrace)*"\n")
end

.get_level(level) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/rbbt/util/log.rb', line 51

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



199
200
201
# File 'lib/rbbt/util/log.rb', line 199

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

.highlight(str = nil) ⇒ Object



129
130
131
132
133
134
135
136
137
# File 'lib/rbbt/util/log.rb', line 129

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

.ignore_stderrObject



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

def self.ignore_stderr
  LOG_MUTEX.synchronize do
    backup_stderr = STDERR.dup
    File.open('/dev/null', 'w') do |f|
      STDERR.reopen(f)
      begin
        yield
      ensure
        STDERR.reopen backup_stderr
        backup_stderr.close
      end
    end
  end
end

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



203
204
205
# File 'lib/rbbt/util/log.rb', line 203

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

.last_caller(stack) ⇒ Object



27
28
29
30
31
32
33
34
# File 'lib/rbbt/util/log.rb', line 27

def self.last_caller(stack)
  line = nil
  pos ||= 0
  while line.nil? or line =~ /util\/log\.rb/ and stack.any? 
    line = stack.shift 
  end
  line ||= caller.first
end

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



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/rbbt/util/log.rb', line 140

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.%L")

  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



174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/rbbt/util/log.rb', line 174

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

  line = Log.last_caller stack

  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



161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/rbbt/util/log.rb', line 161

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

  line = Log.last_caller stack

  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



191
192
193
# File 'lib/rbbt/util/log.rb', line 191

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

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



195
196
197
# File 'lib/rbbt/util/log.rb', line 195

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

.reset_colorObject



105
106
107
# File 'lib/rbbt/util/log.rb', line 105

def self.reset_color
  reset
end

.return_lineObject



121
122
123
# File 'lib/rbbt/util/log.rb', line 121

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

.stack(stack) ⇒ Object



242
243
244
245
246
247
248
249
250
# File 'lib/rbbt/util/log.rb', line 242

def self.stack(stack)
  LOG_MUTEX.synchronize do

    STDERR.puts Log.color :magenta, "Stack trace: " << Log.last_caller(caller)
    color_stack(stack).each do |line|
      STDERR.puts line
    end
  end
end

.tsv(tsv) ⇒ Object



236
237
238
239
240
# File 'lib/rbbt/util/log.rb', line 236

def self.tsv(tsv)
  STDERR.puts Log.color :magenta, "TSV log: " << Log.last_caller(caller)
  puts Log.color(:blue, "=> "<< Misc.fingerprint(tsv), true) 
  puts Log.color(:cyan, "=> " << tsv.summary)
end

.uncolor(str) ⇒ Object



101
102
103
# File 'lib/rbbt/util/log.rb', line 101

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

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



207
208
209
# File 'lib/rbbt/util/log.rb', line 207

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

.with_severity(level) ⇒ Object



82
83
84
85
86
87
88
89
90
# File 'lib/rbbt/util/log.rb', line 82

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