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
-
.logfile(file = nil) ⇒ Object
Returns the value of attribute logfile.
-
.nocolor ⇒ Object
Returns the value of attribute nocolor.
-
.severity ⇒ Object
Returns the value of attribute severity.
-
.tty_size ⇒ Object
Returns the value of attribute tty_size.
Class Method Summary collapse
- ._ignore_stderr ⇒ Object
- .clear_line(out = STDOUT) ⇒ Object
- .color(severity, str = nil, reset = false) ⇒ Object
- .color_stack(stack) ⇒ Object
- .count_stack ⇒ Object
- .debug(message = nil, &block) ⇒ Object
- .deprecated(m) ⇒ Object
- .down_lines(num = 1) ⇒ Object
- .error(message = nil, &block) ⇒ Object
- .exception(e) ⇒ Object
- .get_level(level) ⇒ Object
- .high(message = nil, &block) ⇒ Object
- .highlight(str = nil) ⇒ Object
- .ignore_stderr(&block) ⇒ Object
- .info(message = nil, &block) ⇒ Object
- .last_caller(stack) ⇒ Object
- .log(message = nil, severity = MEDIUM, &block) ⇒ Object
- .log_obj_fingerprint(obj, level, file = $stdout) ⇒ Object
- .log_obj_inspect(obj, level, file = $stdout) ⇒ Object
- .logn(message = nil, severity = MEDIUM, &block) ⇒ Object
- .low(message = nil, &block) ⇒ Object
- .medium(message = nil, &block) ⇒ Object
- .reset_color ⇒ Object
- .return_line ⇒ Object
- .stack(stack) ⇒ Object
- .trap_std(msg = "STDOUT", msge = "STDERR", severity = 0, severity_err = nil) ⇒ Object
- .trap_stderr(msg = "STDERR", severity = 0) ⇒ Object
- .tsv(tsv) ⇒ Object
- .uncolor(str) ⇒ Object
- .up_lines(num = 1) ⇒ Object
- .warn(message = nil, &block) ⇒ Object
- .with_severity(level) ⇒ Object
- .with_stack_counts(head = 10, total = 100) ⇒ Object
Class Attribute Details
.logfile(file = nil) ⇒ Object
Returns the value of attribute logfile.
137 138 139 |
# File 'lib/rbbt/util/log.rb', line 137 def logfile @logfile end |
.nocolor ⇒ Object
Returns the value of attribute nocolor.
137 138 139 |
# File 'lib/rbbt/util/log.rb', line 137 def nocolor @nocolor end |
.severity ⇒ Object
Returns the value of attribute severity.
137 138 139 |
# File 'lib/rbbt/util/log.rb', line 137 def severity @severity end |
.tty_size ⇒ Object
Returns the value of attribute tty_size.
137 138 139 |
# File 'lib/rbbt/util/log.rb', line 137 def tty_size @tty_size end |
Class Method Details
._ignore_stderr ⇒ Object
101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/rbbt/util/log.rb', line 101 def self._ignore_stderr 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 |
.clear_line(out = STDOUT) ⇒ Object
213 214 215 |
# File 'lib/rbbt/util/log.rb', line 213 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
189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/rbbt/util/log.rb', line 189 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
326 327 328 329 330 331 332 333 |
# File 'lib/rbbt/util/log.rb', line 326 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 |
.count_stack ⇒ Object
351 352 353 354 355 356 357 358 359 360 361 362 363 |
# File 'lib/rbbt/util/log.rb', line 351 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
286 287 288 |
# File 'lib/rbbt/util/log.rb', line 286 def self.debug( = nil, &block) log(, DEBUG, &block) end |
.deprecated(m) ⇒ Object
320 321 322 323 324 |
# File 'lib/rbbt/util/log.rb', line 320 def self.deprecated(m) stack = caller warn("DEPRECATED: " << Log.last_caller(stack)) warn("* " << (m || "").to_s) end |
.down_lines(num = 1) ⇒ Object
205 206 207 |
# File 'lib/rbbt/util/log.rb', line 205 def self.down_lines(num = 1) nocolor ? "" : "\033[#{num+1}E" end |
.error(message = nil, &block) ⇒ Object
310 311 312 |
# File 'lib/rbbt/util/log.rb', line 310 def self.error( = nil, &block) log(, ERROR, &block) end |
.exception(e) ⇒ Object
314 315 316 317 318 |
# File 'lib/rbbt/util/log.rb', line 314 def self.exception(e) stack = caller error([e.class.to_s, e.].compact * ": " ) error("BACKTRACE [#{Process.pid}]: " << Log.last_caller(stack) << "\n" + color_stack(e.backtrace)*"\n") end |
.get_level(level) ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/rbbt/util/log.rb', line 121 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
298 299 300 |
# File 'lib/rbbt/util/log.rb', line 298 def self.high( = nil, &block) log(, HIGH, &block) end |
.highlight(str = nil) ⇒ Object
217 218 219 220 221 222 223 224 225 |
# File 'lib/rbbt/util/log.rb', line 217 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
115 116 117 118 119 |
# File 'lib/rbbt/util/log.rb', line 115 def self.ignore_stderr(&block) LOG_MUTEX.synchronize do _ignore_stderr &block end end |
.info(message = nil, &block) ⇒ Object
302 303 304 |
# File 'lib/rbbt/util/log.rb', line 302 def self.info( = nil, &block) log(, INFO, &block) end |
.last_caller(stack) ⇒ Object
27 28 29 30 31 32 33 34 35 |
# 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 line.gsub('`', "'") end |
.log(message = nil, severity = MEDIUM, &block) ⇒ Object
252 253 254 255 256 257 258 |
# File 'lib/rbbt/util/log.rb', line 252 def self.log( = nil, severity = MEDIUM, &block) return if severity < self.severity ||= block.call if block_given? return if .nil? = + "\n" unless [-1] == "\n" self.logn , severity, &block end |
.log_obj_fingerprint(obj, level, file = $stdout) ⇒ Object
273 274 275 276 277 278 279 280 281 282 283 284 |
# File 'lib/rbbt/util/log.rb', line 273 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
260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/rbbt/util/log.rb', line 260 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
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/rbbt/util/log.rb', line 228 def self.logn( = nil, severity = MEDIUM, &block) return if severity < self.severity ||= block.call if block_given? return if .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) = "" << highlight << << color(0) if severity >= INFO str = prefix << " " << .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
290 291 292 |
# File 'lib/rbbt/util/log.rb', line 290 def self.low( = nil, &block) log(, LOW, &block) end |
.medium(message = nil, &block) ⇒ Object
294 295 296 |
# File 'lib/rbbt/util/log.rb', line 294 def self.medium( = nil, &block) log(, MEDIUM, &block) end |
.reset_color ⇒ Object
185 186 187 |
# File 'lib/rbbt/util/log.rb', line 185 def self.reset_color reset end |
.return_line ⇒ Object
209 210 211 |
# File 'lib/rbbt/util/log.rb', line 209 def self.return_line nocolor ? "" : "\033[1A" end |
.stack(stack) ⇒ Object
341 342 343 344 345 346 347 348 349 |
# File 'lib/rbbt/util/log.rb', line 341 def self.stack(stack) LOG_MUTEX.synchronize do STDERR.puts Log.color :magenta, "Stack trace [#{Process.pid}]: " << Log.last_caller(caller) color_stack(stack).each do |line| STDERR.puts line end end end |
.trap_std(msg = "STDOUT", msge = "STDERR", severity = 0, severity_err = nil) ⇒ Object
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 74 |
# File 'lib/rbbt/util/log.rb', line 38 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
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/rbbt/util/log.rb', line 76 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) ⇒ Object
335 336 337 338 339 |
# File 'lib/rbbt/util/log.rb', line 335 def self.tsv(tsv) 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) end |
.uncolor(str) ⇒ Object
181 182 183 |
# File 'lib/rbbt/util/log.rb', line 181 def self.uncolor(str) "" << Term::ANSIColor.uncolor(str) end |
.up_lines(num = 1) ⇒ Object
201 202 203 |
# File 'lib/rbbt/util/log.rb', line 201 def self.up_lines(num = 1) nocolor ? "" : "\033[#{num+1}F\033[2K" end |
.warn(message = nil, &block) ⇒ Object
306 307 308 |
# File 'lib/rbbt/util/log.rb', line 306 def self.warn( = nil, &block) log(, WARN, &block) end |
.with_severity(level) ⇒ Object
151 152 153 154 155 156 157 158 159 |
# File 'lib/rbbt/util/log.rb', line 151 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
365 366 367 368 369 370 371 372 373 374 |
# File 'lib/rbbt/util/log.rb', line 365 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 |