diff options
author | Karl Berry <karl@freefriends.org> | 2019-11-01 21:00:21 +0000 |
---|---|---|
committer | Karl Berry <karl@freefriends.org> | 2019-11-01 21:00:21 +0000 |
commit | fdb7d0585af6b9054e895d6f3047766f799b6098 (patch) | |
tree | 59e8b2c6971d1fb91d08250283dfa74589869d2d /Master/texmf-dist/scripts/make4ht/make4ht-logging.lua | |
parent | 1eea7ddd33803c5945bc4b2b2654c960d42aa0c2 (diff) |
make4ht (1nov19)
git-svn-id: svn://tug.org/texlive/trunk@52603 c570f23f-e606-0410-a88d-b1316a301751
Diffstat (limited to 'Master/texmf-dist/scripts/make4ht/make4ht-logging.lua')
-rwxr-xr-x | Master/texmf-dist/scripts/make4ht/make4ht-logging.lua | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/Master/texmf-dist/scripts/make4ht/make4ht-logging.lua b/Master/texmf-dist/scripts/make4ht/make4ht-logging.lua new file mode 100755 index 00000000000..c1d2b24a577 --- /dev/null +++ b/Master/texmf-dist/scripts/make4ht/make4ht-logging.lua @@ -0,0 +1,105 @@ +-- logging system for make4ht +-- inspired by https://github.com/rxi/log.lua +local logging = {} + +local levels = {} +-- level of bugs that should be shown +local show_level = 1 +local max_width = 0 + +logging.use_colors = true + +logging.modes = { + {name = "debug", color = 34}, + {name = "info", color = 32}, + {name = "status", color = 37}, + {name = "warning", color = 33}, + {name = "error", color = 31}, + {name = "fatal", color = 35} +} + +-- prepare table with mapping between mode names and corresponding levels + +function logging.prepare_levels(modes) + local modes = modes or logging.modes + logging.modes = modes + for level, mode in ipairs(modes) do + levels[mode.name] = level + mode.level = level + max_width = math.max(string.len(mode.name), max_width) + end +end + +-- the logging level is set once +function logging.set_level(name) + local level = levels[name] or 1 + show_level = level +end + +function logging.print_msg(header, message, color) + local color = color or 0 + -- use format for collors depending on the use_colors option + local header = "[" .. header .. "]" + local color_format = logging.use_colors and string.format("\27[%im%%s\27[0m%%s", color) or "%s%s" + -- the padding is maximal mode name width + brackets + space + local padded_header = string.format("%-".. max_width + 3 .. "s", header) + print(string.format(color_format, padded_header, message)) +end + +-- +function logging.new(module) + local obj = { + module = module, + output = function(self, output) + -- used for printing of output of commands + if show_level <= (levels[debug] or 1) then + print(output) + end + end + } + obj.__index = obj + -- make a function for each mode + for _, mode in ipairs(logging.modes) do + local name = mode.name + local color = mode.color + obj[name] = function(self, ...) + -- max width is saved in logging.prepare_levels + if mode.level >= show_level then + -- support variable number of parameters + local table_with_holes = table.pack(...) + local table_without_holes = {} + -- trick used to support the nil values in the varargs + -- https://stackoverflow.com/a/7186820/2467963 + for i= 1, table_with_holes.n do + table.insert(table_without_holes, tostring(table_with_holes[i]) or "") + end + local msg = table.concat(table_without_holes, "\t") + logging.print_msg(string.upper(name), string.format("%s: %s", self.module, msg), color) + end + end + end + return setmetatable({}, obj) + +end + + +-- prepare default levels +logging.prepare_levels() + +-- for _, mode in ipairs(logging.modes) do +-- logging.print_msg(mode.name,"xxxx", mode.color) +-- end + +-- local cls = logging.new("sample") +-- cls:warning("hello") +-- cls:error("world") +-- cls:info("set new level") +-- logging.set_level("error") +-- cls:info("level set") +-- cls:error("just print the error") + + +return logging + + + |