class JsDuck::Logger

Central logging of JsDuck

Constants

CLEAR
COLORS

Attributes

colors[RW]

Set true to force colored output. Set false to force no colors.

verbose[RW]

Set to true to enable verbose logging

Public Class Methods

new() click to toggle source
# File lib/jsduck/logger.rb, line 19
def initialize
  @verbose = false
  @colors = nil

  @warnings = Warning::Registry.new

  @shown_warnings = {}
end

Public Instance Methods

configure(opts) click to toggle source

Configures the logger with command line options.

# File lib/jsduck/logger.rb, line 42
def configure(opts)
  self.verbose = true if opts.verbose

  self.colors = opts.color unless opts.color.nil?

  begin
    opts.warnings.each do |w|
      set_warning(w[:type], w[:enabled], w[:path], w[:params])
    end
  rescue Warning::WarnException => e
    warn(nil, e.message)
  end
end
configure_defaults() click to toggle source

Configures warnings to default settings.

NB! Needs to be called before retrieving the documentation with doc_warnings (otherwise the +/- signs will be wrong).

# File lib/jsduck/logger.rb, line 32
def configure_defaults
  # Enable all warnings except some.
  set_warning(:all, true)
  set_warning(:link_auto, false)
  set_warning(:param_count, false)
  set_warning(:fires, false)
  set_warning(:nodoc, false)
end
doc_warnings() click to toggle source

get documentation for all warnings

# File lib/jsduck/logger.rb, line 75
def doc_warnings
  @warnings.doc
end
fatal(msg) click to toggle source

Prints fatal error message with backtrace. The error param should be $! from resque block.

# File lib/jsduck/logger.rb, line 109
def fatal(msg)
  $stderr.puts paint(:red, "Error: ") + msg
end
fatal_backtrace(msg, error) click to toggle source

Prints fatal error message with backtrace. The error param should be $! from resque block.

# File lib/jsduck/logger.rb, line 115
def fatal_backtrace(msg, error)
  $stderr.puts paint(:red, "Error: ") + "#{msg}: #{error}"
  $stderr.puts
  $stderr.puts "Here's a full backtrace:"
  $stderr.puts error.backtrace
end
log(msg, filename=nil) click to toggle source

Prints log message with optional filename appended

# File lib/jsduck/logger.rb, line 57
def log(msg, filename=nil)
  if @verbose
    $stderr.puts paint(:green, msg) + " " + format(filename) + " ..."
  end
end
set_warning(type, enabled, pattern=nil, params=[]) click to toggle source

Enables or disables a particular warning or all warnings when type == :all. Additionally a filename pattern can be specified.

# File lib/jsduck/logger.rb, line 66
def set_warning(type, enabled, pattern=nil, params=[])
  begin
    @warnings.set(type, enabled, pattern, params)
  rescue Warning::WarnException => e
    warn(nil, e.message)
  end
end
warn(type, msg, file={}, args=[]) click to toggle source

Prints warning message.

The type must be one of predefined warning types which can be toggled on/off with command-line options, or it can be nil, in which case the warning is always shown.

Ignores duplicate warnings - only prints the first one. Works best when –processes=0, but it reduces the amount of warnings greatly also when run multiple processes.

The `file` parameter must be a hash like:

{:filename => "foo.js", :linenr => 17}

When supplied, it the filename and line number will be appended to the message, to convey where the warning was triggered.

The optional `args` parameter must be an array of arguments and only applies to some warning types like :nodoc.

# File lib/jsduck/logger.rb, line 99
def warn(type, msg, file={}, args=[])
  if warning_enabled?(type, file[:filename], args)
    print_warning(msg, file[:filename], file[:linenr])
  end

  return false
end
warnings_logged?() click to toggle source

True when at least one warning was logged.

# File lib/jsduck/logger.rb, line 123
def warnings_logged?
  @shown_warnings.length > 0
end

Private Instance Methods

format(filename=nil, line=nil) click to toggle source

Formats filename and line number for output

# File lib/jsduck/logger.rb, line 162
def format(filename=nil, line=nil)
  out = ""
  if filename
    out = Util::OS.windows? ? filename.gsub('/', '\\') : filename
    if line
      out += ":#{line}:"
    end
  end
  paint(:magenta, out)
end
paint(color_name, msg) click to toggle source

Helper for doing colored output in UNIX terminal

Only does color output when STDERR is attached to TTY i.e. is not piped/redirected.

# File lib/jsduck/logger.rb, line 177
def paint(color_name, msg)
  if @colors == false || @colors == nil && (Util::OS.windows? || !$stderr.tty?)
    msg
  else
    COLORS[color_name] + msg + CLEAR
  end
end
print_warning(msg, filename, line) click to toggle source
warning_enabled?(type, filename, args) click to toggle source
# File lib/jsduck/logger.rb, line 142
def warning_enabled?(type, filename, args)
  if type == nil
    true
  elsif !@warnings.has?(type)
    warn(nil, "Unknown warning type #{type}")
  else
    @warnings.enabled?(type, filename, args)
  end
end