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-05-28 09:51:27 +02:00
|
|
|
local LONGEST_LEVEL_NAME = -1
|
|
|
|
|
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-05-28 09:51:27 +02:00
|
|
|
|
|
|
|
--[[== module initialization code ==]]--
|
|
|
|
|
|
|
|
-- M.LEVEL already has idx=>name entries, now create name=>idx entries so it can be indexed both ways, and init LONGEST_LEVEL_NAME
|
2013-06-30 19:06:55 +02:00
|
|
|
for i,v in ipairs(M.LEVEL) do
|
|
|
|
M.LEVEL[v] = i
|
2014-05-28 09:51:27 +02:00
|
|
|
if v:len() > LONGEST_LEVEL_NAME then LONGEST_LEVEL_NAME = v:len() end
|
2013-06-30 19:06:55 +02:00
|
|
|
end
|
|
|
|
|
2013-07-08 13:34:27 +02:00
|
|
|
|
2014-05-28 09:51:27 +02:00
|
|
|
--[[================================]]--
|
|
|
|
|
|
|
|
|
2014-04-25 14:51:29 +02:00
|
|
|
local function log(level, module, msg, verboseFmt)
|
2014-04-17 14:15:36 +02:00
|
|
|
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)
|
2014-04-25 14:51:29 +02:00
|
|
|
if module == nil then module = "LUA " end
|
2013-11-04 22:34:09 +01:00
|
|
|
|
2014-05-28 09:51:27 +02:00
|
|
|
local levelName = M.LEVEL[level]
|
|
|
|
local padding = string.rep(' ', LONGEST_LEVEL_NAME - levelName:len())
|
|
|
|
|
|
|
|
if v then logStream:write(now .. " [" .. module .. "] (" .. levelName .. ")" .. padding .. ": " .. m .. " [" .. name .. "@" .. i.short_src .. ":" .. i.linedefined .. "]\n")
|
|
|
|
else logStream:write(now .. " [" .. module .. "] (" .. levelName .. ")" .. padding .. ": " .. 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.
|
2014-04-25 14:51:29 +02:00
|
|
|
-- @tparam bool verbose Write verbose log messages (include file/line information).
|
2014-04-17 14:15:36 +02:00
|
|
|
function M:init(level, verboseFmt)
|
|
|
|
logLevel = level or M.LEVEL.warning
|
|
|
|
logVerboseFmt = verboseFmt or false
|
2014-04-25 14:51:29 +02:00
|
|
|
--logStream = stream or io.stdout
|
2013-07-28 05:01:58 +02:00
|
|
|
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
|
|
|
|
|
2014-04-25 14:51:29 +02:00
|
|
|
function M:getStream()
|
|
|
|
return logStream
|
|
|
|
end
|
|
|
|
|
|
|
|
function M:error(module, msg, verboseFmt) log(M.LEVEL.error, module, msg, verboseFmt); return false end
|
|
|
|
function M:warning(module, msg, verboseFmt) log(M.LEVEL.warning, module, msg, verboseFmt); return true end
|
|
|
|
function M:info(module, msg, verboseFmt) log(M.LEVEL.info, module, msg, verboseFmt); return true end
|
|
|
|
function M:verbose(module, msg, verboseFmt) log(M.LEVEL.verbose, module, msg, verboseFmt); return true end
|
|
|
|
function M:bulk(module, msg, verboseFmt) log(M.LEVEL.bulk, module, msg, verboseFmt); return true end
|
2013-06-30 19:06:55 +02:00
|
|
|
|
|
|
|
return M
|