summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRichard Ipsum <richard.ipsum@codethink.co.uk>2014-01-08 16:33:49 +0000
committerRichard Ipsum <richard.ipsum@codethink.co.uk>2014-01-17 18:29:41 +0000
commit8b6542395750b9e4594ccb320c517c67a30396db (patch)
tree58f4a8b7037732598289dbdd6976c6ce5f9f8af9
parent499505b70acc410cbf41ce6bf37416738851f49f (diff)
downloadgitano-8b6542395750b9e4594ccb320c517c67a30396db.tar.gz
Add buffering capability to gitano.log
-rw-r--r--lib/gitano/log.lua53
1 files 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
}