Log.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. return;
  59. }
  60. int LogMessage(lua_State* L)
  61. {
  62. Log::Type type = Log::Type((int)luaL_checkinteger(L,1));
  63. const char* str = luaL_checkstring(L,2);
  64. Log::Message(type, str);
  65. return 0;
  66. }
  67. RegType<Log> LogMethods[] =
  68. {
  69. { NULL, NULL },
  70. };
  71. luaL_reg LogGetters[] =
  72. {
  73. { NULL, NULL },
  74. };
  75. luaL_reg LogSetters[] =
  76. {
  77. { NULL, NULL },
  78. };
  79. LUATYPEDEFINE(Log,false)
  80. }
  81. }
  82. }