From 343077218254bccdd7e518c5cc74d473d0bbf605 Mon Sep 17 00:00:00 2001 From: Marius Date: Thu, 9 Aug 2012 01:00:13 +0300 Subject: beta 2012.08.08 23:45 --- tex/context/base/trac-fil.lua | 193 +++++++++++++++++++++++++----------------- 1 file changed, 114 insertions(+), 79 deletions(-) (limited to 'tex/context/base/trac-fil.lua') diff --git a/tex/context/base/trac-fil.lua b/tex/context/base/trac-fil.lua index 73d5d1c7d..8cc903e2a 100644 --- a/tex/context/base/trac-fil.lua +++ b/tex/context/base/trac-fil.lua @@ -6,13 +6,14 @@ if not modules then modules = { } end modules ['trac-fil'] = { license = "see context related readme files" } -local rawset, tonumber = rawset, tonumber +local rawset, tonumber, type, pcall = rawset, tonumber, type, pcall local format, concat = string.format, table.concat local openfile = io.open local date = os.date local sortedpairs = table.sortedpairs -local P, C, Cc, Cg, Cf, Ct, Cs, lpegmatch = lpeg.P, lpeg.C, lpeg.Cc, lpeg.Cg, lpeg.Cf, lpeg.Ct, lpeg.Cs, lpeg.match +local P, C, Cc, Cg, Cf, Ct, Cs, Carg = lpeg.P, lpeg.C, lpeg.Cc, lpeg.Cg, lpeg.Cf, lpeg.Ct, lpeg.Cs, lpeg.Carg +local lpegmatch = lpeg.match local patterns = lpeg.patterns local cardinal = patterns.cardinal @@ -44,29 +45,45 @@ patterns.timestamp = timestamp loggers = loggers or { } -local tz = os.timezone(true) - -local bugged = { } - -function loggers.message(filename,t) - if not bugged[filename] then - local f = openfile(filename,"a+") - if not f then - dir.mkdirs(file.dirname(filename)) - f = openfile(filename,"a+") - end - if f then - -- if needed we can speed this up with a concat - f:write("[",date("!%Y-%m-%d %H:%M:%S"),tz,"]") - for k, v in sortedpairs(t) do - f:write(format(" %s=%q",k,v)) +local timeformat = format("[%%s%s]",os.timezone(true)) +local dateformat = "!%Y-%m-%d %H:%M:%S" + +function loggers.makeline(t) + local result = { } -- minimize time that file is open + result[#result+1] = format(timeformat,date(dateformat)) + for k, v in sortedpairs(t) do + local tv = type(v) + if tv == "string" then + if v ~= "password" then + result[#result+1] = format(" %s=%q",k,v) end - f:write("\n") - f:close() - else - bugged[filename] = true + elseif tv == "number" or tv == "boolean" then + result[#result+1] = format(" %s=%q",k,tostring(v)) end end + return concat(result," ") +end + +local function append(filename,...) + local f = openfile(filename,"a+") + if not f then + dir.mkdirs(file.dirname(filename)) + f = openfile(filename,"a+") + end + if f then + f:write(...) + f:close() + return true + else + return false + end +end + +function loggers.store(filename,data) -- a log service is nicer + if type(data) == "table"then + data = loggers.makeline(data) + end + pcall(append,filename,data,"\n") end function loggers.collect(filename,result) @@ -87,60 +104,78 @@ function loggers.collect(filename,result) end end ---~ local template = [[ ---~ ---~ %s ---~ %s ---~
---~ ]] - ---~ function loggers.tohtml(entries,fields) ---~ if not fields or #fields == 0 then ---~ return "" ---~ end ---~ if type(entries) == "string" then ---~ entries = loggers.collect(entries) ---~ end ---~ local scratch, lines = { }, { } ---~ for i=1,#entries do ---~ local entry = entries[i] ---~ local status = entry.status ---~ for i=1,#fields do ---~ local field = fields[i] ---~ local v = status[field.name] ---~ if v ~= nil then ---~ v = tostring(v) ---~ local f = field.format ---~ if f then v = format(f,v) end ---~ scratch[i] = format("%s",field.align or "left",v) ---~ else ---~ scratch[i] = "" ---~ end ---~ end ---~ lines[i] = "" .. concat(scratch) .. "" ---~ end ---~ for i=1,#fields do ---~ local field = fields[i] ---~ scratch[i] = format("%s", field.label or field.name) ---~ end ---~ local result = format(template,concat(scratch),concat(lines,"\n")) ---~ return result, entries ---~ end - ---~ -- loggers.message("test.log","name","whatever","more",123) - ---~ local fields = { ---~ -- { name = "id", align = "left" }, ---~ -- { name = "timestamp", align = "left" }, ---~ { name = "assessment", align = "left" }, ---~ { name = "assessmentname", align = "left" }, ---~ -- { name = "category", align = "left" }, ---~ { name = "filesize", align = "right" }, ---~ { name = "nofimages", align = "center" }, ---~ -- { name = "product", align = "left" }, ---~ { name = "resultsize", align = "right" }, ---~ { name = "fetchtime", align = "right", format = "%2.3f" }, ---~ { name = "runtime", align = "right", format = "%2.3f" }, ---~ { name = "organization", align = "left" }, ---~ -- { name = "username", align = "left" }, ---~ } +function loggers.fields(results) -- returns hash of fields with counts so that we can decide on importance + local fields = { } + if results then + for i=1,#results do + local r = results[i] + for k, v in next, r do + local f = fields[k] + if not f then + fields[k] = 1 + else + fields[k] = f + 1 + end + end + end + end + return fields +end + +local template = [[ + +%s +%s +
+ +]] + +function loggers.tohtml(entries,fields) + if not fields or #fields == 0 then + return "" + end + if type(entries) == "string" then + entries = loggers.collect(entries) + end + local scratch, lines = { }, { } + for i=1,#entries do + local entry = entries[i] + local status = entry.status + for i=1,#fields do + local field = fields[i] + local v = status[field.name] + if v ~= nil then + v = tostring(v) + local f = field.format + if f then + v = format(f,v) + end + scratch[i] = format("%s",field.align or "left",v) + else + scratch[i] = "" + end + end + lines[i] = format("%s",concat(scratch)) + end + for i=1,#fields do + local field = fields[i] + scratch[i] = format("%s", field.label or field.name) + end + local result = format(template,concat(scratch),concat(lines,"\n")) + return result, entries +end + +-- loggers.store("test.log", { name = "whatever", more = math.random(1,100) }) + +-- local fields = { +-- { name = "name", align = "left" }, +-- { name = "more", align = "right" }, +-- } + +-- local entries = loggers.collect("test.log") +-- local html = loggers.tohtml(entries,fields) + +-- inspect(entries) +-- inspect(fields) +-- inspect(html) + -- cgit v1.2.3