Log.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #include "Log.h"
  2. #include <RmlUi/Core/Log.h>
  3. #include <RmlUi/Core/StringUtilities.h>
  4. namespace Rml {
  5. namespace Lua {
  6. template <>
  7. void ExtraInit<Log>(lua_State* L, int metatable_index)
  8. {
  9. // due to they way that LuaType::Register is made, we know that the method table is at the index
  10. // directly below the metatable
  11. int method_index = metatable_index - 1;
  12. lua_pushcfunction(L, LogMessage);
  13. lua_setfield(L, method_index, "Message");
  14. // construct the "logtype" table, so that we can use the Log::Type enum like Log.logtype.always in Lua for Log::LT_ALWAYS
  15. lua_newtable(L);
  16. int logtype = lua_gettop(L);
  17. lua_pushvalue(L, -1); // copy of the new table, so that the logtype index will stay valid
  18. lua_setfield(L, method_index, "logtype");
  19. lua_pushinteger(L, (int)Log::LT_ALWAYS);
  20. lua_setfield(L, logtype, "always");
  21. lua_pushinteger(L, (int)Log::LT_ERROR);
  22. lua_setfield(L, logtype, "error");
  23. lua_pushinteger(L, (int)Log::LT_WARNING);
  24. lua_setfield(L, logtype, "warning");
  25. lua_pushinteger(L, (int)Log::LT_INFO);
  26. lua_setfield(L, logtype, "info");
  27. lua_pushinteger(L, (int)Log::LT_DEBUG);
  28. lua_setfield(L, logtype, "debug");
  29. lua_pop(L, 1); // pop the logtype table
  30. return;
  31. }
  32. int LogMessage(lua_State* L)
  33. {
  34. Log::Type type = Log::Type((int)luaL_checkinteger(L, 1));
  35. const char* str = luaL_checkstring(L, 2);
  36. Log::Message(type, "%s", str);
  37. return 0;
  38. }
  39. RegType<Log> LogMethods[] = {
  40. {nullptr, nullptr},
  41. };
  42. luaL_Reg LogGetters[] = {
  43. {nullptr, nullptr},
  44. };
  45. luaL_Reg LogSetters[] = {
  46. {nullptr, nullptr},
  47. };
  48. RMLUI_LUATYPE_DEFINE(Log)
  49. } // namespace Lua
  50. } // namespace Rml