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

Instance Method Details

#dry_run?Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/dapp/application/logging.rb', line 22

def dry_run?
  cli_options[: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

Returns:

  • (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(message, 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(message, process: process, style: style, &blk)
  else
    log_process_short(message, 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, success_message, failed_message, inline: false)
  log info, inline: inline
  message = success_message
  start = Time.now
  yield
rescue Exception::Base, Error::Base, SignalException, StandardError => _e
  message = failed_message
  raise
ensure
  time = paint_string("#{(Time.now - start).round(2)} sec", DEFAULT_STYLE[:time])
  log "#{message} #{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(message, style: {}, &blk)
  info            = "#{paint_string(slice(message), style[:message])} ... "
  success_message = paint_string(rjust(t(code: 'status.success.default'), info), style[:success])
  failed_message  = paint_string(rjust(t(code: 'status.failed.default'), info), style[:failed])
  log_process_default(info, success_message, failed_message, 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(message, process:, style: {}, &blk)
  process         = paint_string(rjust(process, message), style[:process])
  info            = paint_string(message, style[:message]) + process
  success_message = paint_string(slice(message), style[:message]) +
                    paint_string(rjust(t(code: 'status.success.default'), message), style[:success])
  failed_message  = paint_string(slice(message) +
    rjust(t(code: 'status.failed.default'), message), style[:failed])
  log_process_default(info, success_message, failed_message, &blk)
end

#log_quiet?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/dapp/application/logging.rb', line 10

def log_quiet?
  cli_options[: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(message, **kvargs, &blk)
  log_process(message, **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(message, state:, styles: {})
  styles[:message] ||= DEFAULT_STYLE[:message]
  styles[:status] ||= DEFAULT_STYLE[:status]

  message = slice(message)
  state   = rjust(state, message)
  formatted_message = paint_string(message, styles[:message])
  formatted_status  = paint_string(state, styles[:status])

  log "#{formatted_message}#{formatted_status}"
end

#log_time?Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/dapp/application/logging.rb', line 18

def log_time?
  cli_options[:log_time]
end

#log_verbose?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/dapp/application/logging.rb', line 14

def log_verbose?
  cli_options[: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_widthObject



115
116
117
# File 'lib/dapp/application/logging.rb', line 115

def terminal_width
  @terminal_width ||= `tput cols`.strip.to_i
end