Dump Buffer Function

kaeus
Posts: 50
Joined: Thu Dec 31, 2009 4:33 pm

Dump Buffer Function

Post by kaeus »

So, I'm basically trying to make a logging function that just saves what I see in the buffer currently to a file with color formatting. Here is what I have so far:
Code: [show] | [select all] lua
function logScreen(logname)
if logname == nil then
	cecho("\n<red>Error:<white> You must sepecify a logname!")
	return false
end
local logdir = getMudletHomeDir() .. "\\log\\" .. logname .. ".html"
file_output = io.open(logdir, "w")
local line_count = getLineCount()
local cur_buffer = getLines(1, line_count-1)
local curcolor = ""
local index = 0
file_output:write("<body bgcolor='black'><span>")
for line_num,cur_line in pairs(cur_buffer) do
	index = 0
	while index < #cur_line do
		index = index + 1
		moveCursor("main", index, line_num)
		selectString(cur_line:sub(index), 1)
		r,g,b = getFgColor()
		if curcolor ~= (r .. "," .. g .. "," .. b) then
			curcolor = r .. "," .. g .. "," .. b
			file_output:write("</span><span style=\'color: rgb(" .. curcolor .. ");'>" .. cur_line:sub(index, index))
		else
			file_output:write(cur_line:sub(index, index))
		end
		cur_line:sub(index, index)
		deselect()
	end
	file_output:write("<br>\n")
end

file_output:write("</body>")

file_output:close()

cecho("\n<cyan>Current buffer saved to: <white>" .. logdir)
end
The main issue I run into at the moment is as the buffer gets longer, it takes a decent bit of time. I essentially am running line by line, and within each line going character by character to get the color code so i can output a fully color coded HTML file.

Does anyone have any advice on how to improve the function/speed it up a bit? I could limit the amount I save from the buffer (IE the last 1000 lines) but my logs that I want to save can be quite long and would still lock up Mudlet for a bit.

User avatar
Heiko
Site Admin
Posts: 1548
Joined: Wed Mar 11, 2009 6:26 pm

Re: Dump Buffer Function

Post by Heiko »

I'd try to log line by line. Make a trigger that triggers on every new line. Then you'll save the previous line in the buffer.

User avatar
Vadi
Posts: 5042
Joined: Sat Mar 14, 2009 3:13 pm

Re: Dump Buffer Function

Post by Vadi »

Logging is different from a dump... a dump you can do without having logging always on.

User avatar
Vadi
Posts: 5042
Joined: Sat Mar 14, 2009 3:13 pm

Re: Dump Buffer Function

Post by Vadi »

This should be faster. I also tried a version that does efficient string concatenation, but in the case of file.write it didn't make a difference.
Code: [show] | [select all] lua
function logScreen(logname)
if logname == nil then
        cecho("\n<red>Error:<white> You must sepecify a logname!")
        return false
end

if string.char(getMudletHomeDir():byte()) == "/" then _sep = "/" else  _sep = "\\" end
local logdir = getMudletHomeDir() .. _sep .. "log" .. _sep .. logname .. ".html"
local file_output = io.open(logdir, "w")
local fo = {}
local line_count = getLineCount()
local cur_buffer = getLines(1, line_count-1)
local curcolor = ""
local index = 0
local r,g,b = 0,0,0
local cr,cg,cb -- last colors
local perf = createStopWatch()
startStopWatch(perf)

