Re: Challenge: Build a Color-Coded Log Viewer for Phix/Euphoria (Inspired by chat.exw)

new topic     » goto parent     » topic index » view thread      » older message » newer message

Here is a program that converts a log in a colored HTML file.

include std/filesys.e 
include std/io.e 
include std/search.e 
 
sequence cmd = command_line() 
if length(cmd) < 3 then 
  puts(STDERR, "Syntax: html_log file\n") 
  abort(1) 
end if 
sequence log_file = cmd[3] 
 
integer f_out = open(filebase(log_file) & ".html", "w") 
puts(f_out, "<!DOCTYPE html>\n<html>\n<title>HTML Log</title>\n" & 
  "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n" & 
  "<link rel=\"stylesheet\" href=\"https://www.w3schools.com/w3css/5/w3.css\">\n<body>" 
) 
sequence lines = read_lines(log_file) 
if begins({#EF,#BB,#BF}, lines[1]) then  -- UTF-8 BOM 
  lines[1] = remove(lines[1], 1, 3) 
end if 
for i = 1 to length( lines ) label "main" do 
  if length(lines[i]) = 0 then continue end if 
  if begins("[verbose]", lines[i]) then 
    puts(f_out, "<div class=\"w3-container w3-light-grey\">\n") 
    puts(f_out, lines[i] & "\n") 
    puts(f_out, "</div>\n") 
  elsif begins("[debug]", lines[i]) then 
    puts(f_out, "<div class=\"w3-container w3-white\">\n") 
    puts(f_out, lines[i] & "\n") 
    puts(f_out, "</div>\n") 
  elsif begins("[warning]", lines[i]) then 
    puts(f_out, "<div class=\"w3-container w3-pale-yellow\">\n") 
    puts(f_out, lines[i] & "\n") 
    puts(f_out, "</div>\n") 
  elsif begins("[error]", lines[i]) then 
    puts(f_out, "<div class=\"w3-container w3-pale-red\">\n") 
    puts(f_out, lines[i] & "\n") 
    puts(f_out, "</div>\n") 
  elsif begins("[info]", lines[i]) then 
    puts(f_out, "<div class=\"w3-container w3-pale-green\">\n") 
    puts(f_out, lines[i] & "\n") 
    puts(f_out, "</div>\n") 
  else 
    puts(f_out, lines[i] & "\n") 
  end if 
end for   
puts(f_out, "</body>\n</html>") 
close(f_out) 

Jean-Marc

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu