From d67940a1d7700c0389341c42f845b91e8b32eea6 Mon Sep 17 00:00:00 2001 From: Matthew Wild Date: Fri, 14 Nov 2008 16:03:33 +0000 Subject: A treat for Linux users ;) --- core/stanza_router.lua | 2 +- util/logger.lua | 17 +++++++++++++++-- util/stanza.lua | 48 ++++++++++++++++++++++++++++++++++++++++++++---- util/termcolours.lua | 33 +++++++++++++++++++++++++++++++++ 4 files changed, 93 insertions(+), 7 deletions(-) create mode 100644 util/termcolours.lua diff --git a/core/stanza_router.lua b/core/stanza_router.lua index 6c117c25..16e6598f 100644 --- a/core/stanza_router.lua +++ b/core/stanza_router.lua @@ -32,7 +32,7 @@ local jid_split = require "util.jid".split; local print = print; function core_process_stanza(origin, stanza) - log("debug", "Received["..origin.type.."]: "..tostring(st.reply(st.reply(stanza)))) + log("debug", "Received[%s]: %s", origin.type, stanza:pretty_top_tag()) if not stanza.attr.xmlns then stanza.attr.xmlns = "jabber:client"; end -- FIXME Hack. This should be removed when we fix namespace handling. -- TODO verify validity of stanza (as well as JID validity) diff --git a/util/logger.lua b/util/logger.lua index fd5d3d80..f93cafc1 100644 --- a/util/logger.lua +++ b/util/logger.lua @@ -3,8 +3,21 @@ local format = string.format; local print = print; local debug = debug; local tostring = tostring; + +local getstyle, getstring = require "util.termcolours".getstyle, require "util.termcolours".getstring; +local do_pretty_printing = not os.getenv("WINDIR"); + module "logger" +local logstyles = {}; + +--TODO: This should be done in config, but we don't have proper config yet +if do_pretty_printing then + logstyles["info"] = getstyle("bold"); + logstyles["warn"] = getstyle("bold", "yellow"); + logstyles["error"] = getstyle("bold", "red"); +end + function init(name) --name = nil; -- While this line is not commented, will automatically fill in file/line number info return function (level, message, ...) @@ -13,9 +26,9 @@ function init(name) level = level .. ","..tostring(inf.short_src):match("[^/]*$")..":"..inf.currentline; end if ... then - print(name, level, format(message, ...)); + print(name, getstring(logstyles[level], level), format(message, ...)); else - print(name, level, message); + print(name, getstring(logstyles[level], level), message); end end end diff --git a/util/stanza.lua b/util/stanza.lua index cfa33c5b..5a6ba8c5 100644 --- a/util/stanza.lua +++ b/util/stanza.lua @@ -10,8 +10,11 @@ local next = next; local print = print; local unpack = unpack; local s_gsub = string.gsub; +local os = os; + +local do_pretty_printing = not os.getenv("WINDIR"); +local getstyle, getstring = require "util.termcolours".getstyle, require "util.termcolours".getstring; -local debug = debug; local log = require "util.logger".init("stanza"); module "stanza" @@ -157,9 +160,6 @@ function deserialize(stanza) end stanza.tags = tags; end - if not stanza.last_add then - stanza.last_add = {}; - end end return stanza; @@ -195,4 +195,44 @@ function presence(attr) return stanza("presence", attr); end +if do_pretty_printing then + local style_attrk = getstyle("yellow"); + local style_attrv = getstyle("red"); + local style_tagname = getstyle("red"); + local style_punc = getstyle("magenta"); + + local attr_format = " "..getstring(style_attrk, "%s")..getstring(style_punc, "=")..getstring(style_attrv, "'%s'"); + local top_tag_format = getstring(style_punc, "<")..getstring(style_tagname, "%s").."%s"..getstring(style_punc, ">"); + --local tag_format = getstring(style_punc, "<")..getstring(style_tagname, "%s").."%s"..getstring(style_punc, ">").."%s"..getstring(style_punc, ""); + local tag_format = top_tag_format.."%s"..getstring(style_punc, ""); + function stanza_mt.pretty_print(t) + local children_text = ""; + for n, child in ipairs(t) do + if type(child) == "string" then + children_text = children_text .. xml_escape(child); + else + children_text = children_text .. child:pretty_print(); + end + end + + local attr_string = ""; + if t.attr then + for k, v in pairs(t.attr) do if type(k) == "string" then attr_string = attr_string .. s_format(attr_format, k, tostring(v)); end end + end + return s_format(tag_format, t.name, attr_string, children_text, t.name); + end + + function stanza_mt.pretty_top_tag(t) + local attr_string = ""; + if t.attr then + for k, v in pairs(t.attr) do if type(k) == "string" then attr_string = attr_string .. s_format(attr_format, k, tostring(v)); end end + end + return s_format(top_tag_format, t.name, attr_string); + end +else + -- Sorry, fresh out of colours for you guys ;) + stanza_mt.pretty_print = stanza_mt.__tostring; + stanza_mt.pretty_top_tag = stanza_mt.top_tag; +end + return _M; diff --git a/util/termcolours.lua b/util/termcolours.lua new file mode 100644 index 00000000..5cf5b555 --- /dev/null +++ b/util/termcolours.lua @@ -0,0 +1,33 @@ +local t_concat, t_insert = table.concat, table.insert; +local char, format = string.char, string.format; +local ipairs = ipairs; +module "termcolours" + +local stylemap = { + reset = 0; bright = 1, dim = 2, underscore = 4, blink = 5, reverse = 7, hidden = 8; + black = 30; red = 31; green = 32; yellow = 33; blue = 34; magenta = 35; cyan = 36; white = 37; + ["black background"] = 40; ["red background"] = 41; ["green background"] = 42; ["yellow background"] = 43; ["blue background"] = 44; ["magenta background"] = 45; ["cyan background"] = 46; ["white background"] = 47; + bold = 1, dark = 2, underline = 4, underlined = 4, normal = 0; + } + +local fmt_string = char(0x1B).."[%sm%s"..char(0x1B).."[0m"; +function getstring(style, text) + if style then + return format(fmt_string, style, text); + else + return text; + end +end + +function getstyle(...) + local styles, result = { ... }, {}; + for i, style in ipairs(styles) do + style = stylemap[style]; + if style then + t_insert(result, style); + end + end + return t_concat(result, ";"); +end + +return _M; -- cgit v1.2.3