Blame view

src/shared/debug.lua 9.3 KB
314bc5df   zhengshouren   提交服务器初始代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
  --[[
  
  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 "<var>"
          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 "<var>"
          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 "<var>"
          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