scripting/debug_lua.cpp

Go to the documentation of this file.
00001 /* $Id: debug_lua.cpp 53543 2012-03-16 18:42:13Z shadowmaster $ */
00002 /*
00003    Copyright (C) 2009 - 2012 by Guillaume Melquiond <guillaume.melquiond@gmail.com>
00004    Part of the Battle for Wesnoth Project http://www.wesnoth.org/
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 2 of the License, or
00009    (at your option) any later version.
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY.
00012 
00013    See the COPYING file for more details.
00014 */
00015 
00016 #ifdef DEBUG_LUA
00017 
00018 #include <cassert>
00019 
00020 #include "debug_lua.hpp"
00021 
00022 #include "log.hpp"
00023 
00024 static lg::log_domain log_scripting_lua("scripting/lua");
00025 #define LOG_LUA LOG_STREAM(info, log_scripting_lua)
00026 
00027 
00028 static void value_to_stringstream(
00029     std::stringstream& output,
00030     int i, lua_State* L,
00031     std::string indent,
00032     const bool verbose_table = true)
00033 {
00034     const int t = lua_type(L, i);
00035     switch (t) {
00036         case LUA_TSTRING:
00037             output << "STRING; VALUE: " << lua_tostring(L, i);
00038         break;
00039         case LUA_TBOOLEAN:
00040             output << "BOOLEAN; VALUE: " << (lua_toboolean(L, i) ? "true" : "false");
00041         break;
00042         case LUA_TNUMBER:
00043             output << "NUMBER; VALUE: " << lua_tonumber(L, i);
00044         break;
00045         case LUA_TNIL:
00046             output << "NIL; VALUE: nil";
00047         break;
00048         case LUA_TTABLE:
00049         {
00050             output << "TABLE; VALUE: " << lua_topointer(L, i);
00051             if(verbose_table)
00052             {
00053                 indent += "\t";
00054                 unsigned keyindex = lua_gettop(L) + 1;
00055                 lua_pushnil(L);
00056                 while(lua_next(L, i) != 0)
00057                 {
00058                     output << "\n" << indent << "KEY: ";
00059                     const int keytype = lua_type(L, keyindex);
00060                     switch(keytype) {
00061                         case LUA_TSTRING:
00062                             output << lua_tostring(L, keyindex);
00063                         break;
00064                         case LUA_TBOOLEAN:
00065                             output << (lua_toboolean(L, keyindex) ? "true" : "false");
00066                         break;
00067                         case LUA_TNUMBER:
00068                             output << lua_tonumber(L, keyindex);
00069                         break;
00070                         default:
00071                             output << lua_topointer(L, keyindex);
00072                         break;
00073                     }
00074                     output << "; TYPE: ";
00075                     value_to_stringstream(output, keyindex + 1, L, indent);
00076                     lua_pop(L, 1);
00077                 }
00078             }
00079         }
00080         break;
00081         case LUA_TUSERDATA:
00082             output << "USERDATA; VALUE: " << lua_topointer(L, i);
00083         break;
00084         case LUA_TFUNCTION:
00085             output << "FUNCTION; VALUE: " << lua_topointer(L, i);
00086         break;
00087         case LUA_TTHREAD:
00088             output << "THREAD; VALUE: " << lua_topointer(L, i);
00089         break;
00090         case LUA_TLIGHTUSERDATA:
00091             output << "LIGHTUSERDATA; VALUE: " << lua_topointer(L, i);
00092         break;
00093         default:
00094             //There are no other types!
00095             assert(false);
00096         break;
00097     }
00098 }
00099 
00100 void ds(lua_State *L, const bool verbose_table) {
00101     std::stringstream output;
00102     output << "\n";
00103     int top = lua_gettop(L);
00104     for (int i = 1; i <= top; i++) {
00105         output << "INDEX: " << i << "; TYPE: ";
00106         value_to_stringstream(output, i, L, "", verbose_table);
00107         output << "\n";
00108     }
00109     output << "\n";
00110     LOG_LUA << output.str();
00111 }
00112 
00113 #endif
00114 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Thu May 24 2012 01:02:51 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs