Log.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /*
  2. * This source file is part of RmlUi, the HTML/CSS Interface Middleware
  3. *
  4. * For the latest information, see http://github.com/mikke89/RmlUi
  5. *
  6. * Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
  7. * Copyright (c) 2019 The RmlUi Team, and contributors
  8. *
  9. * Permission is hereby granted, free of charge, to any person obtaining a copy
  10. * of this software and associated documentation files (the "Software"), to deal
  11. * in the Software without restriction, including without limitation the rights
  12. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. * copies of the Software, and to permit persons to whom the Software is
  14. * furnished to do so, subject to the following conditions:
  15. *
  16. * The above copyright notice and this permission notice shall be included in
  17. * all copies or substantial portions of the Software.
  18. *
  19. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. * THE SOFTWARE.
  26. *
  27. */
  28. #include "Log.h"
  29. #include <RmlUi/Core/Log.h>
  30. #include <RmlUi/Core/StringUtilities.h>
  31. namespace Rml {
  32. namespace Lua {
  33. template<> void ExtraInit<Log>(lua_State* L, int metatable_index)
  34. {
  35. //due to they way that LuaType::Register is made, we know that the method table is at the index
  36. //directly below the metatable
  37. int method_index = metatable_index - 1;
  38. lua_pushcfunction(L,LogMessage);
  39. lua_setfield(L,method_index, "Message");
  40. //construct the "logtype" table, so that we can use the Log::Type enum like Log.logtype.always in Lua for Log::LT_ALWAYS
  41. lua_newtable(L);
  42. int logtype = lua_gettop(L);
  43. lua_pushvalue(L,-1); //copy of the new table, so that the logtype index will stay valid
  44. lua_setfield(L,method_index,"logtype");
  45. lua_pushinteger(L,(int)Log::LT_ALWAYS);
  46. lua_setfield(L,logtype,"always");
  47. lua_pushinteger(L,(int)Log::LT_ERROR);
  48. lua_setfield(L,logtype,"error");
  49. lua_pushinteger(L,(int)Log::LT_WARNING);
  50. lua_setfield(L,logtype,"warning");
  51. lua_pushinteger(L,(int)Log::LT_INFO);
  52. lua_setfield(L,logtype,"info");
  53. lua_pushinteger(L,(int)Log::LT_DEBUG);
  54. lua_setfield(L,logtype,"debug");
  55. lua_pop(L,1); //pop the logtype table
  56. return;
  57. }
  58. int LogMessage(lua_State* L)
  59. {
  60. Log::Type type = Log::Type((int)luaL_checkinteger(L,1));
  61. const char* str = luaL_checkstring(L,2);
  62. Log::Message(type, "%s", str);
  63. return 0;
  64. }
  65. RegType<Log> LogMethods[] =
  66. {
  67. { nullptr, nullptr },
  68. };
  69. luaL_Reg LogGetters[] =
  70. {
  71. { nullptr, nullptr },
  72. };
  73. luaL_Reg LogSetters[] =
  74. {
  75. { nullptr, nullptr },
  76. };
  77. RMLUI_LUATYPE_DEFINE(Log)
  78. } // namespace Lua
  79. } // namespace Rml