Log.cpp 2.9 KB

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