Module: Dapp::Application::Logging
- Included in:
- Dapp::Application
- Defined in:
- lib/dapp/application/logging.rb
Overview
Logging
Constant Summary collapse
- DEFAULT_STYLE =
{ message: :step, process: :secondary, status: :secondary, success: :success, failed: :warning, time: :default }.freeze
Instance Method Summary collapse
- #dry_run? ⇒ Boolean
- #free_space(str) ⇒ Object
- #log? ⇒ Boolean
- #log_process(message, process: nil, short: false, style: {}, &blk) ⇒ Object
- #log_process_default(info, success_message, failed_message, inline: false) ⇒ Object
- #log_process_short(message, style: {}, &blk) ⇒ Object
- #log_process_verbose(message, process:, style: {}, &blk) ⇒ Object
- #log_quiet? ⇒ Boolean
- #log_secondary_process(message, **kvargs, &blk) ⇒ Object
- #log_state(message, state:, styles: {}) ⇒ Object
- #log_time? ⇒ Boolean
- #log_verbose? ⇒ Boolean
- #rjust(str, start_string) ⇒ Object
- #slice(str) ⇒ Object
- #terminal_width ⇒ Object
Instance Method Details
#dry_run? ⇒ Boolean
22 23 24 |
# File 'lib/dapp/application/logging.rb', line 22 def dry_run? [:dry_run] end |
#free_space(str) ⇒ Object
107 108 109 110 111 112 113 |
# File 'lib/dapp/application/logging.rb', line 107 def free_space(str) base_time = log_time? ? log_time.length : 0 indent = log_indent.length str = Paint.unpaint(str.to_s).length time = 15 terminal_width - base_time - str - indent - time end |
#log? ⇒ Boolean
6 7 8 |
# File 'lib/dapp/application/logging.rb', line 6 def log? !log_quiet? end |
#log_process(message, process: nil, short: false, style: {}, &blk) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/dapp/application/logging.rb', line 47 def log_process(, process: nil, short: false, style: {}, &blk) style[:message] ||= DEFAULT_STYLE[:message] style[:process] ||= DEFAULT_STYLE[:process] style[:failed] ||= DEFAULT_STYLE[:failed] style[:success] ||= DEFAULT_STYLE[:success] if log_verbose? && !short process ||= t(code: 'status.process.default') log_process_verbose(, process: process, style: style, &blk) else log_process_short(, style: style, &blk) end end |
#log_process_default(info, success_message, failed_message, inline: false) ⇒ Object
82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/dapp/application/logging.rb', line 82 def log_process_default(info, , , inline: false) log info, inline: inline = start = Time.now yield rescue Exception::Base, Error::Base, SignalException, StandardError => _e = raise ensure time = paint_string("#{(Time.now - start).round(2)} sec", DEFAULT_STYLE[:time]) log "#{} #{time}", indent: !inline, time: !inline end |
#log_process_short(message, style: {}, &blk) ⇒ Object
75 76 77 78 79 80 |
# File 'lib/dapp/application/logging.rb', line 75 def log_process_short(, style: {}, &blk) info = "#{paint_string(slice(), style[:message])} ... " = paint_string(rjust(t(code: 'status.success.default'), info), style[:success]) = paint_string(rjust(t(code: 'status.failed.default'), info), style[:failed]) log_process_default(info, , , inline: true, &blk) end |
#log_process_verbose(message, process:, style: {}, &blk) ⇒ Object
65 66 67 68 69 70 71 72 73 |
# File 'lib/dapp/application/logging.rb', line 65 def log_process_verbose(, process:, style: {}, &blk) process = paint_string(rjust(process, ), style[:process]) info = paint_string(, style[:message]) + process = paint_string(slice(), style[:message]) + paint_string(rjust(t(code: 'status.success.default'), ), style[:success]) = paint_string(slice() + rjust(t(code: 'status.failed.default'), ), style[:failed]) log_process_default(info, , , &blk) end |
#log_quiet? ⇒ Boolean
10 11 12 |
# File 'lib/dapp/application/logging.rb', line 10 def log_quiet? [:log_quiet] end |
#log_secondary_process(message, **kvargs, &blk) ⇒ Object
61 62 63 |
# File 'lib/dapp/application/logging.rb', line 61 def log_secondary_process(, **kvargs, &blk) log_process(, **kvargs.merge(style: { message: :secondary, success: :secondary }), &blk) end |
#log_state(message, state:, styles: {}) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/dapp/application/logging.rb', line 35 def log_state(, state:, styles: {}) styles[:message] ||= DEFAULT_STYLE[:message] styles[:status] ||= DEFAULT_STYLE[:status] = slice() state = rjust(state, ) = paint_string(, styles[:message]) formatted_status = paint_string(state, styles[:status]) log "#{}#{formatted_status}" end |
#log_time? ⇒ Boolean
18 19 20 |
# File 'lib/dapp/application/logging.rb', line 18 def log_time? [:log_time] end |
#log_verbose? ⇒ Boolean
14 15 16 |
# File 'lib/dapp/application/logging.rb', line 14 def log_verbose? [:log_verbose] end |
#rjust(str, start_string) ⇒ Object
95 96 97 |
# File 'lib/dapp/application/logging.rb', line 95 def rjust(str, start_string) str.rjust(free_space(start_string)) end |
#slice(str) ⇒ Object
99 100 101 102 103 104 105 |
# File 'lib/dapp/application/logging.rb', line 99 def slice(str) if (index = free_space(t(code: 'state.using_cache'))) >= 0 # free space by longest status str.slice(0..index) else str.slice(0) end end |
#terminal_width ⇒ Object
115 116 117 |
# File 'lib/dapp/application/logging.rb', line 115 def terminal_width @terminal_width ||= `tput cols`.strip.to_i end |