module Sord::Logging

Handles writing logs to stdout and any other classes which request them.

Constants

AVAILABLE_TYPES

An array of all available logging types.

Public Class Methods

add_hook(&blk) click to toggle source

Adds a hook to the logger. @yieldparam [Symbol] kind The kind of log message this is. @yieldparam [String] msg The log message to write. @yieldparam [YARD::CodeObjects::Base] item The CodeObject which this log

is associated with, if any. This is shown before the log message if it is
specified.

@yieldreturn [void] @return [void]

# File lib/sord/logging.rb, line 186
def self.add_hook(&blk)
  @@hooks << blk
end
done(msg, item = nil) click to toggle source

Print a done message. This should be used when a process completes successfully. @param [String] msg The log message to write. @param [YARD::CodeObjects::Base] item The CodeObject which this log

is associated with, if any. This is shown before the log message if it is
specified.

@return [void]

# File lib/sord/logging.rb, line 161
def self.done(msg, item = nil)
  generic(:done, Rainbow('[DONE ]').green, msg, item)
end
duck(msg, item = nil) click to toggle source

Print a duck-typing message. This should be used when the YARD documentation contains duck typing, which isn't supported by Sorbet, so it is substituted for something different. @param [String] msg The log message to write. @param [YARD::CodeObjects::Base] item The CodeObject which this log

is associated with, if any. This is shown before the log message if it is
specified.

@return [void]

# File lib/sord/logging.rb, line 115
def self.duck(msg, item = nil)
  generic(:duck, Rainbow('[DUCK ]').cyan, msg, item)
end
enabled_types() click to toggle source

Gets the array of log messages types which should be processed. Any not on this list will be discarded. @return [Array<Symbol>] @return [void]

# File lib/sord/logging.rb, line 50
def self.enabled_types
  @@enabled_types
end
enabled_types=(value) click to toggle source

Sets the array of log messages types which should be processed. Any not on this list will be discarded. This should be a subset of AVAILABLE_TYPES. @param [Array<Symbol>] value @return [void]

# File lib/sord/logging.rb, line 41
def self.enabled_types=(value)
  raise 'invalid types' unless valid_types?(value)
  @@enabled_types = value
end
error(msg, item = nil) click to toggle source

Print an error message. This should be used for things which require the current process to stop. @param [String] msg The log message to write. @param [YARD::CodeObjects::Base] item The CodeObject which this log

is associated with, if any. This is shown before the log message if it is
specified.

@return [void]

# File lib/sord/logging.rb, line 126
def self.error(msg, item = nil)
  generic(:error, Rainbow('[ERROR]').red, msg, item)
end
generic(kind, header, msg, item) click to toggle source

A generic log message writer which is called by all other specific logging methods. This shouldn't be called outside of the Logging class itself. @param [Symbol] kind The kind of log message this is. @param [String] header The prefix for this log message. For consistency,

it should be up to five uppercase characters wrapped in square brackets,
with some unique colour applied.

@param [String] msg The log message to write. @param [YARD::CodeObjects::Base] item The CodeObject which this log

is associated with, if any. This is shown before the log message if it is
specified.

@return [void]

# File lib/sord/logging.rb, line 73
def self.generic(kind, header, msg, item)
  return unless enabled_types.include?(kind)

  if item
    puts "#{header} (#{Rainbow(item.path).bold}) #{msg}" unless silent?
  else
    puts "#{header} #{msg}" unless silent?
  end

  invoke_hooks(kind, msg, item)
end
hooks() click to toggle source

@return [Array<Proc>] The hooks registered on the logger.

# File lib/sord/logging.rb, line 12
def self.hooks
  @@hooks
end
infer(msg, item = nil) click to toggle source

Print an infer message. This should be used when the user should be told that some information has been filled in or guessed for them, and that information is likely correct. @param [String] msg The log message to write. @param [YARD::CodeObjects::Base] item The CodeObject which this log

is associated with, if any. This is shown before the log message if it is
specified.

@return [void]

# File lib/sord/logging.rb, line 138
def self.infer(msg, item = nil)
  generic(:infer, Rainbow('[INFER]').blue, msg, item)
end
info(msg, item = nil) click to toggle source

Print an info message. This should be used for generic informational messages which the user doesn't need to act on. @param [String] msg The log message to write. @param [YARD::CodeObjects::Base] item The CodeObject which this log

is associated with, if any. This is shown before the log message if it is
specified.

@return [void]

# File lib/sord/logging.rb, line 103
def self.info(msg, item = nil)
  generic(:info, '[INFO ]', msg, item)
end
invoke_hooks(kind, msg, item) click to toggle source

Invokes all registered hooks on the logger. @param [Symbol] kind The kind of log message this is. @param [String] msg The log message to write. @param [YARD::CodeObjects::Base] item The CodeObject which this log

is associated with, if any. This is shown before the log message if it is
specified.

@return [void]

# File lib/sord/logging.rb, line 172
def self.invoke_hooks(kind, msg, item)
  @@hooks.each do |hook|
    hook.(kind, msg, item) rescue nil
  end
end
omit(msg, item = nil) click to toggle source

Print an omit message. This should be used as a special type of warning to alert the user that there is some information missing, but this information is not critical to the completion of the process. @param [String] msg The log message to write. @param [YARD::CodeObjects::Base] item The CodeObject which this log

is associated with, if any. This is shown before the log message if it is
specified.

@return [void]

# File lib/sord/logging.rb, line 150
def self.omit(msg, item = nil)
  generic(:omit, Rainbow('[OMIT ]').magenta, msg, item)
end
silent=(value) click to toggle source

Sets whether log messages should be printed or not. @param [Boolean] value @return [void]

# File lib/sord/logging.rb, line 28
def self.silent=(value)
  @@silent = value
end
silent?() click to toggle source

@return [Boolean] Whether log messages should be printed or not. This is

used for testing.
# File lib/sord/logging.rb, line 21
def self.silent?
  @@silent
end
valid_types?(value) click to toggle source

Returns a boolean indicating whether a given array is a valid value for enabled_types. @param [Array<Symbol>] value @return [void]

# File lib/sord/logging.rb, line 58
def self.valid_types?(value)
  (value - AVAILABLE_TYPES).empty?
end
warn(msg, item = nil) click to toggle source

Print a warning message. This should be used for things which require the user's attention but do not prevent the process from stopping. @param [String] msg The log message to write. @param [YARD::CodeObjects::Base] item The CodeObject which this log

is associated with, if any. This is shown before the log message if it is
specified.

@return [void]

# File lib/sord/logging.rb, line 92
def self.warn(msg, item = nil)
  generic(:warn, Rainbow('[WARN ]').yellow, msg, item)
end