Commit 9547e604 authored by Vaastav Anand's avatar Vaastav Anand
Browse files

Add basic lua wrapper library around xtrace

parent 211a4f09
......@@ -47,7 +47,25 @@ install(DIRECTORY "include/"
install(TARGETS libxtrace DESTINATION "${CMAKE_INSTALL_LIBDIR}")
# Compile the Lua library
find_package(Lua51 REQUIRED)
add_library(luaxtrace SHARED
src/luaxtrace.cpp
src/module.cpp
)
target_include_directories(luaxtrace PRIVATE ${LUA_INCLUDE_DIR} /usr/local/include/xtrace)
set_target_properties(luaxtrace PROPERTIES PREFIX "")
set_target_properties(luaxtrace PROPERTIES SUFFIX ".so")
target_link_libraries(luaxtrace libxtrace)
install(TARGETS luaxtrace DESTINATION ${CMAKE_INSTALL_LIBDIR})
# Compile the main example program
add_executable (main src/main.cc )
target_include_directories(main PRIVATE /usr/local/include/xtrace)
......
#ifndef _LUAXTRACE_H_
#define _LUAXTRACE_H_
#ifdef __cplusplus
extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
#endif
namespace lua_xtracer {
extern int StartLuaTrace(lua_State* L);
extern int IsTracing(lua_State* L);
extern void RegisterXtraceLua(lua_State* L);
extern int LogLocal(lua_State* L);
}
#ifdef __cplusplus
}
#endif
#endif
#include "luaxtrace.h"
#include <vector>
#include <string>
#include <iostream>
#include "xtrace.h"
extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
namespace lua_xtracer {
void RegisterXtraceLua(lua_State* L) {
lua_register(L, "StartLuaTrace", StartLuaTrace);
lua_register(L, "IsTracing", IsTracing);
}
int StartLuaTrace(lua_State* L) {
std::cout << "Starting trace\n";
int n = lua_gettop(L);
if (n == 0) {
XTrace::StartTrace();
} else {
std::vector<std::string> tags;
for (int i = 1; i <= n; i++) {
tags.push_back(lua_tostring(L, i));
}
XTrace::StartTrace(tags);
}
return 0;
}
int IsTracing(lua_State* L) {
bool isTracing = XTrace::IsTracing();
lua_pushboolean(L, isTracing);
return 1;
}
int LogLocal(lua_State* L) {
int n = lua_gettop(L);
if (n == 0) {
std::cout << "Invalid use of LogLocal\n";
} else {
std::string msg(lua_tostring(L,1));
XTRACE(msg);
}
return 0;
}
} // end namespace lua_xtracer
#include "luaxtrace.h"
#include <iostream>
extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
// Copied from Lua 5.3 so that we can use it with Lua 5.1.
static void setfuncs(lua_State* L, const luaL_Reg* l, int nup) {
luaL_checkstack(L, nup + 1, "too many upvalues");
for (; l->name != NULL; l++) { /* fill the table with given functions */
int i;
lua_pushstring(L, l->name);
for (i = 0; i < nup; i++) /* copy upvalues to the top */
lua_pushvalue(L, -(nup + 1));
lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */
lua_settable(L, -(nup + 3));
}
lua_pop(L, nup); /* remove upvalues */
}
/** This is the function called when require "luaxtrace" is called
*
*/
extern "C" int luaopen_luaxtrace(lua_State* L) {
std::cout << "lua opening luaxtrace lib" << std::endl;
lua_newtable(L);
const struct luaL_Reg functions[] = {
{"IsTracing", lua_xtracer::IsTracing},
{"StartLuaTrace", lua_xtracer::StartLuaTrace},
{"LogXTrace", lua_xtracer::LogLocal},
{nullptr, nullptr}
};
std::cout << "Registering functions" << std::endl;
setfuncs(L, functions, 0);
return 1;
}
print("Requiring luaxtrace")
local xtracer = require 'luaxtrace'
local tracing = xtracer.IsTracing()
if tracing ~= true then
xtracer.StartLuaTrace("LuaTest")
end
print("Logging now")
xtracer.LogXTrace("Well fancy seeing you here")
os.execute("sleep " .. 5)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment