Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
[Lua] Added new 'dump()' debug function for lua
[simgrid.git] / src / bindings / lua / simgrid_lua.c
1 /* Copyright (c) 2010-2015. The SimGrid Team.
2  * All rights reserved.                                                     */
3
4 /* This program is free software; you can redistribute it and/or modify it
5  * under the terms of the license (GNU LGPL) which comes with this package. */
6
7 /* SimGrid Lua bindings                                                     */
8
9 #include "lua_private.h"
10 #include "lua_utils.h"
11 #include "xbt.h"
12 #include "simgrid/msg.h"
13 #include "simgrid/simdag.h"
14 #include "surf/surfxml_parse.h"
15 #include <lauxlib.h>
16
17
18 XBT_LOG_NEW_DEFAULT_SUBCATEGORY(lua, bindings, "Lua Bindings");
19
20 int luaopen_simgrid(lua_State *L);
21 static void sglua_register_c_functions(lua_State *L);
22
23 /* ********************************************************************************* */
24 /*                                  simgrid API                                      */
25 /* ********************************************************************************* */
26
27 /**
28  * \brief Prints a log string with debug level.
29  * \param L a Lua state
30  * \return number of values returned to Lua
31  *
32  * - Argument 1 (string): the text to print
33  */
34 static int debug(lua_State* L) {
35
36   const char* str = luaL_checkstring(L, 1);
37   XBT_DEBUG("%s", str);
38   return 0;
39 }
40
41 /**
42  * \brief Prints a log string with info level.
43  * \param L a Lua state
44  * \return number of values returned to Lua
45  *
46  * - Argument 1 (string): the text to print
47  */
48 static int info(lua_State* L) {
49
50   const char* str = luaL_checkstring(L, 1);
51   XBT_INFO("%s", str);
52   return 0;
53 }
54
55 static int error(lua_State* L) {
56
57   const char* str = luaL_checkstring(L, 1);
58   XBT_ERROR("%s", str);
59   return 0;
60 }
61
62 static int critical(lua_State* L) {
63
64   const char* str = luaL_checkstring(L, 1);
65   XBT_CRITICAL("%s", str);
66   return 0;
67 }
68
69 /**
70  * @brief Dumps a lua table with XBT_DEBUG
71  *
72  * This function can be called from within lua via "simgrid.dump(table)". It will
73  * then dump the table via XBT_DEBUG 
74  */
75 static int dump(lua_State* L) {
76   int argc = lua_gettop(L);
77
78   for (int i = 1; i <= argc; i++) {
79     if (lua_istable(L, i)) {
80       lua_pushnil(L); /* table nil */
81
82       //lua_next pops the topmost element from the stack and 
83       //gets the next pair from the table at the specified index
84       while (lua_next(L, i)) { /* table key val  */
85         // we need to copy here, as a cast from "Number" to "String"
86         // could happen in Lua.
87         // see remark in the lua manual, function "lua_tolstring"
88         // http://www.lua.org/manual/5.3/manual.html#lua_tolstring
89
90         lua_pushvalue(L, -2); /* table key val key */
91
92         const char *key = lua_tostring(L, -1); /* table key val key */
93         const char *val = lua_tostring(L, -2); /* table key val key */
94
95         XBT_DEBUG("%s", sglua_keyvalue_tostring(L, -1, -2));
96       }
97
98       lua_settop(L, argc); // Remove everything except the initial arguments
99     }
100   }
101
102   return 0;
103 }
104
105 static const luaL_Reg simgrid_functions[] = {
106   {"dump", dump},
107   {"debug", debug},
108   {"info", info},
109   {"critical", critical},
110   {"error", error},
111   /* short names */
112   /* methods to bypass XML parser */
113   {NULL, NULL}
114 };
115
116 /* ********************************************************************************* */
117 /*                           module management functions                             */
118 /* ********************************************************************************* */
119
120 #define LUA_MAX_ARGS_COUNT 10   /* maximum amount of arguments we can get from lua on command line */
121
122 /**
123  * \brief Opens the simgrid Lua module.
124  *
125  * This function is called automatically by the Lua interpreter when some
126  * Lua code requires the "simgrid" module.
127  *
128  * \param L the Lua state
129  */
130
131 int luaopen_simgrid(lua_State *L)
132 {
133   XBT_DEBUG("luaopen_simgrid *****");
134
135   sglua_register_c_functions(L);
136
137   return 1;
138 }
139
140
141 /**
142  * \brief Makes the core functions available to the Lua world.
143  * \param L a Lua world
144  */
145 static void sglua_register_core_functions(lua_State *L)
146 {
147   /* register the core C functions to lua */
148   luaL_newlib(L, simgrid_functions); /* simgrid */
149   lua_pushvalue(L, -1);              /* simgrid simgrid */
150   lua_setglobal(L, "simgrid");       /* simgrid */
151 }
152
153 /**
154  * \brief Creates the simgrid module and make it available to Lua.
155  * \param L a Lua world
156  */
157 static void sglua_register_c_functions(lua_State *L)
158 {
159   sglua_register_core_functions(L);
160   sglua_register_host_functions(L);
161   sglua_register_platf_functions(L);
162 }