Class: ArduinoCI::Logger
- Inherits:
-
Object
- Object
- ArduinoCI::Logger
- Defined in:
- lib/arduino_ci/logger.rb
Overview
Provide all text processing functions to aid readability of the test log
Constant Summary collapse
- TAB_WIDTH =
4
- INDENT_CHAR =
" ".freeze
- FAILED_ASSURANCE_MESSAGE =
"This may indicate a problem with your configuration; halting here".freeze
Instance Attribute Summary collapse
-
#failure_count ⇒ Integer
readonly
The number of failures reported through the logging mechanism.
-
#tab ⇒ Integer
readonly
The cardinal number of indents.
Class Method Summary collapse
-
.auto_width ⇒ Object
create a logger that’s automatically sized to the console, between 80 and 132 characters.
Instance Method Summary collapse
-
#assure(message) { ... } ⇒ Object
Make a nice status (with checkmark) for something that kills the script immediately on failure.
-
#assure_multiline(message) { ... } ⇒ Object
Make a nice multiline status (with checkmark) for something that kills the script immediately on failure.
-
#attempt(message) { ... } ⇒ Object
Make a nice status (with checkmark) for something that defers any failure code until script exit.
-
#attempt_multiline(message) { ... } ⇒ Object
Make a nice multiline status (with checkmark) for something that defers any failure code until script exit.
-
#banner ⇒ Object
print a nice banner for this project.
-
#fail(message) ⇒ Object
print a failure message (with checkmark) but do not exit.
-
#halt(message) ⇒ Object
print a failure message (with checkmark) and exit immediately afterward.
-
#indent(amount = 1) { ... } ⇒ void
increment an indentation level for the duration of a block’s execution.
-
#indentation ⇒ String
The current line indentation.
-
#inform(message) { ... } ⇒ Object
Print a value as a status line “message…
-
#inform_multiline(message) { ... } ⇒ Object
Print section beginning and end.
-
#initialize(width = nil) ⇒ Logger
constructor
A new instance of Logger.
-
#iprint(str) ⇒ void
print an indented string.
-
#iputs(str = "") ⇒ void
put an indented string.
-
#perform_action(message, multiline, mark_fn, on_fail_msg, tally_on_fail, abort_on_fail) { ... } ⇒ Object
make a nice status line for an action and react to the action.
-
#phase(name) ⇒ void
Print a section heading to the console to break up the text output.
-
#rule(char) ⇒ void
Print a horizontal rule across the console.
-
#warn(message) ⇒ Object
print a failure message (with checkmark) but do not tally a failure.
Constructor Details
#initialize(width = nil) ⇒ Logger
Returns a new instance of Logger.
18 19 20 21 22 23 |
# File 'lib/arduino_ci/logger.rb', line 18 def initialize(width = nil) @tab = 0 @width = width.nil? ? 80 : width @failure_count = 0 @passfail = proc { |result| result ? "✓" : "✗" } end |
Instance Attribute Details
#failure_count ⇒ Integer (readonly)
Returns The number of failures reported through the logging mechanism.
15 16 17 |
# File 'lib/arduino_ci/logger.rb', line 15 def failure_count @failure_count end |
#tab ⇒ Integer (readonly)
Returns the cardinal number of indents.
12 13 14 |
# File 'lib/arduino_ci/logger.rb', line 12 def tab @tab end |
Class Method Details
.auto_width ⇒ Object
create a logger that’s automatically sized to the console, between 80 and 132 characters
26 27 28 29 30 31 32 33 34 |
# File 'lib/arduino_ci/logger.rb', line 26 def self.auto_width width = begin [132, [80, IO::console.winsize[1] - 2].max].min rescue NoMethodError 80 end self.new(width) end |
Instance Method Details
#assure(message) { ... } ⇒ Object
Make a nice status (with checkmark) for something that kills the script immediately on failure
169 170 171 |
# File 'lib/arduino_ci/logger.rb', line 169 def assure(, &block) perform_action(, false, @passfail, FAILED_ASSURANCE_MESSAGE, true, true, &block) end |
#assure_multiline(message) { ... } ⇒ Object
Make a nice multiline status (with checkmark) for something that kills the script immediately on failure
179 180 181 |
# File 'lib/arduino_ci/logger.rb', line 179 def assure_multiline(, &block) perform_action(, true, @passfail, FAILED_ASSURANCE_MESSAGE, true, true, &block) end |
#attempt(message) { ... } ⇒ Object
Make a nice status (with checkmark) for something that defers any failure code until script exit
148 149 150 |
# File 'lib/arduino_ci/logger.rb', line 148 def attempt(, &block) perform_action(, false, @passfail, nil, true, false, &block) end |
#attempt_multiline(message) { ... } ⇒ Object
Make a nice multiline status (with checkmark) for something that defers any failure code until script exit
158 159 160 |
# File 'lib/arduino_ci/logger.rb', line 158 def attempt_multiline(, &block) perform_action(, true, @passfail, nil, true, false, &block) end |
#banner ⇒ Object
print a nice banner for this project
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/arduino_ci/logger.rb', line 37 def art = [ " . __ ___", " _, ,_ _| , . * ._ _ / ` | ", "(_| [ `(_] (_| | [ ) (_) \\__. _|_ v#{ArduinoCI::VERSION}", ] pad = " " * ((@width - art[2].length) / 2) art.each { |l| puts "#{pad}#{l}" } puts end |
#fail(message) ⇒ Object
print a failure message (with checkmark) but do not exit
193 194 195 |
# File 'lib/arduino_ci/logger.rb', line 193 def fail() attempt() { false } end |
#halt(message) ⇒ Object
print a failure message (with checkmark) and exit immediately afterward
200 201 202 |
# File 'lib/arduino_ci/logger.rb', line 200 def halt() assure() { false } end |
#indent(amount = 1) { ... } ⇒ void
This method returns an undefined value.
increment an indentation level for the duration of a block’s execution
82 83 84 85 86 87 |
# File 'lib/arduino_ci/logger.rb', line 82 def indent(amount = 1, &block) @tab += amount block.call ensure @tab -= amount end |
#indentation ⇒ String
Returns the current line indentation.
50 51 52 |
# File 'lib/arduino_ci/logger.rb', line 50 def indentation (INDENT_CHAR * TAB_WIDTH * @tab) end |
#inform(message) { ... } ⇒ Object
Print a value as a status line “message… retval”
210 211 212 |
# File 'lib/arduino_ci/logger.rb', line 210 def inform(, &block) perform_action(, false, proc { |x| x }, nil, false, false, &block) end |
#inform_multiline(message) { ... } ⇒ Object
Print section beginning and end
219 220 221 |
# File 'lib/arduino_ci/logger.rb', line 219 def inform_multiline(, &block) perform_action(, true, nil, nil, false, false, &block) end |
#iprint(str) ⇒ void
This method returns an undefined value.
print an indented string
72 73 74 75 |
# File 'lib/arduino_ci/logger.rb', line 72 def iprint(str) print(indentation) print(str) end |
#iputs(str = "") ⇒ void
This method returns an undefined value.
put an indented string
58 59 60 61 62 63 64 65 66 |
# File 'lib/arduino_ci/logger.rb', line 58 def iputs(str = "") print(indentation) # split the lines and interleave with a newline character, then render stream_lines = str.to_s.split("\n") marked_stream_lines = stream_lines.flat_map { |s| [s, :nl] }.tap(&:pop) marked_stream_lines.each { |l| print(l == :nl ? "\n#{indentation}" : l) } puts end |
#perform_action(message, multiline, mark_fn, on_fail_msg, tally_on_fail, abort_on_fail) { ... } ⇒ Object
make a nice status line for an action and react to the action
TODO / note to self: inform_multiline is tougher to write
without altering the signature because it only leaves space
for the checkmark _after_ the multiline, it doesn't know how
to make that conditionally the body
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/arduino_ci/logger.rb', line 105 def perform_action(, multiline, mark_fn, on_fail_msg, tally_on_fail, abort_on_fail) line = "#{indentation}#{}... " endline = "#{indentation}...#{} " if multiline puts line @tab += 1 else print line end $stdout.flush # handle the block and any errors it raises caught_error = nil begin result = yield rescue StandardError => e caught_error = e result = false ensure @tab -= 1 if multiline end # put the trailing mark mark = mark_fn.nil? ? "" : mark_fn.call(result) # if multiline, put checkmark at full width print endline if multiline puts mark.to_s.rjust(@width - line.length, " ") unless result iputs on_fail_msg unless on_fail_msg.nil? raise caught_error unless caught_error.nil? @failure_count += 1 if tally_on_fail terminate if abort_on_fail end result end |
#phase(name) ⇒ void
This method returns an undefined value.
Print a section heading to the console to break up the text output
234 235 236 237 238 239 |
# File 'lib/arduino_ci/logger.rb', line 234 def phase(name) puts rule("=") puts("| #{name}") puts("====") end |
#rule(char) ⇒ void
This method returns an undefined value.
Print a horizontal rule across the console
226 227 228 |
# File 'lib/arduino_ci/logger.rb', line 226 def rule(char) puts char[0] * @width end |
#warn(message) ⇒ Object
print a failure message (with checkmark) but do not tally a failure
186 187 188 |
# File 'lib/arduino_ci/logger.rb', line 186 def warn() inform("WARNING") { } end |