Class: Metanorma::Utils::Log
- Inherits:
-
Object
- Object
- Metanorma::Utils::Log
- Defined in:
- lib/utils/log.rb
Instance Attribute Summary collapse
-
#suppress_log ⇒ Object
writeonly
Sets the attribute suppress_log.
-
#xml ⇒ Object
writeonly
Sets the attribute xml.
Instance Method Summary collapse
- #abort_messages ⇒ Object
-
#add(category, loc, msg, severity: 2, display: true) ⇒ Object
severity: 0: abort; 1: serious; 2: not serious; 3: info only.
- #break_up_long_str(str, threshold, punct) ⇒ Object
- #context(node) ⇒ Object
- #context_render(entry) ⇒ Object
- #create_entry(loc, msg, severity) ⇒ Object
- #current_location(node) ⇒ Object
-
#human_readable_xml(node) ⇒ Object
try to approximate input, at least for maths.
- #index_severities(entries) ⇒ Object
-
#initialize ⇒ Log
constructor
A new instance of Log.
- #line(node, msg) ⇒ Object
- #loc_link(entry) ⇒ Object
- #loc_to_url(loc) ⇒ Object
- #log_hdr(file) ⇒ Object
- #log_index ⇒ Object
- #mapid(old, new) ⇒ Object
- #render_preproc_entry(entry) ⇒ Object
- #save_to(filename, dir = nil) ⇒ Object
- #suppress_display?(category, _loc, _msg, display) ⇒ Boolean
- #suppress_log?(category, severity, msg) ⇒ Boolean
- #to_ncname(tag) ⇒ Object
- #write(file = nil) ⇒ Object
- #write_entry(file, entry) ⇒ Object
- #write_key(file, key) ⇒ Object
Constructor Details
#initialize ⇒ Log
Returns a new instance of Log.
8 9 10 11 12 13 |
# File 'lib/utils/log.rb', line 8 def initialize @log = {} @c = HTMLEntities.new @mapid = {} @suppress_log = { severity: 4, category: [] } end |
Instance Attribute Details
#suppress_log=(value) ⇒ Object (writeonly)
Sets the attribute suppress_log
6 7 8 |
# File 'lib/utils/log.rb', line 6 def suppress_log=(value) @suppress_log = value end |
#xml=(value) ⇒ Object (writeonly)
Sets the attribute xml
6 7 8 |
# File 'lib/utils/log.rb', line 6 def xml=(value) @xml = value end |
Instance Method Details
#abort_messages ⇒ Object
38 39 40 41 42 43 44 |
# File 'lib/utils/log.rb', line 38 def @log.values.each_with_object([]) do |v, m| v.each do |e| e[:severity].zero? and m << e[:message] end end end |
#add(category, loc, msg, severity: 2, display: true) ⇒ Object
severity: 0: abort; 1: serious; 2: not serious; 3: info only
28 29 30 31 32 33 34 35 36 |
# File 'lib/utils/log.rb', line 28 def add(category, loc, msg, severity: 2, display: true) @novalid || suppress_log?(category, severity, msg) and return @log[category] ||= [] item = create_entry(loc, msg, severity) @log[category] << item loc = loc.nil? ? "" : "(#{current_location(loc)}): " suppress_display?(category, loc, msg, display) or warn "#{category}: #{loc}#{msg}" end |
#break_up_long_str(str, threshold, punct) ⇒ Object
223 224 225 |
# File 'lib/utils/log.rb', line 223 def break_up_long_str(str, threshold, punct) Metanorma::Utils.break_up_long_str(str, threshold, punct) end |
#context(node) ⇒ Object
111 112 113 114 115 116 |
# File 'lib/utils/log.rb', line 111 def context(node) node.is_a? String and return nil node.respond_to?(:to_xml) and return human_readable_xml(node) node.respond_to?(:to_s) and return node.to_s nil end |
#context_render(entry) ⇒ Object
196 197 198 199 200 |
# File 'lib/utils/log.rb', line 196 def context_render(entry) entry[:context] or return nil entry[:context].split("\n").first(5) .join("\n").gsub("><", "> <") end |
#create_entry(loc, msg, severity) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/utils/log.rb', line 57 def create_entry(loc, msg, severity) msg = msg.encode("UTF-8", invalid: :replace, undef: :replace) item = { location: current_location(loc), severity: severity, message: msg, context: context(loc), line: line(loc, msg) } if item[:message].include?(" :: ") a = item[:message].split(" :: ", 2) item[:context] = a[1] item[:message] = a[0] end item end |
#current_location(node) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/utils/log.rb', line 69 def current_location(node) if node.nil? then "" elsif node.respond_to?(:id) && !node.id.nil? then "ID #{node.id}" elsif node.respond_to?(:id) && node.id.nil? && node.respond_to?(:parent) while !node.nil? && node.id.nil? node = node.parent end node.nil? ? "" : "ID #{node.id}" elsif node.respond_to?(:to_xml) && node.respond_to?(:parent) while !node.nil? && node["id"].nil? && node.respond_to?(:parent) node = node.parent end node.respond_to?(:parent) ? "ID #{node['id']}" : "" elsif node.is_a? String then node elsif node.respond_to?(:lineno) && !node.lineno.nil? && !node.lineno.empty? "Asciidoctor Line #{'%06d' % node.lineno}" elsif node.respond_to?(:line) && !node.line.nil? "XML Line #{'%06d' % node.line}" elsif node.respond_to?(:parent) while !node.nil? && (!node.respond_to?(:level) || node.level.positive?) && (!node.respond_to?(:context) || node.context != :section) node = node.parent return "Section: #{node.title}" if node.respond_to?(:context) && node&.context == :section end "??" else "??" end end |
#human_readable_xml(node) ⇒ Object
try to approximate input, at least for maths
119 120 121 122 123 124 125 126 127 |
# File 'lib/utils/log.rb', line 119 def human_readable_xml(node) ret = node.dup ret.xpath(".//*[local-name() = 'stem']").each do |s| sub = s.at("./*[local-name() = 'asciimath'] | " \ "./*[local-name() = 'latexmath']") sub and s.replace(sub) end ret.to_xml end |
#index_severities(entries) ⇒ Object
153 154 155 156 157 158 159 160 161 |
# File 'lib/utils/log.rb', line 153 def index_severities(entries) s = entries.each_with_object({}) do |e, m| m[e[:severity]] ||= 0 m[e[:severity]] += 1 end.compact s.keys.sort.map do |k| "Severity #{k}: <b>#{s[k]}</b> errors" end.join("; ") end |
#line(node, msg) ⇒ Object
101 102 103 104 105 106 107 108 109 |
# File 'lib/utils/log.rb', line 101 def line(node, msg) if node.respond_to?(:line) && !node.line.nil? "#{'%06d' % node.line}" elsif /^XML Line /.match?(msg) msg.sub(/^XML Line /, "").sub(/(^[^:]+):.*$/, "\\1") else "000000" end end |
#loc_link(entry) ⇒ Object
206 207 208 209 210 211 212 213 |
# File 'lib/utils/log.rb', line 206 def loc_link(entry) loc = entry[:location] loc.nil? || loc.empty? and loc = "--" loc, url = loc_to_url(loc) loc &&= break_up_long_str(loc, 10, 2) url and loc = "<a href='#{url}'>#{loc}</a>" loc end |
#loc_to_url(loc) ⇒ Object
215 216 217 218 219 220 221 |
# File 'lib/utils/log.rb', line 215 def loc_to_url(loc) /^ID /.match?(loc) or return [loc, nil] loc.sub!(/^ID /, "") loc = @mapid[loc] while @mapid[loc] url = "#{@htmlfilename}##{loc}" [loc, url] end |
#log_hdr(file) ⇒ Object
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 |
# File 'lib/utils/log.rb', line 129 def log_hdr(file) <<~HTML <html><head><title>#{file} errors</title> <meta charset="UTF-8"/> <style> pre { white-space: pre-wrap; } thead th { font-weight: bold; background-color: aqua; } .severity0 { font-weight: bold; background-color: lightpink } .severity1 { font-weight: bold; } .severity2 { } .severity3 { font-style: italic; color: grey; } </style> </head><body><h1>#{file} errors</h1> <ul>#{log_index}</ul> HTML end |
#log_index ⇒ Object
145 146 147 148 149 150 151 |
# File 'lib/utils/log.rb', line 145 def log_index @log.each_with_object([]) do |(k, v), m| m << <<~HTML <li><p><b><a href="##{to_ncname(k)}">#{k}</a></b>: #{index_severities(v)}</p></li> HTML end.join("\n") end |
#mapid(old, new) ⇒ Object
202 203 204 |
# File 'lib/utils/log.rb', line 202 def mapid(old, new) @mapid[old] = new end |
#render_preproc_entry(entry) ⇒ Object
186 187 188 189 190 191 192 193 194 |
# File 'lib/utils/log.rb', line 186 def render_preproc_entry(entry) ret = entry.dup ret[:line] = nil if ret[:line] == "000000" ret[:location] = loc_link(entry) ret[:message] = break_up_long_str(entry[:message], 10, 2) .gsub(/`([^`]+)`/, "<code>\\1</code>") ret[:context] = context_render(entry) ret.compact end |
#save_to(filename, dir = nil) ⇒ Object
19 20 21 22 23 24 25 |
# File 'lib/utils/log.rb', line 19 def save_to(filename, dir = nil) dir ||= File.dirname(filename) new_fn = filename.sub(/\.err\.html$/, ".html") b = File.join(dir, File.basename(new_fn, ".*")) @filename = "#{b}.err.html" @htmlfilename = "#{b}.html" end |
#suppress_display?(category, _loc, _msg, display) ⇒ Boolean
52 53 54 55 |
# File 'lib/utils/log.rb', line 52 def suppress_display?(category, _loc, _msg, display) ["Metanorma XML Syntax", "Relaton"].include?(category) || !display end |
#suppress_log?(category, severity, msg) ⇒ Boolean
46 47 48 49 50 |
# File 'lib/utils/log.rb', line 46 def suppress_log?(category, severity, msg) category == "Relaton" && /^Fetching /.match?(msg) || @suppress_log[:severity] <= severity || @suppress_log[:category].include?(category) end |
#to_ncname(tag) ⇒ Object
15 16 17 |
# File 'lib/utils/log.rb', line 15 def to_ncname(tag) ::Metanorma::Utils.to_ncname(tag) end |
#write(file = nil) ⇒ Object
163 164 165 166 167 168 169 170 |
# File 'lib/utils/log.rb', line 163 def write(file = nil) (!file && @filename) or save_to(file || "metanorma", nil) File.open(@filename, "w:UTF-8") do |f| f.puts log_hdr(@filename) @log.each_key { |key| write_key(f, key) } f.puts "</body></html>\n" end end |
#write_entry(file, entry) ⇒ Object
227 228 229 230 231 232 233 234 |
# File 'lib/utils/log.rb', line 227 def write_entry(file, entry) entry[:context] &&= @c.encode(break_up_long_str(entry[:context], 40, 2)) file.print <<~HTML <tr class="severity#{entry[:severity]}"> <td>#{entry[:line]}</td><th><code>#{entry[:location]}</code></th> <td>#{entry[:]}</td><td><pre>#{entry[:context]}</pre></td><td>#{entry[:severity]}</td></tr> HTML end |
#write_key(file, key) ⇒ Object
172 173 174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/utils/log.rb', line 172 def write_key(file, key) file.puts <<~HTML <h2 id="#{to_ncname(key)}">#{key}</h2>\n<table border="1"> <thead><th width="5%">Line</th><th width="20%">ID</th> <th width="30%">Message</th><th width="40%">Context</th><th width="5%">Severity</th></thead> <tbody> HTML @log[key].sort_by { |a| [a[:line], a[:location], a[:message]] } .each do |n| write_entry(file, render_preproc_entry(n)) end file.puts "</tbody></table>\n" end |