Class: Giblish::AsciidoctorLogger::UserInfoFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/giblish/utils.rb

Overview

log formatter specialized for formatting messages from asciidoctor’s stdout, handles the different log record types that Asciidoctor emits

Constant Summary collapse

SEVERITY_LABELS =
{"WARN" => "WARNING", "FATAL" => "FAILED"}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.adoc_to_message(msg) ⇒ Object

The hash that can be emitted as the msg from asciidoctor have the following format:

:source_location=>#<Asciidoctor::Reader::Cursor:0x000055e65a8729e0
        @file="<full adoc filename>",
        @dir="<full src dir>",
        @path="<only file name>",
        @lineno=<src line no>



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

def self.adoc_to_message(msg)
  case msg
  when ::String
    msg
  when ::Hash
    # asciidoctor seem to emit a hash with the following structure on errors:
    # :text => String
    # :source_location => Reader::Cursor with the following props:
    #   dir, file, lineno, path
    # Only the lineno prop can be trusted when Asciidoctor is used via Giblish
    #
    src_loc = msg.fetch(:source_location, nil)
    err_txt = msg.fetch(:text, "")
    str = ""
    str << "Line #{src_loc.lineno} - " if src_loc&.lineno
    str << err_txt
    str
  else
    msg.inspect
  end
end

Instance Method Details

#call(severity, datetime, progname, msg) ⇒ Object



52
53
54
# File 'lib/giblish/utils.rb', line 52

def call(severity, datetime, progname, msg)
  %(#{datetime.strftime("%H:%M:%S")} #{progname}: #{SEVERITY_LABELS[severity] || severity}: #{UserInfoFormatter.adoc_to_message(msg)}\n)
end