A treat for Linux users ;)

Fri, 14 Nov 2008 16:03:33 +0000

author
Matthew Wild <mwild1@gmail.com>
date
Fri, 14 Nov 2008 16:03:33 +0000
changeset 262
8c73fb2ff4a2
parent 261
790cf21e2af7
child 263
75275b6b4e03

A treat for Linux users ;)

core/stanza_router.lua file | annotate | diff | comparison | revisions
util/logger.lua file | annotate | diff | comparison | revisions
util/stanza.lua file | annotate | diff | comparison | revisions
util/termcolours.lua file | annotate | diff | comparison | revisions
--- a/core/stanza_router.lua	Fri Nov 14 14:26:56 2008 +0000
+++ b/core/stanza_router.lua	Fri Nov 14 16:03:33 2008 +0000
@@ -32,7 +32,7 @@
 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)
--- a/util/logger.lua	Fri Nov 14 14:26:56 2008 +0000
+++ b/util/logger.lua	Fri Nov 14 16:03:33 2008 +0000
@@ -3,8 +3,21 @@
 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 @@
 					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
--- a/util/stanza.lua	Fri Nov 14 14:26:56 2008 +0000
+++ b/util/stanza.lua	Fri Nov 14 16:03:33 2008 +0000
@@ -10,8 +10,11 @@
 local print          =          print;
 local unpack        =        unpack;
 local s_gsub        =   string.gsub;
+local os = os;
 
-local debug = debug;
+local do_pretty_printing = not os.getenv("WINDIR");
+local getstyle, getstring = require "util.termcolours".getstyle, require "util.termcolours".getstring;
+
 local log = require "util.logger".init("stanza");
 
 module "stanza"
@@ -157,9 +160,6 @@
 			end
 			stanza.tags = tags;
 		end
-		if not stanza.last_add then
-			stanza.last_add = {};
-		end
 	end
 	
 	return stanza;
@@ -195,4 +195,44 @@
 	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, "</")..getstring(style_tagname, "%s")..getstring(style_punc, ">");
+	local tag_format = top_tag_format.."%s"..getstring(style_punc, "</")..getstring(style_tagname, "%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;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/util/termcolours.lua	Fri Nov 14 16:03:33 2008 +0000
@@ -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;

mercurial