--[[ Copyright (c) 2011-2012 qeeplay.com http://dualface.github.com/quick-cocos2d-x/ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ]] --[[-- Debug functions. ## Functions ## - echo - echoInfo - echoError - printf ]] local skynet = require "skynet" if ngx and ngx.say then echo_ = ngx.say elseif CCLuaLog then echo_ = CCLuaLog end if not echo_ then echo_ = print end if not echof_ then echof_ = skynet.error end io.output():setvbuf('no') function echo(...) local arr = {} for i, a in ipairs({...}) do arr[#arr + 1] = tostring(a) end echo_(table.concat(arr, "\t")) end function echof( ... ) local arr = {} for i, a in ipairs({...}) do arr[#arr + 1] = tostring(a) end echof_(table.concat(arr, "\t")) end if CCLuaLog then print = echo end --[[-- Output a formatted string. Depends on the platform, output to console or log file. @see echo(). @param string format @param mixed ... @see echo ]] function printf(fmt, ...) echo(string.format(tostring(fmt), ...)) end function echoError(fmt, ...) echo(string.format("[ERR] %s%s", string.format(tostring(fmt), ...), debug.traceback("", 2))) end function echoInfo(fmt, ...) echo("[INFO] " .. string.format(tostring(fmt), ...)) end function echoLog(tag, fmt, ...) echo(string.format("[%s] %s", string.upper(tostring(tag)), string.format(tostring(fmt), ...))) end function getPackageName(moduleName) local packageName = "" local pos = string.find(string.reverse(moduleName), "%.") if pos then packageName = string.sub(moduleName, 1, string.len(moduleName) - pos + 1) end return packageName end --[[-- Dumps information about a variable. @param mixed object @param string label @param bool isReturnContents @param int nesting @return nil|string ]] function dump(object, label, isReturnContents, nesting) if type(nesting) ~= "number" then nesting = 99 end local lookupTable = {} local result = {} local function _v(v) if type(v) == "string" then v = "\"" .. v .. "\"" end return tostring(v) end local traceback = string.split(debug.traceback("", 2), "\n") echo("dump from: " .. string.trim(traceback[3])) local function _dump(object, label, indent, nest, keylen) label = label or "" spc = "" if type(keylen) == "number" then spc = string.rep(" ", keylen - string.len(_v(label))) end if type(object) ~= "table" then result[#result +1 ] = string.format("%s%s%s = %s", indent, _v(label), spc, _v(object)) elseif lookupTable[object] then result[#result +1 ] = string.format("%s%s%s = *REF*", indent, label, spc) else lookupTable[object] = true if nest > nesting then result[#result +1 ] = string.format("%s%s = *MAX NESTING*", indent, label) else result[#result +1 ] = string.format("%s%s = {", indent, _v(label)) local indent2 = indent.." " local keys = {} local keylen = 0 local values = {} for k, v in pairs(object) do keys[#keys + 1] = k local vk = _v(k) local vkl = string.len(vk) if vkl > keylen then keylen = vkl end values[k] = v end table.sort(keys, function(a, b) if type(a) == "number" and type(b) == "number" then return a < b else return tostring(a) < tostring(b) end end) for i, k in ipairs(keys) do _dump(values[k], k, indent2, nest + 1, keylen) end result[#result +1] = string.format("%s}", indent) end end end _dump(object, label, "- ", 1) if isReturnContents then return table.concat(result, "\n") end for i, line in ipairs(result) do echo(line) end end --[[-- Dumps information about a variable into file. @param mixed object @param string label @param bool isReturnContents @param int nesting @return nil|string ]] function dump2file(object, label, isReturnContents, nesting) if type(nesting) ~= "number" then nesting = 99 end local lookupTable = {} local result = {} local function _v(v) if type(v) == "string" then v = "\"" .. v .. "\"" end return tostring(v) end local traceback = string.split(debug.traceback("", 2), "\n") echo("dump from: " .. string.trim(traceback[3])) local function _dump(object, label, indent, nest, keylen) label = label or "" spc = "" if type(keylen) == "number" then spc = string.rep(" ", keylen - string.len(_v(label))) end if type(object) ~= "table" then result[#result +1 ] = string.format("%s%s%s = %s", indent, _v(label), spc, _v(object)) elseif lookupTable[object] then result[#result +1 ] = string.format("%s%s%s = *REF*", indent, label, spc) else lookupTable[object] = true if nest > nesting then result[#result +1 ] = string.format("%s%s = *MAX NESTING*", indent, label) else result[#result +1 ] = string.format("%s%s = {", indent, _v(label)) local indent2 = indent.." " local keys = {} local keylen = 0 local values = {} for k, v in pairs(object) do keys[#keys + 1] = k local vk = _v(k) local vkl = string.len(vk) if vkl > keylen then keylen = vkl end values[k] = v end table.sort(keys, function(a, b) if type(a) == "number" and type(b) == "number" then return a < b else return tostring(a) < tostring(b) end end) for i, k in ipairs(keys) do _dump(values[k], k, indent2, nest + 1, keylen) end result[#result +1] = string.format("%s}", indent) end end end _dump(object, label, "- ", 1) if isReturnContents then return table.concat(result, "\n") end for i, line in ipairs(result) do echof(line) end end --[[-- Outputs or returns a parsable string representation of a variable. @param mixed object @param string label @return string ]] function vardump(object, label) local lookupTable = {} local result = {} local function _v(v) if type(v) == "string" then v = "\"" .. v .. "\"" end return tostring(v) end local function _vardump(object, label, indent, nest) label = label or "" local postfix = "" if nest > 1 then postfix = "," end if type(object) ~= "table" then if type(label) == "string" then result[#result +1] = string.format("%s%s = %s%s", indent, label, _v(object), postfix) else result[#result +1] = string.format("%s%s%s", indent, _v(object), postfix) end elseif not lookupTable[object] then lookupTable[object] = true if type(label) == "string" then result[#result +1 ] = string.format("%s%s = {", indent, label) else result[#result +1 ] = string.format("%s{", indent) end local indent2 = indent .. " " local keys = {} local values = {} for k, v in pairs(object) do keys[#keys + 1] = k values[k] = v end table.sort(keys, function(a, b) if type(a) == "number" and type(b) == "number" then return a < b else return tostring(a) < tostring(b) end end) for i, k in ipairs(keys) do _vardump(values[k], k, indent2, nest + 1) end result[#result +1] = string.format("%s}%s", indent, postfix) end end _vardump(object, label, "", 1) return table.concat(result, "\n") end