From 8b6542395750b9e4594ccb320c517c67a30396db Mon Sep 17 00:00:00 2001 From: Richard Ipsum Date: Wed, 8 Jan 2014 16:33:49 +0000 Subject: Add buffering capability to gitano.log --- lib/gitano/log.lua | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 50 insertions(+), 3 deletions(-) diff --git a/lib/gitano/log.lua b/lib/gitano/log.lua index f243b87..fca9e42 100644 --- a/lib/gitano/log.lua +++ b/lib/gitano/log.lua @@ -14,6 +14,7 @@ local prefix = "[gitano] " local transactionid = nil local stream = sio.stderr +local is_buffered = false local ERRS = 0 local WARN = 1 @@ -24,6 +25,40 @@ local DEEPDEBUG = 5 local level = ERRS +local LogBuf = {} +LogBuf.__index = LogBuf + +function LogBuf:new() + return setmetatable({strings = {}}, self) +end + +function LogBuf:write(s) + table.insert(self.strings, s) +end + +function LogBuf:get() + return table.concat(self.strings) +end + +local function is_buffered_output() + return is_buffered +end + +local function buffer_output() + if not is_buffered_output() then + stream = LogBuf:new() + is_buffered = true + end +end + +local function get_buffered_output() + if is_buffered_output() then + return stream:get() + else + return nil + end +end + local function syslog_write(priority, ...) local strs = {...} @@ -98,7 +133,12 @@ end local function stdout(...) local savedstream, savedprefix = stream, prefix - stream, prefix = sio.stdout, "" + + prefix = "" + if not is_buffered_output() then + stream = sio.stdout + end + state(...) stream, prefix = savedstream, savedprefix end @@ -106,7 +146,11 @@ end local function fatal(...) syslog_write(luxio.LOG_EMERG, ...) AT(ERRS, "FATAL:", ...) - stream:close() + + if not is_buffered_output() then + stream:close() + end + luxio._exit(1) end @@ -244,5 +288,8 @@ return { info = syslog_info, debug = syslog_debug, close = syslog_close, - } + }, + buffer_output = buffer_output, + is_buffered_output = is_buffered_output, + get_buffered_output = get_buffered_output } -- cgit v1.2.1