file_output:write("<body bgcolor='black'><span>")
--fo[#fo+1] = "<body bgcolor='black'><span>"
for line_num,cur_line in ipairs(cur_buffer) do
        index = 0
        while index < #cur_line do
                index = index + 1
                moveCursor("main", index, line_num)
                selectString(cur_line:sub(index), 1)
                r,g,b = getFgColor()
                if cr ~= r or cg ~= g or cb ~= b then
						   cr,cg,cb = r,g,b
                        file_output:write(string.format("</span><span style=\'color: rgb(%d,%d,%d);'>%s", r,g,b, cur_line:sub(index, index)))
						   --fo[#fo+1] = string.format("</span><span style=\'color: rgb(%d,%d,%d);'>%s", r,g,b, cur_line:sub(index, index))
                else
                        file_output:write(cur_line:sub(index, index))
							--fo[#fo+1] = cur_line:sub(index, index)
                end
                cur_line:sub(index, index)
                --deselect() -- selection is overwritten anyway... deselect isn't for this case
        end
        file_output:write("<br>\n")
		 --fo[#fo+1] = "<br>\n"
end

file_output:write("</body>")
--fo[#fo+1] = "</body>"
--file_output:write(table.concat(fo))

file_output:close()

cecho("\n<cyan>Current buffer saved to: <white>" .. logdir .. "<cyan>, took <white>" .. stopStopWatch(perf) .. "ms<cyan>.")
end

User avatar
Vadi
Posts: 5042
Joined: Sat Mar 14, 2009 3:13 pm

Re: Dump Buffer Function

Post by Vadi »

Makes validator happier, although I can't solve the one error left.
Code: [show] | [select all] lua
function logScreen(logname)
if logname == nil then
        cecho("\n<red>Error:<white> You must sepecify a logname!")
        return false
end

if string.char(getMudletHomeDir():byte()) == "/" then _sep = "/" else  _sep = "\\" end
local logdir = getMudletHomeDir() .. _sep .. "log" .. _sep .. logname .. ".html"
local file_output = io.open(logdir, "w")
local fo = {}
local line_count = getLineCount()
local cur_buffer = getLines(1, line_count-1)
local curcolor = ""
local index = 0
local r,g,b = 0,0,0
local cr,cg,cb -- last colors
local perf = createStopWatch()
startStopWatch(perf)

file_output:write([[  <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
      <html>
		<head>
			<meta http-equiv="Content-Type" content="text/html;charset=utf-8" >
		</head>
		<body bgcolor='black'><span>]])
--fo[#fo+1] = "<body bgcolor='black'><span>"
for line_num,cur_line in ipairs(cur_buffer) do
        index = 0
        while index < #cur_line do
                index = index + 1
                moveCursor("main", index, line_num)
                selectString(cur_line:sub(index), 1)
                r,g,b = getFgColor()
                if cr ~= r or cg ~= g or cb ~= b then
						   cr,cg,cb = r,g,b
                        file_output:write(string.format("</span><span style=\'color: rgb(%d,%d,%d);'>%s", r,g,b, cur_line:sub(index, index)))
						   --fo[#fo+1] = string.format("</span><span style=\'color: rgb(%d,%d,%d);'>%s", r,g,b, cur_line:sub(index, index))
                else
                        file_output:write(cur_line:sub(index, index))
							--fo[#fo+1] = cur_line:sub(index, index)
                end
                cur_line:sub(index, index)
                --deselect() -- selection is overwritten anyway... deselect isn't for this case
        end
        file_output:write("<br>\n")
		 --fo[#fo+1] = "<br>\n"
end

file_output:write("</span></body></html>")
--fo[#fo+1] = "</body>"
--file_output:write(table.concat(fo))

file_output:close()

cecho("\n<cyan>Current buffer saved to: <white>" .. logdir .. "<cyan>, took <white>" .. stopStopWatch(perf) .. "ms<cyan>.")
end

User avatar
Vadi
Posts: 5042
Joined: Sat Mar 14, 2009 3:13 pm

Re: Dump Buffer Function

Post by Vadi »

If this is the final version, you should stick it into a package with an alias that invokes http://mudlet.org/asciidoc/manual.html#invokeFileDialog and release.

User avatar
Vadi
Posts: 5042
Joined: Sat Mar 14, 2009 3:13 pm

Re: Dump Buffer Function

Post by Vadi »

Now to fix it eating spaces... :P

kaeus
Posts: 50
Joined: Thu Dec 31, 2009 4:33 pm

Re: Dump Buffer Function

Post by kaeus »

Vadi wrote:Now to fix it eating spaces... :P
Doing that now with subbing space with &nbsp;

User avatar
Vadi
Posts: 5042
Joined: Sat Mar 14, 2009 3:13 pm

Re: Dump Buffer Function

Post by Vadi »

I wouldn't... just use the preformatted hint. I think <pre> or you can use it in css.

kaeus
Posts: 50
Joined: Thu Dec 31, 2009 4:33 pm

Re: Dump Buffer Function

Post by kaeus »

Vadi wrote:I wouldn't... just use the preformatted hint. I think <pre> or you can use it in css.
Seems like a better idea, I'll go ahead and do that.

Post Reply