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
- PROGRESS_INDICATORS =
The list of characters displayed beside the progress bar to indicate "movement".
["\u230C", "\u230D", "\u230E", "\u230F"]
Instance Attribute Summary (collapse)
-
- (Boolean) show_backtraces
Whether backtraces should be shown (by default this is on).
-
- (Boolean) show_progress
Whether progress indicators should be shown when logging CLIs (by default this is off).
Class Method Summary (collapse)
-
+ (Logger) instance(pipe = STDOUT)
The logger instance.
Instance Method Summary (collapse)
-
- (void) backtrace(exc, level_meth = :error)
Prints the backtrace exc to the logger as error data.
-
- (void) capture(msg, nontty_log = :debug) { ... }
Captures the duration of a block of code for benchmark analysis.
-
- (void) clear_progress
Clears the progress indicator in the TTY display.
-
- (Object) debug(*args)
Changes the debug level to DEBUG if $DEBUG is set and writes a debugging message.
-
- (Object) enter_level(new_level = level) { ... }
Sets the logger level for the duration of the block.
-
- (Logger) initialize(pipe, *args)
constructor
Creates a new logger.
-
- (IO) io
The IO object being logged to.
- - (Object) io=(pipe)
-
- (void) print(msg = '')
(also: #<<)
Displays an unformatted line to the logger output stream.
-
- (void) progress(msg, nontty_log = :debug)
Displays a progress indicator for a given message.
-
- (void) puts(msg = '')
Displays an unformatted line to the logger output stream, adding a newline.
-
- (void) warn_no_continuations
deprecated
Deprecated.
Continuations are no longer needed by YARD 0.8.0+.
Constructor Details
- (Logger) initialize(pipe, *args)
Creates a new logger
41 42 43 44 45 46 47 48 49 50 |
# File 'lib/yard/logging.rb', line 41 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
- (Boolean) show_backtraces
Returns whether backtraces should be shown (by default this is on).
20 |
# File 'lib/yard/logging.rb', line 20 def show_backtraces; @show_backtraces || level == DEBUG end |
- (Boolean) show_progress
Returns whether progress indicators should be shown when logging CLIs (by default this is off).
25 26 27 28 29 30 31 |
# File 'lib/yard/logging.rb', line 25 def show_progress return false if YARD.ruby18? # threading is too ineffective for progress support return false if YARD.windows? # windows has poor ANSI 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
+ (Logger) instance(pipe = STDOUT)
The logger instance
36 37 38 |
# File 'lib/yard/logging.rb', line 36 def self.instance(pipe = STDOUT) @logger ||= new(pipe) end |
Instance Method Details
- (void) backtrace(exc, level_meth = :error)
This method returns an undefined value.
Prints the backtrace exc to the logger as error data.
141 142 143 144 145 146 |
# File 'lib/yard/logging.rb', line 141 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 |
- (void) capture(msg, nontty_log = :debug) { ... }
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.
68 69 70 71 72 73 |
# File 'lib/yard/logging.rb', line 68 def capture(msg, nontty_log = :debug, &block) progress(msg, nontty_log) yield ensure clear_progress end |
- (void) clear_progress
This method returns an undefined value.
Clears the progress indicator in the TTY display.
108 109 110 111 112 |
# File 'lib/yard/logging.rb', line 108 def clear_progress return unless show_progress print_no_newline("\e[?25h\e[2K") @progress_msg = nil end |
- (Object) debug(*args)
Changes the debug level to DEBUG if $DEBUG is set and writes a debugging message.
54 55 56 57 |
# File 'lib/yard/logging.rb', line 54 def debug(*args) self.level = DEBUG if $DEBUG super end |
- (Object) enter_level(new_level = level) { ... }
Sets the logger level for the duration of the block
166 167 168 169 170 |
# File 'lib/yard/logging.rb', line 166 def enter_level(new_level = level, &block) old_level, self.level = level, new_level yield self.level = old_level end |
- (IO) io
Returns the IO object being logged to
15 |
# File 'lib/yard/logging.rb', line 15 def io; @logdev end |
- (Object) io=(pipe)
16 |
# File 'lib/yard/logging.rb', line 16 def io=(pipe) @logdev = pipe end |
- (void) print(msg = '') Also known as: <<
This method returns an undefined value.
Displays an unformatted line to the logger output stream.
130 131 132 133 |
# File 'lib/yard/logging.rb', line 130 def print(msg = '') clear_line print_no_newline(msg) end |
- (void) progress(msg, nontty_log = :debug)
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.
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/yard/logging.rb', line 84 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 print("\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 |
- (void) puts(msg = '')
This method returns an undefined value.
Displays an unformatted line to the logger output stream, adding a newline.
119 120 121 |
# File 'lib/yard/logging.rb', line 119 def puts(msg = '') print("#{msg}\n") end |
- (void) warn_no_continuations
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.
154 155 |
# File 'lib/yard/logging.rb', line 154 def warn_no_continuations end |