Class: Doing::LogAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/doing/log_adapter.rb

Overview

Log adapter

Constant Summary collapse

TOPIC_WIDTH =
12
LOG_LEVELS =
{
  debug: ::Logger::DEBUG,
  info: ::Logger::INFO,
  warn: ::Logger::WARN,
  error: ::Logger::ERROR
}.freeze
COUNT_KEYS =
%i[
  added
  added_tags
  archived
  autotag
  completed
  completed_archived
  deleted
  moved
  removed_tags
  skipped
  updated
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(level = :info) ⇒ LogAdapter

Create a new instance of a log writer

Parameters:

  • level (optional, symbol) (defaults to: :info) —

    the log level



40
41
42
43
44
45
46
47
48
# File 'lib/doing/log_adapter.rb', line 40

def initialize(level = :info)
  @messages = []
  @counters = {}
  COUNT_KEYS.each { |key| @counters[key] = { tag: [], count: 0 } }
  @results = []
  @logdev = $stderr
  @max_length = `tput cols`.strip.to_i - 5 || 85
  self.log_level = level
end

Instance Attribute Details

#level ⇒ Object (readonly)

Returns the value of attribute level.



10
11
12
# File 'lib/doing/log_adapter.rb', line 10

def level
  @level
end

#logdev=(value) ⇒ Object (writeonly)

Sets the attribute logdev

Parameters:

  • value —

    the value to set the attribute logdev to.



8
9
10
# File 'lib/doing/log_adapter.rb', line 8

def logdev=(value)
  @logdev = value
end

#max_length=(value) ⇒ Object (writeonly)

Sets the attribute max_length

Parameters:

  • value —

    the value to set the attribute max_length to.



8
9
10
# File 'lib/doing/log_adapter.rb', line 8

def max_length=(value)
  @max_length = value
end

#messages ⇒ Object (readonly)

Returns the value of attribute messages.



10
11
12
# File 'lib/doing/log_adapter.rb', line 10

def messages
  @messages
end

#results ⇒ Object (readonly)

Returns the value of attribute results.



10
11
12
# File 'lib/doing/log_adapter.rb', line 10

def results
  @results
end

Instance Method Details

#abort_with(topic, message = nil, &block) ⇒ Object

Print an error message and immediately abort the process

Parameters:

  • topic —

    the topic of the message, e.g. "Configuration file", "Deprecation", etc.

  • message (defaults to: nil) —

    the message detail (can be omitted)

Returns:

  • nothing



159
160
161
162
# File 'lib/doing/log_adapter.rb', line 159

def abort_with(topic, message = nil, &block)
  error(topic, message, &block)
  abort
end

#adjust_verbosity(options = {}) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/doing/log_adapter.rb', line 74

def adjust_verbosity(options = {})
  if options[:quiet]
    self.log_level = :error
  elsif options[:verbose] || options[:debug]
    self.log_level = :debug
  end
  log_now :debug, 'Logging at level:', @level.to_s
  # log_now :debug, 'Doing Version:', Doing::VERSION
end

#count(key, level: :info, count: 1, tag: nil, message: nil) ⇒ Object

Raises:

  • (ArgumentError)


84
85
86
87
88
89
90
91
# File 'lib/doing/log_adapter.rb', line 84

def count(key, level: :info, count: 1, tag: nil, message: nil)
  raise ArgumentError, 'invalid counter key' unless COUNT_KEYS.include?(key)

  @counters[key][:count] += count
  @counters[key][:tag].concat(tag).sort.uniq unless tag.nil?
  @counters[key][:level] ||= level
  @counters[key][:message] ||= message
end

#debug(topic, message = nil, &block) ⇒ Object

Print a debug message

Parameters:

  • topic —

    the topic of the message

  • message (defaults to: nil) —

    the message detail

Returns:

  • nothing



101
102
103
# File 'lib/doing/log_adapter.rb', line 101

def debug(topic, message = nil, &block)
  write(:debug, topic, message, &block)
end

#error(topic, message = nil, &block) ⇒ Object

Print an error message

Parameters:

  • topic —

    the topic of the message, e.g. "Configuration file", "Deprecation", etc.

  • message (defaults to: nil) —

    the message detail

Returns:

  • nothing



143
144
145
# File 'lib/doing/log_adapter.rb', line 143

def error(topic, message = nil, &block)
  write(:error, topic, message, &block)
end

#formatted_topic(topic, colon: false) ⇒ Object

Format the topic

Parameters:

  • topic —

    the topic of the message, e.g. "Configuration file", "Deprecation", etc.

  • colon (defaults to: false) —

    Separate with a colon?

Returns:

  • the formatted topic statement



174
175
176
177
178
179
180
181
182
# File 'lib/doing/log_adapter.rb', line 174

def formatted_topic(topic, colon: false)
  if colon
    "#{topic}: ".rjust(TOPIC_WIDTH)
  elsif topic =~ /:$/
    "#{topic} ".rjust(TOPIC_WIDTH)
  else
    "#{topic} "
  end
end

#info(topic, message = nil, &block) ⇒ Object

Print a message

Parameters:

  • topic —

    the topic of the message, e.g. "Configuration file", "Deprecation", etc.

  • message (defaults to: nil) —

    the message detail

Returns:

  • nothing



115
116
117
# File 'lib/doing/log_adapter.rb', line 115

def info(topic, message = nil, &block)
  write(:info, topic, message, &block)
end

#log_level=(level = 'info') ⇒ Object

Set the log level on the writer

Parameters:

  • level (symbol) (defaults to: 'info') —

    the log level

Returns:

  • nothing



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/doing/log_adapter.rb', line 57

def log_level=(level = 'info')
  level = level.to_s

  level = case level
          when /^[e0]/i
            :error
          when /^[w1]/i
            :warn
          when /^[d3]/i
            :debug
          else
            :info
          end

  @level = level
end

#log_now(level, topic, message = nil, &block) ⇒ Object

Log to console immediately instead of writing messages on exit

Parameters:

  • level (Symbol) —

    The level

  • topic (String) —

    The topic or full message

  • message (String) (defaults to: nil) —

    The message (optional)

  • block —

    a block containing the message (optional)



213
214
215
216
217
218
219
220
221
# File 'lib/doing/log_adapter.rb', line 213

def log_now(level, topic, message = nil, &block)
  return false unless write_message?(level)

  if @logdev == $stdout
    @logdev.puts message(topic, message, &block)
  else
    @logdev.puts color_message(level, topic, message, &block)
  end
end

#output_results ⇒ Object

Output registers based on log level

Returns:

  • nothing



228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/doing/log_adapter.rb', line 228

def output_results
  total_counters

  results = @results.select { |msg| write_message?(msg[:level]) }.uniq

  if @logdev == $stdout
    $stdout.print results.map {|res| res[:message].uncolor }.join("\n")
  else
    results.each do |msg|
      @logdev.puts color_message(msg[:level], msg[:message])
    end
  end
end

#warn(topic, message = nil, &block) ⇒ Object

Print a message

Parameters:

  • topic —

    the topic of the message, e.g. "Configuration file", "Deprecation", etc.

  • message (defaults to: nil) —

    the message detail

Returns:

  • nothing



129
130
131
# File 'lib/doing/log_adapter.rb', line 129

def warn(topic, message = nil, &block)
  write(:warn, topic, message, &block)
end

#write(level_of_message, topic, message = nil, &block) ⇒ Boolean

Log a message.

Parameters:

  • level_of_message (Symbol) —

    the Symbol level of message, one of :debug, :info, :warn, :error

  • topic (String) —

    the String topic or full message

  • message (String) (defaults to: nil) —

    the String message (optional)

  • block —

    a block containing the message (optional)

Returns:

  • (Boolean) —

    false if the message was not written



200
201
202
203
# File 'lib/doing/log_adapter.rb', line 200

def write(level_of_message, topic, message = nil, &block)
  @results << { level: level_of_message, message: message(topic, message, &block) }
  true
end