Added logging to lua-side, logging library is complete

This commit is contained in:
j4nk 2023-05-23 11:26:47 -04:00
parent 1576e64176
commit a6c18d293a
4 changed files with 60 additions and 0 deletions

1
main.c
View File

@ -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");

View File

@ -1,6 +1,10 @@
#include <stdio.h>
#include <string.h>
#include <lua5.3/lualib.h>
#include <lua5.3/lauxlib.h>
#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;
}

View File

@ -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 <lua5.3/lualib.h>
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

View File

@ -3,6 +3,8 @@ require("termbox_defs")
math.randomseed(os.time())
OCT_LOG_INFO("Hello, world! From lua");
width = 74;
height = 25;