summaryrefslogtreecommitdiff
path: root/lib/gitano/log.lua
diff options
context:
space:
mode:
Diffstat (limited to 'lib/gitano/log.lua')
-rw-r--r--lib/gitano/log.lua57
1 files changed, 53 insertions, 4 deletions
diff --git a/lib/gitano/log.lua b/lib/gitano/log.lua
index f243b87..e1df00b 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,15 +133,26 @@ 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
local function fatal(...)
- syslog_write(luxio.LOG_EMERG, ...)
+ syslog_write(luxio.LOG_CRIT, ...)
AT(ERRS, "FATAL:", ...)
- stream:close()
+
+ if is_buffered_output() then
+ sio.stderr:write(get_buffered_output())
+ else
+ stream:close()
+ end
+
luxio._exit(1)
end
@@ -244,5 +290,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
}