open-card-table/oct_log.c

68 lines
1.7 KiB
C

#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;
// If filename is null, default to stderr
int oct_log_init(char* filename, int level) {
if (filename) {
oct_log_output_file = fopen(filename, "w");
}
else {
oct_log_output_file = stderr;
}
oct_log_level = level;
OCT_LOG_INFO("Initialized logging, output file: %s", filename ? filename : "stderr");
return 1;
}
int oct_log_deinit() {
if (oct_log_output_file != stderr) {
fclose(oct_log_output_file);
}
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;
}