PageRenderTime 12ms CodeModel.GetById 6ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

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