summaryrefslogtreecommitdiff
path: root/src/lua_core.c
diff options
context:
space:
mode:
authorjussi2022-03-04 15:46:22 +0200
committerjussi2022-03-04 15:46:22 +0200
commit8035d31109964504d970ed302c7aa6ffa0a8f106 (patch)
treebc28faf7c9616394b820d7a16ca9636f14eb9c5e /src/lua_core.c
parentc3ae0a6c28316174891f8361ee0226d699a24013 (diff)
downloadreilua-enhanced-8035d31109964504d970ed302c7aa6ffa0a8f106.tar.gz
reilua-enhanced-8035d31109964504d970ed302c7aa6ffa0a8f106.tar.bz2
reilua-enhanced-8035d31109964504d970ed302c7aa6ffa0a8f106.zip
Custom log function.
Diffstat (limited to 'src/lua_core.c')
-rw-r--r--src/lua_core.c40
1 files changed, 40 insertions, 0 deletions
diff --git a/src/lua_core.c b/src/lua_core.c
index 55e0fda..ece4c97 100644
--- a/src/lua_core.c
+++ b/src/lua_core.c
@@ -204,6 +204,44 @@ void defineGlobals() {
/*DOC_END*/
}
+// Custom logging funtion
+void LogCustom( int msgType, const char *text, va_list args ) {
+ char string[ STRING_LEN ] = {'\0'};
+ char msg[ STRING_LEN ] = {'\0'};
+
+ vsprintf( string, text, args );
+
+ switch ( msgType ) {
+ case LOG_ALL: sprintf( msg, "ALL: %s", string ); break;
+ case LOG_TRACE: sprintf( msg, "TRACE: %s", string ); break;
+ case LOG_DEBUG: sprintf( msg, "DEBUG: %s", string ); break;
+ case LOG_INFO: sprintf( msg, "INFO: %s", string ); break;
+ case LOG_WARNING: sprintf( msg, "WARNING: %s", string ); break;
+ case LOG_ERROR: sprintf( msg, "ERROR: %s", string ); break;
+ case LOG_FATAL: sprintf( msg, "FATAL: %s", string ); break;
+ default: break;
+ }
+ printf( "%s\n", msg );
+
+ /* Call Lua log function if exists. */
+ lua_State *L = state->luaState;
+
+ lua_pushcfunction( L, luaTraceback );
+ int tracebackidx = lua_gettop( L );
+ lua_getglobal( L, "log" );
+
+ if ( lua_isfunction( L, -1 ) ) {
+ lua_pushstring( L, msg );
+
+ if ( lua_pcall( L, 1, 0, tracebackidx ) != 0 ) {
+ TraceLog( LOG_WARNING, "Lua error: %s", lua_tostring( L, -1 ) );
+ state->run = false;
+ return;
+ }
+ }
+ lua_pop( L, -1 );
+}
+
bool luaInit() {
state->luaState = luaL_newstate();
@@ -284,6 +322,8 @@ bool luaCallMain() {
TraceLog( LOG_WARNING, "%s", "No Lua main found!" );
return false;
}
+ /* Apply custom callback here. */
+ SetTraceLogCallback( LogCustom );
return true;
}