/TGame/TCommon/Entry/MainLua.cpp

http://awoe.googlecode.com/ · C++ · 70 lines · 58 code · 11 blank · 1 comment · 4 complexity · 67e74d35a8799d4277230a6cae7d2e46 MD5 · raw file

  1. #include "stdafx.h"
  2. #include "MainLua.h"
  3. namespace MainLua
  4. {
  5. int log(lua_State* L, int lvl)
  6. {
  7. woe::tostringstream oss;
  8. int n = lua_gettop(L); /* number of arguments */
  9. int i;
  10. lua_getglobal(L, "tostring");
  11. for (i=1; i<=n; i++) {
  12. const char *s;
  13. lua_pushvalue(L, -1); /* function to be called */
  14. lua_pushvalue(L, i); /* value to print */
  15. lua_call(L, 1, 1);
  16. s = lua_tostring(L, -1); /* get result */
  17. if (s == NULL)
  18. return luaL_error(L, LUA_QL("tostring") " must return a string to "
  19. LUA_QL("print"));
  20. if (i>1)
  21. {
  22. oss<<"\t";
  23. }
  24. oss<<s;
  25. lua_pop(L, 1); /* pop result */
  26. }
  27. woe::Logger::instance()->log(lvl, woe::LLB_ALL, oss.str());
  28. return 0;
  29. }
  30. int log_debug(lua_State* L)
  31. {
  32. return log(L, woe::LL_DEBUG);
  33. }
  34. int log_info(lua_State* L)
  35. {
  36. return log(L, woe::LL_INFO);
  37. }
  38. int log_warn(lua_State* L)
  39. {
  40. return log(L, woe::LL_WARN);
  41. }
  42. int log_error(lua_State* L)
  43. {
  44. return log(L, woe::LL_ERROR);
  45. }
  46. static const luaL_Reg log_funcs[] = {
  47. {"debug", log_debug},
  48. {"info", log_info},
  49. {"warn", log_warn},
  50. {"error", log_error},
  51. {NULL, NULL}
  52. };
  53. int libaray(lua_State* L)
  54. {
  55. // fill member list into metatable
  56. luaL_register(L, "log", log_funcs);
  57. return 0;
  58. }
  59. } // end of scene lua