Class: JsDuck::Logger

Inherits:
Object
  • Object
show all
Includes:
Util::Singleton
Defined in:
lib/jsduck/logger.rb

Overview

Central logging of JsDuck

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Util::Singleton

included

Constructor Details

#initializeLogger

Returns a new instance of Logger.



19
20
21
22
23
24
25
26
# File 'lib/jsduck/logger.rb', line 19

def initialize
  @verbose = false
  @colors = nil

  @warnings = Warning::Registry.new

  @shown_warnings = {}
end

Instance Attribute Details

#colorsObject

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



17
18
19
# File 'lib/jsduck/logger.rb', line 17

def colors
  @colors
end

#verboseObject

Set to true to enable verbose logging



13
14
15
# File 'lib/jsduck/logger.rb', line 13

def verbose
  @verbose
end

Instance Method Details

#configure(opts) ⇒ Object

Configures the logger with command line options.



42
43
44
45
46
47
48
49
50
51
52
53
54
# 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_defaultsObject

Configures warnings to default settings.

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



32
33
34
35
36
37
38
39
# 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_warningsObject

get documentation for all warnings



75
76
77
# File 'lib/jsduck/logger.rb', line 75

def doc_warnings
  @warnings.doc
end

#fatal(msg) ⇒ Object

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



109
110
111
# File 'lib/jsduck/logger.rb', line 109

def fatal(msg)
  $stderr.puts paint(:red, "Error: ") + msg
end

#fatal_backtrace(msg, error) ⇒ Object

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



115
116
117
118
119
120
# 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) ⇒ Object

Prints log message with optional filename appended



57
58
59
60
61
# 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 = []) ⇒ Object

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



66
67
68
69
70
71
72
# 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 = []) ⇒ Object

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.



99
100
101
102
103
104
105
# 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?Boolean

True when at least one warning was logged.

Returns:

  • (Boolean)


123
124
125
# File 'lib/jsduck/logger.rb', line 123

def warnings_logged?
  @shown_warnings.length > 0
end