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