Log.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 "precompiled.h"
  29. #include "Log.h"
  30. #include <RmlUi/Core/Log.h>
  31. #include <RmlUi/Core/String.h>
  32. #include <RmlUi/Core/StringUtilities.h>
  33. namespace Rml {
  34. namespace Core {
  35. namespace Lua {
  36. template<> void ExtraInit<Log>(lua_State* L, int metatable_index)
  37. {
  38. //due to they way that LuaType::Register is made, we know that the method table is at the index
  39. //directly below the metatable
  40. int method_index = metatable_index - 1;
  41. lua_pushcfunction(L,LogMessage);
  42. lua_setfield(L,method_index, "Message");
  43. //construct the "logtype" table, so that we can use the Rml::Core::Log::Type enum like Log.logtype.always in Lua for Log::LT_ALWAYS
  44. lua_newtable(L);
  45. int logtype = lua_gettop(L);
  46. lua_pushvalue(L,-1); //copy of the new table, so that the logtype index will stay valid
  47. lua_setfield(L,method_index,"logtype");
  48. lua_pushinteger(L,(int)Log::LT_ALWAYS);
  49. lua_setfield(L,logtype,"always");
  50. lua_pushinteger(L,(int)Log::LT_ERROR);
  51. lua_setfield(L,logtype,"error");
  52. lua_pushinteger(L,(int)Log::LT_WARNING);
  53. lua_setfield(L,logtype,"warning");
  54. lua_pushinteger(L,(int)Log::LT_INFO);
  55. lua_setfield(L,logtype,"info");
  56. lua_pushinteger(L,(int)Log::LT_DEBUG);
  57. lua_setfield(L,logtype,"debug");
  58. lua_pop(L,1); //pop the logtype table
  59. return;
  60. }
  61. int LogMessage(lua_State* L)
  62. {
  63. Log::Type type = Log::Type((int)luaL_checkinteger(L,1));
  64. const char* str = luaL_checkstring(L,2);
  65. Log::Message(type, str);
  66. return 0;
  67. }
  68. RegType<Log> LogMethods[] =
  69. {
  70. { nullptr, nullptr },
  71. };
  72. luaL_Reg LogGetters[] =
  73. {
  74. { nullptr, nullptr },
  75. };
  76. luaL_Reg LogSetters[] =
  77. {
  78. { nullptr, nullptr },
  79. };
  80. LUACORETYPEDEFINE(Log)
  81. }
  82. }
  83. }