Permalink
Browse files

fix a log bug

  • Loading branch information...
1 parent 2fa1c40 commit a73714d2fbac69a6d15660013bf854e84242a7fa zrong committed Feb 7, 2015
Showing with 65 additions and 6 deletions.
  1. +17 −6 lib/zrong/zr/log/LogHandler.lua
  2. +48 −0 test/testlog.lua
@@ -4,6 +4,10 @@
local LogHandler = class('LogHandler')
+local function _isfmt(fmt)
+ return type(fmt) == 'string' and string.find(fmt, "%%")
+end
+
-- @starttime
-- @gettime A function, return current time.
function LogHandler:ctor(starttime, gettime)
@@ -26,13 +30,20 @@ function LogHandler:getString(level, fmt, args)
if self._starttime then
strlist[#strlist+1] = string.format('[%.4f]', self._gettime()-self._starttime)
end
- if #args > 0 and
- type(fmt) == 'string' and
- string.find(fmt, "%%") then
- strlist[#strlist+1] = string.format(fmt, unpack(args))
- else
+ local argsnum = #args
+ if argsnum > 0 and _isfmt(fmt) then
+ local fmtnum = select(2, string.gsub(fmt, '%%%a', ''))
+ if fmtnum ~= argsnum then
+ strlist[#strlist+1] = 'LogHandler ERROR -- Cannot get a nil value between arguments OR you give a bad amout of arguments.'
+ else
+ strlist[#strlist+1] = string.format(fmt, unpack(args))
+ end
+ elseif _isfmt(fmt) then
+ fmt = string.gsub(fmt, '%%%a', 'nil')
strlist[#strlist+1] = fmt
- for i=1, #args do
+ else
+ strlist[#strlist+1] = tostring(fmt)
+ for i=1, argsnum do
strlist[#strlist+1] = tostring(args[i])
end
end
View
@@ -0,0 +1,48 @@
+-- Please set LUA_PATH to '../lib' in absolute path.
+print(package.path)
+
+require('zrong.init')
+local Logger = zr.log.Logger
+local PrintHandler = zr.log.PrintHandler
+local log = zr.log.Logger.new(Logger.NOTSET, PrintHandler.new(print))
+
+function d(fmt, ...)
+ log:debug(fmt, ...)
+end
+
+local t = {1, 2, 3, 4}
+local n = nil
+
+local args = {nil,nil,nil}
+print(#args)
+for k,v in ipairs(args) do
+ print(k,v)
+end
+
+-- print(string.format('abc %s', unpack({nil})))
+
+d('abc %s %d -- %f', nil, 3)
+d(t)
+
+do return end
+
+p = 'abc %s %d -- %f'
+print(string.gsub(p, '%%%a', ''))
+
+local function getArgs(...)
+ print(...)
+ local args = {...}
+ print(#args)
+ dump(args)
+ for k,v in ipairs(args) do
+ print('-', k,v)
+ end
+ print(unpack(args))
+end
+
+getArgs('a', 1, false, 3)
+getArgs('a', 1, nil, 3)
+
+for k,v in ipairs{'a', 1, nil, 3} do
+ print('-', k,v)
+end

0 comments on commit a73714d

Please sign in to comment.