The Battle for Wesnoth  1.15.2+dev
debug_lua.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 - 2018 by Guillaume Melquiond <guillaume.melquiond@gmail.com>
3  Part of the Battle for Wesnoth Project https://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 "scripting/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,
30  std::string indent,
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
#define LUA_TTHREAD
Definition: lua.h:72
LUA_API int lua_type(lua_State *L, int idx)
Definition: lapi.cpp:251
#define LUA_TUSERDATA
Definition: lua.h:71
LUA_API int lua_gettop(lua_State *L)
Definition: lapi.cpp:167
#define LUA_TLIGHTUSERDATA
Definition: lua.h:66
#define lua_tonumber(L, i)
Definition: lua.h:341
#define LUA_TFUNCTION
Definition: lua.h:70
#define LOG_LUA
#define lua_pop(L, n)
Definition: lua.h:344
#define LUA_TSTRING
Definition: lua.h:68
static lg::log_domain log_scripting_lua("scripting/lua")
static std::ostream & output()
Definition: log.cpp:51
#define LUA_TNIL
Definition: lua.h:64
LUA_API int lua_toboolean(lua_State *L, int idx)
Definition: lapi.cpp:367
#define LUA_TNUMBER
Definition: lua.h:67
static int indent
Definition: log.cpp:44
LUA_API void lua_pushnil(lua_State *L)
Definition: lapi.cpp:450
std::size_t i
Definition: function.cpp:933
#define lua_tostring(L, i)
Definition: lua.h:366
LUA_API const void * lua_topointer(lua_State *L, int idx)
Definition: lapi.cpp:429
double t
Definition: astarsearch.cpp:64
#define LUA_TTABLE
Definition: lua.h:69
#define LUA_TBOOLEAN
Definition: lua.h:65
LUA_API int lua_next(lua_State *L, int idx)
Definition: lapi.cpp:1123