Class: YARD::Logger
- Inherits:
-
Logger
- Object
- Logger
- YARD::Logger
- Defined in:
- lib/yard/logging.rb
Overview
Handles console logging for info, warnings and errors. Uses the stdlib Logger class in Ruby for all the backend logic.
Constant Summary collapse
- PROGRESS_INDICATORS =
The list of characters displayed beside the progress bar to indicate “movement”.
["\u230C", "\u230D", "\u230E", "\u230F"]
Instance Attribute Summary collapse
-
#io ⇒ IO
The IO object being logged to.
-
#show_backtraces ⇒ Boolean
Whether backtraces should be shown (by default this is on).
-
#show_progress ⇒ Boolean
Whether progress indicators should be shown when logging CLIs (by default this is off).
Class Method Summary collapse
-
.instance(pipe = STDOUT) ⇒ Logger
The logger instance.
Instance Method Summary collapse
-
#backtrace(exc, level_meth = :error) ⇒ void
Prints the backtrace
exc
to the logger as error data. -
#capture(msg, nontty_log = :debug) { ... } ⇒ void
Captures the duration of a block of code for benchmark analysis.
-
#clear_progress ⇒ void
Clears the progress indicator in the TTY display.
-
#debug(*args) ⇒ Object
Changes the debug level to DEBUG if $DEBUG is set and writes a debugging message.
-
#enter_level(new_level = level) { ... } ⇒ Object
Sets the logger level for the duration of the block.
-
#initialize(pipe, *args) ⇒ Logger
constructor
Creates a new logger.
-
#progress(msg, nontty_log = :debug) ⇒ void
Displays a progress indicator for a given message.
-
#puts(msg) ⇒ void
Displays an unformatted line to the logger output stream.
-
#warn_no_continuations ⇒ void
deprecated
Deprecated.
Continuations are no longer needed by YARD 0.8.0+.
Constructor Details
#initialize(pipe, *args) ⇒ Logger
Creates a new logger
39 40 41 42 43 44 45 46 47 48 |
# File 'lib/yard/logging.rb', line 39 def initialize(pipe, *args) super(pipe, *args) self.io = pipe self.show_backtraces = true self.show_progress = false self.level = WARN self.formatter = method(:format_log) @progress_indicator = 0 @mutex = Mutex.new end |
Instance Attribute Details
#io ⇒ IO
Returns the IO object being logged to.
15 16 17 |
# File 'lib/yard/logging.rb', line 15 def io @io end |
#show_backtraces ⇒ Boolean
Returns whether backtraces should be shown (by default this is on).
19 |
# File 'lib/yard/logging.rb', line 19 def show_backtraces; @show_backtraces || level == DEBUG end |
#show_progress ⇒ Boolean
Returns whether progress indicators should be shown when logging CLIs (by default this is off).
24 25 26 27 28 29 |
# File 'lib/yard/logging.rb', line 24 def show_progress return false if RUBY18 # threading is too ineffective for progress support return false unless io.tty? # no TTY support on IO return false if level > WARN # no progress in verbose/debug modes @show_progress end |
Class Method Details
.instance(pipe = STDOUT) ⇒ Logger
The logger instance
34 35 36 |
# File 'lib/yard/logging.rb', line 34 def self.instance(pipe = STDOUT) @logger ||= new(pipe) end |
Instance Method Details
#backtrace(exc, level_meth = :error) ⇒ void
This method returns an undefined value.
Prints the backtrace exc
to the logger as error data.
126 127 128 129 130 131 |
# File 'lib/yard/logging.rb', line 126 def backtrace(exc, level_meth = :error) return unless show_backtraces send(level_meth, "#{exc.class.class_name}: #{exc.}") send(level_meth, "Stack trace:" + exc.backtrace[0..5].map {|x| "\n\t#{x}" }.join + "\n") end |
#capture(msg, nontty_log = :debug) { ... } ⇒ void
Implement capture storage for reporting of benchmarks
This method returns an undefined value.
Captures the duration of a block of code for benchmark analysis. Also calls #progress on the message to display it to the user.
66 67 68 69 70 71 |
# File 'lib/yard/logging.rb', line 66 def capture(msg, nontty_log = :debug, &block) progress(msg, nontty_log) yield ensure clear_progress end |
#clear_progress ⇒ void
This method returns an undefined value.
Clears the progress indicator in the TTY display.
106 107 108 109 110 |
# File 'lib/yard/logging.rb', line 106 def clear_progress return unless show_progress self << "\e[?25h\e[2K" @progress_msg = nil end |
#debug(*args) ⇒ Object
Changes the debug level to DEBUG if $DEBUG is set and writes a debugging message.
52 53 54 55 |
# File 'lib/yard/logging.rb', line 52 def debug(*args) self.level = DEBUG if $DEBUG super end |
#enter_level(new_level = level) { ... } ⇒ Object
Sets the logger level for the duration of the block
151 152 153 154 155 |
# File 'lib/yard/logging.rb', line 151 def enter_level(new_level = level, &block) old_level, self.level = level, new_level yield self.level = old_level end |
#progress(msg, nontty_log = :debug) ⇒ void
This method returns an undefined value.
Displays a progress indicator for a given message. This progress report is only displayed on TTY displays, otherwise the message is passed to the nontty_log
level.
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/yard/logging.rb', line 82 def progress(msg, nontty_log = :debug) send(nontty_log, msg) if nontty_log return unless show_progress icon = "" if defined?(::Encoding) icon = PROGRESS_INDICATORS[@progress_indicator] + " " end self << "\e[2K\e[?25l\e[1m#{icon}#{msg}\e[0m\r" @mutex.synchronize do @progress_msg = msg @progress_indicator += 1 @progress_indicator %= PROGRESS_INDICATORS.size end Thread.new do sleep(0.05) @mutex.synchronize do progress(msg + ".", nil) if @progress_msg == msg end end end |
#puts(msg) ⇒ void
This method returns an undefined value.
Displays an unformatted line to the logger output stream. Similar to the #<< method, but adds a newline.
117 118 119 |
# File 'lib/yard/logging.rb', line 117 def puts(msg) self << "#{msg}\n" end |
#warn_no_continuations ⇒ void
Continuations are no longer needed by YARD 0.8.0+.
This method returns an undefined value.
Warns that the Ruby environment does not support continuations. Applies to JRuby, Rubinius and MacRuby. This warning will only display once per Ruby process.
139 140 |
# File 'lib/yard/logging.rb', line 139 def warn_no_continuations end |