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

#doc_warningsObject

get documentation for all warnings



47
48
49
# File 'lib/jsduck/logger.rb', line 47

def doc_warnings
  @warnings.doc
end

#fatal(msg) ⇒ Object

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



94
95
96
# File 'lib/jsduck/logger.rb', line 94

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.



100
101
102
103
104
105
# File 'lib/jsduck/logger.rb', line 100

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



29
30
31
32
33
# File 'lib/jsduck/logger.rb', line 29

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.



38
39
40
41
42
43
44
# File 'lib/jsduck/logger.rb', line 38

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, filename = nil, line = nil) ⇒ 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.

Optionally filename and line number will be inserted to message. These two last arguments can also be supplied as one hash of:

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


66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/jsduck/logger.rb', line 66

def warn(type, msg, filename=nil, line=nil)
  if filename.is_a?(Hash)
    line = filename[:linenr]
    filename = filename[:filename]
  end

  if warning_enabled?(type, filename)
    print_warning(msg, filename, line)
  end

  return false
end

#warn_nodoc(type, visibility, msg, file) ⇒ Object

Prints :nodoc warning message.

Because the :nodoc warning needs different parameters, for now we’re using a separate method specially for these.



83
84
85
86
87
88
89
90
# File 'lib/jsduck/logger.rb', line 83

def warn_nodoc(type, visibility, msg, file)
  filename = file[:filename]
  line = file[:linenr]

  if @warnings.enabled?(:nodoc, filename, [type, visibility])
    print_warning(msg, filename, line)
  end
end

#warnings_logged?Boolean

True when at least one warning was logged.

Returns:

  • (Boolean)


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

def warnings_logged?
  @shown_warnings.length > 0
end