Class: WifiWand::Commands::Log

Inherits:
Base
  • Object
show all
Defined in:
lib/wifi_wand/commands/log.rb

Constant Summary collapse

VALUE_TAKING_OPTIONS =
%w[--interval --verbose-logs].freeze
OPTIONAL_VALUE_OPTIONS =
%w[--file].freeze
FLAG_OPTIONS =
%w[--stdout].freeze
OPTION_DEFINITIONS =
{
  interval: {
    parser_description: 'Poll interval in seconds (default: 5)',
    summary:            '--interval N (default 5 seconds)',
    switch:             '--interval N',
  },
  file:     {
    parser_description: 'Enable file logging (default: wifiwand-events.log)',
    summary:            '--file [PATH] (default: wifiwand-events.log)',
    switch:             '--file [PATH]',
  },
  stdout:   {
    parser_description: 'Keep stdout when file destination is used',
    summary:            '--stdout (keep stdout when file destination is used)',
    switch:             '--stdout',
  },
}.freeze
COMMAND_OPTION_SPECS =
{
  optional_value_options: OPTIONAL_VALUE_OPTIONS,
  scoped_options:         (VALUE_TAKING_OPTIONS + OPTIONAL_VALUE_OPTIONS + FLAG_OPTIONS).freeze,
  value_taking_options:   VALUE_TAKING_OPTIONS,
}.freeze

Constants inherited from Base

Base::DEFAULT_INVOCATION_OPTIONS

Instance Attribute Summary

Attributes inherited from Base

#cli, #metadata

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#aliases, allow_invocation_options, allowed_invocation_options, #bind, binding_sources, binds, command_metadata, declared_metadata, #initialize, #validate_options

Constructor Details

This class inherits a constructor from WifiWand::Commands::Base

Class Method Details

.add_command_options(parser, command_options) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/wifi_wand/commands/log.rb', line 67

def self.add_command_options(parser, command_options)
  parser.on(option_switch(:interval), Float, option_parser_description(:interval)) do |value|
    command_options[:interval] = value
  end

  parser.on(option_switch(:file), option_parser_description(:file)) do |value|
    command_options[:log_file_path] = LogFileManager::DEFAULT_LOG_FILE
    command_options[:log_file_path] = value unless value.nil?
    command_options[:file_destination_requested] = true
  end

  parser.on(option_switch(:stdout), option_parser_description(:stdout)) do
    command_options[:stdout_explicit] = true
  end

  parser.on('--verbose-logs BOOLEAN', TrueClass, 'Enable verbose EventLogger diagnostics') do |value|
    command_options[:verbose_logs] = value
  end
end

.command_option_specsObject



53
# File 'lib/wifi_wand/commands/log.rb', line 53

def self.command_option_specs = COMMAND_OPTION_SPECS

.consume_file_option_value?(args, index, selected_command, value) ⇒ Boolean

Returns:

  • (Boolean)


117
118
119
120
121
122
123
124
125
# File 'lib/wifi_wand/commands/log.rb', line 117

def self.consume_file_option_value?(args, index, selected_command, value)
  return false if value.nil? || value.start_with?('-')
  return true unless selected_command
  return true unless pre_command_option?(args, index, selected_command)

  # A pre-command optional value is a real path only when another copy of
  # the selected command remains later to serve as the command token.
  args[(index + 2)..].include?(selected_command)
end

.help_summary_linesObject



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/wifi_wand/commands/log.rb', line 55

def self.help_summary_lines
  [
    .description,
    "options: #{option_summary(:interval)}, #{option_summary(:file)},",
    option_summary(:stdout),
    'Outputs JSON Lines: one JSON object per event',
    'Internet events are derived from reachable/unreachable state; ' \
      'indeterminate is preserved as unknown',
    'Ctrl+C to stop',
  ]
end

.normalize_command_option_args!(args, selected_command:) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/wifi_wand/commands/log.rb', line 99

def self.normalize_command_option_args!(args, selected_command:)
  index = 0

  while index < args.length
    if args[index] == '--file'
      value = args[index + 1]
      if consume_file_option_value?(args, index, selected_command, value)
        args[index] = "--file=#{value}"
        args.delete_at(index + 1)
      elsif value == selected_command && pre_command_option?(args, index, selected_command)
        args[index] = "--file=#{LogFileManager::DEFAULT_LOG_FILE}"
      end
    end

    index += 1
  end
end

.option_parser_description(option_name) ⇒ Object



91
92
93
# File 'lib/wifi_wand/commands/log.rb', line 91

def self.option_parser_description(option_name)
  OPTION_DEFINITIONS.fetch(option_name).fetch(:parser_description)
end

.option_summary(option_name) ⇒ Object



95
96
97
# File 'lib/wifi_wand/commands/log.rb', line 95

def self.option_summary(option_name)
  OPTION_DEFINITIONS.fetch(option_name).fetch(:summary)
end

.option_switch(option_name) ⇒ Object



87
88
89
# File 'lib/wifi_wand/commands/log.rb', line 87

def self.option_switch(option_name)
  OPTION_DEFINITIONS.fetch(option_name).fetch(:switch)
end

.pre_command_option?(args, index, selected_command) ⇒ Boolean

Returns:

  • (Boolean)


127
128
129
130
# File 'lib/wifi_wand/commands/log.rb', line 127

def self.pre_command_option?(args, index, selected_command)
  command_index = args.index(selected_command)
  command_index && index < command_index
end

Instance Method Details

#call(*options) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/wifi_wand/commands/log.rb', line 139

def call(*options)
  parse_result = parse_options(options)
  return if parse_result == :skip_execution

  if model.runtime_config.verbose
    raise WifiWand::ConfigurationError, <<~MESSAGE.chomp
      Global --verbose is incompatible with the log command: OS command tracing writes plain text that corrupts the JSONL output stream. From the CLI, run `wifiwand log` without global verbose, or use `wifiwand log --verbose-logs true` for EventLogger diagnostics.
    MESSAGE
  end

  interval, log_file_path, output_to_stdout, verbose_flag = parse_result
  logger_out_stream = output_to_stdout ? output : nil

  logger = build_logger(
    interval:          interval,
    verbose_flag:      verbose_flag,
    log_file_path:     log_file_path,
    logger_out_stream: logger_out_stream
  )

  logger.run
end

#help_textObject



134
135
136
137
# File 'lib/wifi_wand/commands/log.rb', line 134

def help_text
  # Reuse the command parser as the single source of truth for help text.
  build_parser(command_options: {}, help_setter: -> {}).help
end

#verbose?Boolean

Returns:

  • (Boolean)


132
# File 'lib/wifi_wand/commands/log.rb', line 132

def verbose? = @verbose_flag