"Fossies" - the Fresh Open Source Software Archive

Member "asciidoctor-2.0.10/lib/asciidoctor/logging.rb" (1 Jun 2019, 2782 Bytes) of package /linux/www/asciidoctor-2.0.10.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Ruby source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 # frozen_string_literal: true
    2 require 'logger'
    3 
    4 module Asciidoctor
    5 class Logger < ::Logger
    6   attr_reader :max_severity
    7 
    8   def initialize *args
    9     super
   10     self.progname = 'asciidoctor'
   11     self.formatter = BasicFormatter.new
   12     self.level = WARN
   13   end
   14 
   15   def add severity, message = nil, progname = nil
   16     if (severity ||= UNKNOWN) > (@max_severity ||= severity)
   17       @max_severity = severity
   18     end
   19     super
   20   end
   21 
   22   class BasicFormatter < Formatter
   23     SEVERITY_LABELS = { 'WARN' => 'WARNING', 'FATAL' => 'FAILED' }
   24 
   25     def call severity, _, progname, msg
   26       %(#{progname}: #{SEVERITY_LABELS[severity] || severity}: #{::String === msg ? msg : msg.inspect}#{LF})
   27     end
   28   end
   29 
   30   module AutoFormattingMessage
   31     def inspect
   32       (sloc = self[:source_location]) ? %(#{sloc}: #{self[:text]}) : self[:text]
   33     end
   34   end
   35 end
   36 
   37 class MemoryLogger < ::Logger
   38   SEVERITY_LABELS = {}.tap {|accum| (Severity.constants false).each {|c| accum[Severity.const_get c, false] = c } }
   39 
   40   attr_reader :messages
   41 
   42   def initialize
   43     self.level = WARN
   44     @messages = []
   45   end
   46 
   47   def add severity, message = nil, progname = nil
   48     message = block_given? ? yield : progname unless message
   49     @messages << { severity: SEVERITY_LABELS[severity || UNKNOWN], message: message }
   50     true
   51   end
   52 
   53   def clear
   54     @messages.clear
   55   end
   56 
   57   def empty?
   58     @messages.empty?
   59   end
   60 
   61   def max_severity
   62     empty? ? nil : @messages.map {|m| Severity.const_get m[:severity], false }.max
   63   end
   64 end
   65 
   66 class NullLogger < ::Logger
   67   attr_reader :max_severity
   68 
   69   def initialize
   70     self.level = WARN
   71   end
   72 
   73   def add severity, message = nil, progname = nil
   74     if (severity ||= UNKNOWN) > (@max_severity ||= severity)
   75       @max_severity = severity
   76     end
   77     true
   78   end
   79 end
   80 
   81 module LoggerManager
   82   @logger_class = Logger
   83   class << self
   84     attr_accessor :logger_class
   85 
   86     # NOTE subsequent calls to logger access the logger via the logger property directly
   87     def logger pipe = $stderr
   88       memoize_logger
   89       @logger ||= (@logger_class.new pipe)
   90     end
   91 
   92     def logger= new_logger
   93       @logger = new_logger || (@logger_class.new $stderr)
   94     end
   95 
   96     private
   97 
   98     def memoize_logger
   99       class << self
  100         alias logger logger # suppresses warning from CRuby
  101         attr_reader :logger
  102       end
  103     end
  104   end
  105 end
  106 
  107 module Logging
  108   # Private: Mixes the {Logging} module as static methods into any class that includes the {Logging} module.
  109   #
  110   # into - The Class that includes the {Logging} module
  111   #
  112   # Returns nothing
  113   private_class_method def self.included into
  114     into.extend Logging
  115   end || :included
  116 
  117   def logger
  118     LoggerManager.logger
  119   end
  120 
  121   def message_with_context text, context = {}
  122     ({ text: text }.merge context).extend Logger::AutoFormattingMessage
  123   end
  124 end
  125 end