class Vmreverter::Logger
Constants
- BLACK
- BLUE
- BRIGHT_BLUE
- BRIGHT_CYAN
- BRIGHT_GREEN
- BRIGHT_MAGENTA
- BRIGHT_NORMAL
- BRIGHT_RED
- BRIGHT_WHITE
- BRIGHT_YELLOW
- CYAN
- GREEN
- GREY
- LOG_LEVELS
- MAGENTA
- NORMAL
- RED
- WHITE
- YELLOW
Attributes
color[RW]
destinations[RW]
log_level[RW]
Public Class Methods
new(*args)
click to toggle source
# File lib/vmreverter/logger.rb, line 34 def initialize(*args) options = args.last.is_a?(Hash) ? args.pop : {} @color = options[:color] @log_level = options[:debug] ? :debug : :normal @destinations = [] dests = args dests << STDOUT unless options[:quiet] dests.uniq! dests.each {|dest| add_destination(dest)} end
Public Instance Methods
add_destination(dest, mode='w')
click to toggle source
# File lib/vmreverter/logger.rb, line 46 def add_destination(dest, mode='w') case dest when IO @destinations << dest when String @destinations << File.open(dest, mode) else raise "Unsuitable log destination #{dest.inspect}" end end
debug(*args)
click to toggle source
# File lib/vmreverter/logger.rb, line 83 def debug *args return unless is_debug? optionally_color WHITE, args end
error(*args)
click to toggle source
# File lib/vmreverter/logger.rb, line 102 def error *args optionally_color BRIGHT_RED, args end
host_output(*args)
click to toggle source
# File lib/vmreverter/logger.rb, line 76 def host_output *args return unless is_debug? strings = strip_colors_from args string = strings.join optionally_color GREY, string, false end
is_debug?()
click to toggle source
# File lib/vmreverter/logger.rb, line 68 def is_debug? LOG_LEVELS[@log_level] <= LOG_LEVELS[:debug] end
is_warn?()
click to toggle source
# File lib/vmreverter/logger.rb, line 72 def is_warn? LOG_LEVELS[@log_level] <= LOG_LEVELS[:warn] end
notify(*args)
click to toggle source
# File lib/vmreverter/logger.rb, line 98 def notify *args optionally_color BRIGHT_WHITE, args end
optionally_color(color_code, msg, add_newline = true)
click to toggle source
# File lib/vmreverter/logger.rb, line 112 def optionally_color color_code, msg, add_newline = true print_statement = add_newline ? :puts : :print @destinations.each do |to| to.print color_code if @color to.send print_statement, msg to.print NORMAL if @color end end
pretty_backtrace(backtrace = caller(1))
click to toggle source
utility method to get the current call stack and format it to a human-readable string (which some IDEs/editors will recognize as links to the line numbers in the trace)
# File lib/vmreverter/logger.rb, line 124 def pretty_backtrace backtrace = caller(1) trace = purge_harness_files_from( Array( backtrace ) ) expand_symlinks( trace ).join "\n" end
remove_destination(dest)
click to toggle source
# File lib/vmreverter/logger.rb, line 57 def remove_destination(dest) case dest when IO @destinations.delete(dest) when String @destinations.delete_if {|d| d.respond_to?(:path) and d.path == dest} else raise "Unsuitable log destination #{dest.inspect}" end end
strip_colors_from(lines)
click to toggle source
# File lib/vmreverter/logger.rb, line 106 def strip_colors_from lines Array(lines).map do |line| line.gsub /\e\[(\d+;)?\d+m/, '' end end
success(*args)
click to toggle source
# File lib/vmreverter/logger.rb, line 94 def success *args optionally_color GREEN, args end
warn(*args)
click to toggle source
# File lib/vmreverter/logger.rb, line 88 def warn *args return unless is_warn? strings = args.map {|msg| "Warning: #{msg}" } optionally_color YELLOW, strings end
Private Instance Methods
expand_symlink(file_path)
click to toggle source
utility method that takes a path as input, checks each component of the path to see if it is a symlink, and expands it if it is. returns the expanded path.
# File lib/vmreverter/logger.rb, line 154 def expand_symlink file_path file_path.split( "/" ).inject do |full_path, next_dir| next_path = full_path + "/" + next_dir if File.symlink? next_path link = File.readlink next_path next_path = case link when /^\// then link else File.expand_path( full_path + "/" + link ) end end next_path end end
expand_symlinks(backtrace)
click to toggle source
# File lib/vmreverter/logger.rb, line 130 def expand_symlinks backtrace backtrace.collect do |line| file_path, line_num = line.split( ":" ) expanded_path = expand_symlink File.expand_path( file_path ) expanded_path.to_s + ":" + line_num.to_s end end
purge_harness_files_from(backtrace)
click to toggle source
# File lib/vmreverter/logger.rb, line 138 def purge_harness_files_from backtrace mostly_purged = backtrace.reject do |line| # LOADED_FEATURES is an array of anything `require`d, i.e. everything # but the test in question $LOADED_FEATURES.any? do |require_path| line.include? require_path end end # And remove lines that contain our program name in them completely_purged = mostly_purged.reject {|line| line.include? $0 } end