2013-12-20 16:29:46 +01:00
|
|
|
--
|
|
|
|
-- This file is part of the Doodle3D project (http://doodle3d.com).
|
|
|
|
--
|
|
|
|
-- @copyright 2013, Doodle3D
|
|
|
|
-- @license This software is licensed under the terms of the GNU GPL v2 or later.
|
|
|
|
-- See file LICENSE.txt or visit http://www.gnu.org/licenses/gpl.html for full license details.
|
|
|
|
|
|
|
|
|
2013-11-04 22:34:09 +01:00
|
|
|
---
|
|
|
|
-- Logging facilities.
|
2013-07-28 05:01:58 +02:00
|
|
|
|
2013-07-17 08:25:24 +02:00
|
|
|
local utils = require('util.utils')
|
|
|
|
|
2013-06-30 19:06:55 +02:00
|
|
|
local M = {}
|
|
|
|
|
2014-04-17 14:15:36 +02:00
|
|
|
local logLevel, logVerboseFmt, logStream
|
2013-06-30 19:06:55 +02:00
|
|
|
|
2014-04-17 14:15:36 +02:00
|
|
|
--- Available log levels (starting at 1)
|
2013-11-04 22:34:09 +01:00
|
|
|
-- @table LEVEL
|
2013-07-28 05:01:58 +02:00
|
|
|
M.LEVEL = {
|
2014-04-17 14:15:36 +02:00
|
|
|
'quiet', -- not used for logging, only for setting levels
|
|
|
|
'error', -- fatal or non-fatal error condition
|
|
|
|
'warning', -- possibly troublesome situation
|
|
|
|
'info', -- information about what the application is doing
|
|
|
|
'verbose', -- extra trail information on what the application is doing
|
|
|
|
'bulk' -- debug information (in large amounts)
|
2013-07-28 05:01:58 +02:00
|
|
|
}
|
2013-06-30 19:06:55 +02:00
|
|
|
|
2014-04-17 14:15:36 +02:00
|
|
|
-- M.LEVEL already has idx=>name entries, now create name=>idx entries so it can be indexed both ways
|
2013-06-30 19:06:55 +02:00
|
|
|
for i,v in ipairs(M.LEVEL) do
|
|
|
|
M.LEVEL[v] = i
|
|
|
|
end
|
|
|
|
|
2013-07-08 13:34:27 +02:00
|
|
|
|
2014-04-17 14:15:36 +02:00
|
|
|
local function log(level, msg, verboseFmt)
|
|
|
|
if level <= logLevel then
|
2013-07-17 08:25:24 +02:00
|
|
|
local now = os.date('%m-%d %H:%M:%S')
|
2013-06-30 19:06:55 +02:00
|
|
|
local i = debug.getinfo(3) --the stack frame just above the logger call
|
2014-04-17 14:15:36 +02:00
|
|
|
local v = verboseFmt
|
|
|
|
if v == nil then v = logVerboseFmt end
|
2013-06-30 19:06:55 +02:00
|
|
|
local name = i.name or "(nil)"
|
2013-07-17 08:25:24 +02:00
|
|
|
local vVal = 'nil'
|
|
|
|
local m = (type(msg) == 'string') and msg or utils.dump(msg)
|
2013-11-04 22:34:09 +01:00
|
|
|
|
2013-07-08 13:34:27 +02:00
|
|
|
if v then logStream:write(now .. " (" .. M.LEVEL[level] .. ") " .. m .. " [" .. name .. "@" .. i.short_src .. ":" .. i.linedefined .. "]\n")
|
|
|
|
else logStream:write(now .. " (" .. M.LEVEL[level] .. ") " .. m .. "\n") end
|
2013-11-04 22:34:09 +01:00
|
|
|
|
2013-08-23 10:43:42 +02:00
|
|
|
logStream:flush()
|
2013-06-30 19:06:55 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-28 05:01:58 +02:00
|
|
|
|
|
|
|
--- Initializes the logger.
|
|
|
|
-- @tparam @{util.logger.LEVEL} level Minimum level of messages to log.
|
|
|
|
-- @tparam bool verbose Write verbose log messages (include file/line inforomation).
|
2014-04-17 14:15:36 +02:00
|
|
|
function M:init(level, verboseFmt)
|
|
|
|
logLevel = level or M.LEVEL.warning
|
|
|
|
logVerboseFmt = verboseFmt or false
|
2013-07-28 05:01:58 +02:00
|
|
|
logStream = stream or io.stdout
|
|
|
|
end
|
|
|
|
|
2014-04-17 14:15:36 +02:00
|
|
|
function M:setLevel(level, verboseFmt)
|
|
|
|
logLevel = level or M.LEVEL.warning
|
|
|
|
logVerboseFmt = verboseFmt or false
|
|
|
|
end
|
|
|
|
|
2013-07-28 05:01:58 +02:00
|
|
|
-- pass nil as stream to reset to stdout
|
|
|
|
function M:setStream(stream)
|
|
|
|
logStream = stream or io.stdout
|
|
|
|
end
|
|
|
|
|
2014-04-17 14:15:36 +02:00
|
|
|
function M:getLevel()
|
|
|
|
return logLevel, logVerboseFmt
|
|
|
|
end
|
|
|
|
|
|
|
|
function M:error(msg, verboseFmt) log(M.LEVEL.error, msg, verboseFmt); return false end
|
|
|
|
function M:warning(msg, verboseFmt) log(M.LEVEL.warning, msg, verboseFmt); return true end
|
|
|
|
function M:info(msg, verboseFmt) log(M.LEVEL.info, msg, verboseFmt); return true end
|
|
|
|
function M:verbose(msg, verboseFmt) log(M.LEVEL.verbose, msg, verboseFmt); return true end
|
|
|
|
function M:bulk(msg, verboseFmt) log(M.LEVEL.bulk, msg, verboseFmt); return true end
|
2013-06-30 19:06:55 +02:00
|
|
|
|
|
|
|
return M
|