You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

188 lines
5.8 KiB

  1. --[[
  2. Copyright (c) 2011-2012 qeeplay.com
  3. http://dualface.github.com/quick-cocos2d-x/
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ]]
  20. if CCLuaLog then
  21. io.output():setvbuf('no')
  22. elseif ngx and ngx.log then
  23. print = function(...)
  24. local arg = {...}
  25. for k,v in pairs(arg) do
  26. arg[k] = tostring(v)
  27. end
  28. ngx.log(ngx.ERR, table.concat(arg, "\t"))
  29. end
  30. end
  31. echo = print
  32. function printf(fmt, ...)
  33. echo(string.format(tostring(fmt), ...))
  34. end
  35. function echoError(fmt, ...)
  36. echoLog("ERR", fmt, ...)
  37. print(debug.traceback("", 2))
  38. end
  39. function echoInfo(fmt, ...)
  40. echoLog("INFO", fmt, ...)
  41. end
  42. function echoLog(tag, fmt, ...)
  43. echo(string.format("[%s] %s", string.upper(tostring(tag)), string.format(tostring(fmt), ...)))
  44. end
  45. function throw(errorType, fmt, ...)
  46. local arg = {...}
  47. for k,v in pairs(arg) do
  48. arg[k] = tostring(v)
  49. end
  50. local msg = string.format(tostring(fmt), unpack(arg))
  51. error(string.format("<<%s>> - %s", tostring(errorType), msg), 2)
  52. end
  53. function dump(object, label, isReturnContents, nesting)
  54. if type(nesting) ~= "number" then nesting = 99 end
  55. local lookupTable = {}
  56. local result = {}
  57. local function _v(v)
  58. if type(v) == "string" then
  59. v = "\"" .. v .. "\""
  60. end
  61. return tostring(v)
  62. end
  63. local traceback = string.split(debug.traceback("", 2), "\n")
  64. echo("dump from: " .. string.trim(traceback[3]))
  65. local function _dump(object, label, indent, nest, keylen)
  66. label = label or "<var>"
  67. spc = ""
  68. if type(keylen) == "number" then
  69. spc = string.rep(" ", keylen - string.len(_v(label)))
  70. end
  71. if type(object) ~= "table" then
  72. result[#result +1 ] = string.format("%s%s%s = %s", indent, _v(label), spc, _v(object))
  73. elseif lookupTable[object] then
  74. result[#result +1 ] = string.format("%s%s%s = *REF*", indent, label, spc)
  75. else
  76. lookupTable[object] = true
  77. if nest > nesting then
  78. result[#result +1 ] = string.format("%s%s = *MAX NESTING*", indent, label)
  79. else
  80. result[#result +1 ] = string.format("%s%s = {", indent, _v(label))
  81. local indent2 = indent.." "
  82. local keys = {}
  83. local keylen = 0
  84. local values = {}
  85. for k, v in pairs(object) do
  86. keys[#keys + 1] = k
  87. local vk = _v(k)
  88. local vkl = string.len(vk)
  89. if vkl > keylen then keylen = vkl end
  90. values[k] = v
  91. end
  92. table.sort(keys, function(a, b)
  93. if type(a) == "number" and type(b) == "number" then
  94. return a < b
  95. else
  96. return tostring(a) < tostring(b)
  97. end
  98. end)
  99. for i, k in ipairs(keys) do
  100. _dump(values[k], k, indent2, nest + 1, keylen)
  101. end
  102. result[#result +1] = string.format("%s}", indent)
  103. end
  104. end
  105. end
  106. _dump(object, label, "- ", 1)
  107. if isReturnContents then
  108. return table.concat(result, "\n")
  109. end
  110. for i, line in ipairs(result) do
  111. echo(line)
  112. end
  113. end
  114. function vardump(object, label)
  115. local lookupTable = {}
  116. local result = {}
  117. local function _v(v)
  118. if type(v) == "string" then
  119. v = v:toLuaString()
  120. end
  121. return tostring(v)
  122. end
  123. local function _key(k)
  124. if type(k) == "string" then
  125. return '["' .. k .. '"]';
  126. else
  127. return '[' .. k .. ']';
  128. end
  129. end
  130. local function _vardump(object, label, indent, nest)
  131. label = label or "<var>"
  132. local postfix = ""
  133. if nest > 1 then postfix = "," end
  134. if type(object) ~= "table" then
  135. result[#result +1] = string.format("%s%s = %s%s", indent, _key(label), _v(object), postfix)
  136. elseif not lookupTable[object] then
  137. lookupTable[object] = true
  138. result[#result +1 ] = string.format("%s%s = {", indent, _key(label))
  139. local indent2 = indent .. " "
  140. local keys = {}
  141. local values = {}
  142. for k, v in pairs(object) do
  143. keys[#keys + 1] = k
  144. values[k] = v
  145. end
  146. table.sort(keys, function(a, b)
  147. if type(a) == "number" and type(b) == "number" then
  148. return a < b
  149. else
  150. return tostring(a) < tostring(b)
  151. end
  152. end)
  153. for i, k in ipairs(keys) do
  154. _vardump(values[k], k, indent2, nest + 1)
  155. end
  156. result[#result +1] = string.format("%s}%s", indent, postfix)
  157. end
  158. end
  159. _vardump(object, label, "", 1)
  160. return table.concat(result, "\n")
  161. end