diff --git a/main.c b/main.c index e41c4aa..572eb23 100644 --- a/main.c +++ b/main.c @@ -189,6 +189,7 @@ int initialize_everything(char* lua_file) { OCT_LOG_INFO("Initialized lua state"); luaL_openlibs(L); oct_tb_initialize_lua(L); + oct_log_init_lua(L); if (luaL_dofile(L, lua_file)) return OCT_LUA_FILE_NOT_FOUND; OCT_LOG_INFO("Begin running oct_init()"); lua_getglobal(L, "oct_init"); diff --git a/oct_log.c b/oct_log.c index b9ad673..4b6a20b 100644 --- a/oct_log.c +++ b/oct_log.c @@ -1,6 +1,10 @@ #include +#include +#include +#include #include "oct_log.h" + FILE* oct_log_output_file; int oct_log_level; @@ -24,3 +28,40 @@ int oct_log_deinit() { } return 1; } + +int oct_log_init_lua(lua_State* L) { + lua_pushcfunction(L, oct_log_error_lua); + lua_setglobal(L, "OCT_LOG_ERROR"); + lua_pushcfunction(L, oct_log_warning_lua); + lua_setglobal(L, "OCT_LOG_WARNING"); + lua_pushcfunction(L, oct_log_info_lua); + lua_setglobal(L, "OCT_LOG_INFO"); + lua_pushcfunction(L, oct_log_debug_lua); + lua_setglobal(L, "OCT_LOG_DEBUG"); + return 1; +} + +int oct_log_error_lua(lua_State* L) { + char buffer[OCT_LOG_MAX_MESSAGE_LENGTH_LUA]; + strncpy(buffer, luaL_checkstring(L, -1), OCT_LOG_MAX_MESSAGE_LENGTH_LUA); + OCT_LOG_ERROR("%s", buffer); + return 1; +} +int oct_log_warning_lua(lua_State* L) { + char buffer[OCT_LOG_MAX_MESSAGE_LENGTH_LUA]; + strncpy(buffer, luaL_checkstring(L, -1), OCT_LOG_MAX_MESSAGE_LENGTH_LUA); + OCT_LOG_WARNING("%s", buffer); + return 1; +} +int oct_log_info_lua(lua_State* L) { + char buffer[OCT_LOG_MAX_MESSAGE_LENGTH_LUA]; + strncpy(buffer, luaL_checkstring(L, -1), OCT_LOG_MAX_MESSAGE_LENGTH_LUA); + OCT_LOG_INFO("%s", buffer); + return 1; +} +int oct_log_debug_lua(lua_State* L) { + char buffer[OCT_LOG_MAX_MESSAGE_LENGTH_LUA]; + strncpy(buffer, luaL_checkstring(L, -1), OCT_LOG_MAX_MESSAGE_LENGTH_LUA); + OCT_LOG_DEBUG("%s", buffer); + return 1; +} diff --git a/oct_log.h b/oct_log.h index 31cb8dc..a3581da 100644 --- a/oct_log.h +++ b/oct_log.h @@ -8,6 +8,10 @@ #define OCT_LOG_LEVEL_INFO 2 #define OCT_LOG_LEVEL_DEBUG 3 +#define OCT_LOG_MAX_MESSAGE_LENGTH_LUA 1024 + +#include + extern FILE* oct_log_output_file; extern int oct_log_level; @@ -43,4 +47,16 @@ extern int oct_log_level; int oct_log_init(char* filename, int level); int oct_log_deinit(); +// Logging on the C-side (oct_log_init) is initialized before lua +// Thus, we have to have a separate function for initializing logging on the +// lua-side that is called after lua is initialized +int oct_log_init_lua(lua_State* L); + +// Can't call macros directly from lua, these are just wrapper functions +// for the above macros +int oct_log_error_lua(lua_State* L); +int oct_log_warning_lua(lua_State* L); +int oct_log_info_lua(lua_State* L); +int oct_log_debug_lua(lua_State* L); + #endif diff --git a/pong.lua b/pong.lua index 792f13a..aeaf587 100644 --- a/pong.lua +++ b/pong.lua @@ -3,6 +3,8 @@ require("termbox_defs") math.randomseed(os.time()) +OCT_LOG_INFO("Hello, world! From lua"); + width = 74; height = 25;