Log.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * This source file is part of libRocket, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://www.librocket.com
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. * THE SOFTWARE.
  25. *
  26. */
  27. #include "precompiled.h"
  28. #include "Log.h"
  29. #include <Rocket/Core/Log.h>
  30. #include <Rocket/Core/String.h>
  31. #include <Rocket/Core/StringUtilities.h>
  32. namespace Rocket {
  33. namespace Core {
  34. namespace Lua {
  35. template<> void ExtraInit<Log>(lua_State* L, int metatable_index)
  36. {
  37. //due to they way that LuaType::Register is made, we know that the method table is at the index
  38. //directly below the metatable
  39. int method_index = metatable_index - 1;
  40. lua_pushcfunction(L,LogMessage);
  41. lua_setfield(L,method_index, "Message");
  42. //construct the "logtype" table, so that we can use the Rocket::Core::Log::Type enum like Log.logtype.always in Lua for Log::LT_ALWAYS
  43. lua_newtable(L);
  44. int logtype = lua_gettop(L);
  45. lua_pushvalue(L,-1); //copy of the new table, so that the logtype index will stay valid
  46. lua_setfield(L,method_index,"logtype");
  47. lua_pushinteger(L,(int)Log::LT_ALWAYS);
  48. lua_setfield(L,logtype,"always");
  49. lua_pushinteger(L,(int)Log::LT_ERROR);
  50. lua_setfield(L,logtype,"error");
  51. lua_pushinteger(L,(int)Log::LT_WARNING);
  52. lua_setfield(L,logtype,"warning");
  53. lua_pushinteger(L,(int)Log::LT_INFO);
  54. lua_setfield(L,logtype,"info");
  55. lua_pushinteger(L,(int)Log::LT_DEBUG);
  56. lua_setfield(L,logtype,"debug");
  57. lua_pop(L,1); //pop the logtype table
  58. //overwrite the default 'print' function
  59. lua_register(L,"print",OverwriteLuaPrint);
  60. return;
  61. }
  62. int LogMessage(lua_State* L)
  63. {
  64. Log::Type type = Log::Type((int)luaL_checkinteger(L,1));
  65. const char* str = luaL_checkstring(L,2);
  66. Log::Message(type, str);
  67. return 0;
  68. }
  69. //Based off of the luaB_print function from lbaselib.c
  70. int OverwriteLuaPrint(lua_State* L)
  71. {
  72. int n = lua_gettop(L); /* number of arguments */
  73. int i;
  74. lua_getglobal(L, "tostring");
  75. StringList string_list = StringList();
  76. String output = "";
  77. for (i=1; i<=n; i++)
  78. {
  79. const char *s;
  80. lua_pushvalue(L, -1); /* function to be called */
  81. lua_pushvalue(L, i); /* value to print */
  82. lua_call(L, 1, 1);
  83. s = lua_tostring(L, -1); /* get result */
  84. if (s == NULL)
  85. return luaL_error(L, LUA_QL("tostring") " must return a string to "
  86. LUA_QL("print"));
  87. if (i>1)
  88. output += "\t";
  89. output += String(s);
  90. lua_pop(L, 1); /* pop result */
  91. }
  92. output += "\n";
  93. Log::Message(Log::LT_INFO, output.CString());
  94. return 0;
  95. }
  96. RegType<Log> LogMethods[] =
  97. {
  98. { NULL, NULL },
  99. };
  100. luaL_reg LogGetters[] =
  101. {
  102. { NULL, NULL },
  103. };
  104. luaL_reg LogSetters[] =
  105. {
  106. { NULL, NULL },
  107. };
  108. LUATYPEDEFINE(Log,false)
  109. }
  110. }
  111. }