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

MockMutex.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

.logfile(file = nil) ⇒ Object

Returns the value of attribute logfile.



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

def logfile
  @logfile
end

.nocolorObject

Returns the value of attribute nocolor.



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

def nocolor
  @nocolor
end

.severityObject

Returns the value of attribute severity.



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

def severity
  @severity
end

.tty_sizeObject

Returns the value of attribute tty_size.



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

def tty_size
  @tty_size
end

Class Method Details

._ignore_stderrObject



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/rbbt/util/log.rb', line 110

def self._ignore_stderr
  begin
    File.open('/dev/null', 'w') do |f|
      backup_stderr = STDERR.dup
      STDERR.reopen(f)
      begin
        yield
      ensure
        STDERR.reopen backup_stderr
        backup_stderr.close
      end
    end
  rescue Errno::ENOENT
    yield
  end
end

._ignore_stdoutObject



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

def self._ignore_stdout
  begin
    File.open('/dev/null', 'w') do |f|
      backup_stdout = STDOUT.dup
      STDOUT.reopen(f)
      begin
        yield
      ensure
        STDOUT.reopen backup_stdout
        backup_stdout.close
      end
    end
  rescue Errno::ENOENT
    yield
  end
end

.clear_line(out = STDOUT) ⇒ Object



250
251
252
# File 'lib/rbbt/util/log.rb', line 250

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



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

def self.color(severity, str = nil, reset = false)
  return str.dup || "" if nocolor 
  color = reset ? Term::ANSIColor.reset : ""
  color << SEVERITY_COLOR[severity] if Integer === 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



368
369
370
371
372
373
374
375
# File 'lib/rbbt/util/log.rb', line 368

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 unless stack.nil?
end

.compactObject



31
32
33
# File 'lib/rbbt/util/log.rb', line 31

def self.compact
  true
end

.count_stackObject



424
425
426
427
428
429
430
431
432
433
434
435
436
# File 'lib/rbbt/util/log.rb', line 424

def self.count_stack
  if ! $count_stacks
    Log.debug "Counting stacks at: " << caller.first
    return 
  end
  $stack_counts ||= {}
  head = $count_stacks_head
  stack = caller[1..head+1]
  stack.reverse.each do |line,i|
    $stack_counts[line] ||= 0
    $stack_counts[line] += 1
  end
end

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



323
324
325
# File 'lib/rbbt/util/log.rb', line 323

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

.deprecated(m) ⇒ Object



362
363
364
365
366
# File 'lib/rbbt/util/log.rb', line 362

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

.down_lines(num = 1) ⇒ Object



242
243
244
# File 'lib/rbbt/util/log.rb', line 242

def self.down_lines(num = 1)
  nocolor ? "" : "\033[#{num+1}E"
end

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



347
348
349
# File 'lib/rbbt/util/log.rb', line 347

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

.exception(e) ⇒ Object



351
352
353
354
355
356
357
358
359
360
# File 'lib/rbbt/util/log.rb', line 351

def self.exception(e)
  stack = caller
  if ENV["RBBT_ORIGINAL_STACK"] == 'true'
    error([e.class.to_s, e.message].compact * ": " )
    error("BACKTRACE [#{Process.pid}]: " << Log.last_caller(stack) << "\n" + color_stack(e.backtrace)*"\n")
  else
    error("BACKTRACE [#{Process.pid}]: " << Log.last_caller(stack) << "\n" + color_stack(e.backtrace.reverse)*"\n")
    error([e.class.to_s, e.message].compact * ": " )
  end
end

.get_level(level) ⇒ Object



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

def self.get_level(level)
  case level
  when Numeric
    level.to_i
  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



335
336
337
# File 'lib/rbbt/util/log.rb', line 335

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

.highlight(str = nil) ⇒ Object



254
255
256
257
258
259
260
261
262
# File 'lib/rbbt/util/log.rb', line 254

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

.ignore_stderr(&block) ⇒ Object



128
129
130
131
132
# File 'lib/rbbt/util/log.rb', line 128

def self.ignore_stderr(&block)
  LOG_MUTEX.synchronize do
    _ignore_stderr &block
  end
end

.ignore_stdout(&block) ⇒ Object



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

def self.ignore_stdout(&block)
  LOG_MUTEX.synchronize do
    _ignore_stdout &block
  end
end

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



339
340
341
# File 'lib/rbbt/util/log.rb', line 339

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

.last_caller(stack) ⇒ Object



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

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
  line.gsub('`', "'")
end

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



289
290
291
292
293
294
295
# File 'lib/rbbt/util/log.rb', line 289

def self.log(message = nil, severity = MEDIUM, &block)
  return if severity < self.severity 
  message ||= block.call if block_given?
  return if message.nil?
  message = message + "\n" unless message[-1] == "\n"
  self.logn message, severity, &block
end

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



310
311
312
313
314
315
316
317
318
319
320
321
# File 'lib/rbbt/util/log.rb', line 310

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



297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/rbbt/util/log.rb', line 297

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

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



265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
# File 'lib/rbbt/util/log.rb', line 265

def self.logn(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.to_s

  LOG_MUTEX.synchronize do
    if logfile.nil?
      STDERR.write str
    else
      logfile.write str 
    end
    Log::LAST.replace "log"
    nil
  end
end

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



327
328
329
# File 'lib/rbbt/util/log.rb', line 327

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

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



331
332
333
# File 'lib/rbbt/util/log.rb', line 331

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

.no_barObject



10
11
12
13
# File 'lib/rbbt/util/log/progress.rb', line 10

def self.no_bar
  @@no_bar = false unless defined?(@@no_bar)
  (@@no_bar || ENV["RBBT_NO_PROGRESS"]).to_s == "true"
end

.no_bar=(value) ⇒ Object



6
7
8
# File 'lib/rbbt/util/log/progress.rb', line 6

def self.no_bar=(value)
  @@no_bar = value
end

.reset_colorObject



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

def self.reset_color
  reset
end

.return_lineObject



246
247
248
# File 'lib/rbbt/util/log.rb', line 246

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

.stack(stack) ⇒ Object



408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
# File 'lib/rbbt/util/log.rb', line 408

def self.stack(stack)
  LOG_MUTEX.synchronize do
    if ENV["RBBT_ORIGINAL_STACK"] == 'true'
      STDERR.puts Log.color :magenta, "Stack trace [#{Process.pid}]: " << Log.last_caller(caller)
    color_stack(stack).each do |line|
      STDERR.puts line
    end
    else
      STDERR.puts Log.color :magenta, "Stack trace [#{Process.pid}]: " << Log.last_caller(caller)
      color_stack(stack.reverse).each do |line|
        STDERR.puts line
      end
    end
  end
end

.terminal_widthObject



27
28
29
# File 'lib/rbbt/util/log.rb', line 27

def self.terminal_width
  80
end

.trap_std(msg = "STDOUT", msge = "STDERR", severity = 0, severity_err = nil) ⇒ Object



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
74
75
76
77
78
79
80
81
82
83
# File 'lib/rbbt/util/log.rb', line 47

def self.trap_std(msg = "STDOUT", msge = "STDERR", severity = 0, severity_err = nil)
  sout, sin = Misc.pipe
  soute, sine = Misc.pipe
  backup_stderr = STDERR.dup
  backup_stdout = STDOUT.dup
  old_logfile = Log.logfile
  Log.logfile(backup_stderr)

  severity_err ||= severity
  th_log = Thread.new do
    while line = sout.gets
      Log.logn "#{msg}: " + line, severity
    end
  end

  th_loge = Thread.new do
    while line = soute.gets
      Log.logn "#{msge}: " + line, severity_err
    end
  end

  begin
    STDOUT.reopen(sin)
    STDERR.reopen(sine)
    yield
  ensure
    STDERR.reopen backup_stderr
    STDOUT.reopen backup_stdout
    sin.close
    sine.close
    th_log.join
    th_loge.join
    backup_stdout.close
    backup_stderr.close
    Log.logfile = old_logfile
  end
end

.trap_stderr(msg = "STDERR", severity = 0) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/rbbt/util/log.rb', line 85

def self.trap_stderr(msg = "STDERR", severity = 0)
  sout, sin = Misc.pipe
  backup_stderr = STDERR.dup
  old_logfile = Log.logfile
  Log.logfile(backup_stderr)

  th_log = Thread.new do
    while line = sout.gets
      Log.logn "#{msg}: " + line, severity
    end
  end

  begin
    STDERR.reopen(sin)
    yield
    sin.close
  ensure
    STDERR.reopen backup_stderr
    th_log.join
    backup_stderr.close
    Log.logfile = old_logfile
  end
end

.tsv(tsv, example = false) ⇒ Object



377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
# File 'lib/rbbt/util/log.rb', line 377

def self.tsv(tsv, example = false)
  STDERR.puts Log.color :magenta, "TSV log: " << Log.last_caller(caller).gsub('`',"'")
  STDERR.puts Log.color(:blue, "=> "<< Misc.fingerprint(tsv), true) 
  STDERR.puts Log.color(:cyan, "=> " << tsv.summary)
  if example && ! tsv.empty?
    key = case example
          when TrueClass, :first, "first"
            tsv.keys.first
          when :random, "random"
            tsv.keys.shuffle.first
          else
            example
          end

    values = tsv[key]
    values = [values] if tsv.type == :flat || tsv.type == :single
    if values.nil?
      STDERR.puts Log.color(:blue, "Key (#{tsv.key_field}) not present: ") + key
    else
      STDERR.puts Log.color(:blue, "Key (#{tsv.key_field}): ") + key
      tsv.fields.zip(values).each do |field,value|
        STDERR.puts Log.color(:magenta, field + ": ") + (Array === value ? value * ", " : value.to_s)
      end
    end
  end
  LOG_MUTEX.synchronize do
    Log::LAST.replace "log"
  end
  nil
end

.uncolor(str) ⇒ Object



218
219
220
# File 'lib/rbbt/util/log.rb', line 218

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

.up_lines(num = 1) ⇒ Object



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

def self.up_lines(num = 1)
  nocolor ? "" : "\033[#{num+1}F\033[2K"
end

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



343
344
345
# File 'lib/rbbt/util/log.rb', line 343

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

.with_severity(level) ⇒ Object



188
189
190
191
192
193
194
195
196
# File 'lib/rbbt/util/log.rb', line 188

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

.with_stack_counts(head = 10, total = 100) ⇒ Object



438
439
440
441
442
443
444
445
446
447
# File 'lib/rbbt/util/log.rb', line 438

def self.with_stack_counts(head = 10, total = 100)
  $count_stacks_head = head
  $count_stacks = true
  $stack_counts = {}
  res = yield
  $count_stacks = false
  Log.debug "STACK_COUNTS:\n" + $stack_counts.sort_by{|line,c| c}.reverse.collect{|line,c| [c, line] * " - "}[0..total] * "\n"
  $stack_counts = {}
  res
end