The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
debug_lua.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2016 by Guillaume Melquiond <[email protected]>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #ifdef DEBUG_LUA
16 
17 #include <cassert>
18 
19 #include "debug_lua.hpp"
20 
21 #include "log.hpp"
22 
23 static lg::log_domain log_scripting_lua("scripting/lua");
24 #define LOG_LUA LOG_STREAM(info, log_scripting_lua)
25 
26 
27 static void value_to_stringstream(
28  std::stringstream& output,
29  int i, lua_State* L,
31  const bool verbose_table = true)
32 {
33  const int t = lua_type(L, i);
34  switch (t) {
35  case LUA_TSTRING:
36  output << "STRING; VALUE: " << lua_tostring(L, i);
37  break;
38  case LUA_TBOOLEAN:
39  output << "BOOLEAN; VALUE: " << (lua_toboolean(L, i) ? "true" : "false");
40  break;
41  case LUA_TNUMBER:
42  output << "NUMBER; VALUE: " << lua_tonumber(L, i);
43  break;
44  case LUA_TNIL:
45  output << "NIL; VALUE: nil";
46  break;
47  case LUA_TTABLE:
48  {
49  output << "TABLE; VALUE: " << lua_topointer(L, i);
50  if(verbose_table)
51  {
52  indent += "\t";
53  unsigned keyindex = lua_gettop(L) + 1;
54  lua_pushnil(L);
55  while(lua_next(L, i) != 0)
56  {
57  output << "\n" << indent << "KEY: ";
58  const int keytype = lua_type(L, keyindex);
59  switch(keytype) {
60  case LUA_TSTRING:
61  output << lua_tostring(L, keyindex);
62  break;
63  case LUA_TBOOLEAN:
64  output << (lua_toboolean(L, keyindex) ? "true" : "false");
65  break;
66  case LUA_TNUMBER:
67  output << lua_tonumber(L, keyindex);
68  break;
69  default:
70  output << lua_topointer(L, keyindex);
71  break;
72  }
73  output << "; TYPE: ";
74  value_to_stringstream(output, keyindex + 1, L, indent);
75  lua_pop(L, 1);
76  }
77  }
78  }
79  break;
80  case LUA_TUSERDATA:
81  output << "USERDATA; VALUE: " << lua_topointer(L, i);
82  break;
83  case LUA_TFUNCTION:
84  output << "FUNCTION; VALUE: " << lua_topointer(L, i);
85  break;
86  case LUA_TTHREAD:
87  output << "THREAD; VALUE: " << lua_topointer(L, i);
88  break;
89  case LUA_TLIGHTUSERDATA:
90  output << "LIGHTUSERDATA; VALUE: " << lua_topointer(L, i);
91  break;
92  default:
93  //There are no other types!
94  assert(false);
95  break;
96  }
97 }
98 
99 void ds(lua_State *L, const bool verbose_table) {
100  std::stringstream output;
101  output << "\n";
102  int top = lua_gettop(L);
103  for (int i = 1; i <= top; ++i) {
104  output << "INDEX: " << i << "; TYPE: ";
105  value_to_stringstream(output, i, L, "", verbose_table);
106  output << "\n";
107  }
108  output << "\n";
109  LOG_LUA << output.str();
110 }
111 
112 #endif
113 
#define LUA_TTHREAD
Definition: lua.h:85
LUA_API int lua_type(lua_State *L, int idx)
Definition: lapi.cpp:243
#define LUA_TUSERDATA
Definition: lua.h:84
LUA_API int lua_gettop(lua_State *L)
Definition: lapi.cpp:154
#define LUA_TLIGHTUSERDATA
Definition: lua.h:79
#define lua_tonumber(L, i)
Definition: lua.h:318
#define LUA_TFUNCTION
Definition: lua.h:83
#define LOG_LUA
GLdouble GLdouble t
Definition: glew.h:1366
#define lua_pop(L, n)
Definition: lua.h:322
#define LUA_TSTRING
Definition: lua.h:81
static lg::log_domain log_scripting_lua("scripting/lua")
#define LUA_TNIL
Definition: lua.h:77
LUA_API int lua_toboolean(lua_State *L, int idx)
Definition: lapi.cpp:377
#define LUA_TNUMBER
Definition: lua.h:80
static int indent
Definition: log.cpp:45
LUA_API void lua_pushnil(lua_State *L)
Definition: lapi.cpp:459
static const char * output
Definition: luac.cpp:31
size_t i
Definition: function.cpp:1057
#define lua_tostring(L, i)
Definition: lua.h:345
LUA_API const void * lua_topointer(lua_State *L, int idx)
Definition: lapi.cpp:437
#define LUA_TTABLE
Definition: lua.h:82
GLsizei const GLcharARB ** string
Definition: glew.h:4503
#define LUA_TBOOLEAN
Definition: lua.h:78
LUA_API int lua_next(lua_State *L, int idx)
Definition: lapi.cpp:1108