Custom log function logLevel.

This commit is contained in:
jussi
2022-06-19 20:38:50 +03:00
parent 1529202e26
commit 6bac1a2c63
4 changed files with 22 additions and 12 deletions

2
API.md
View File

@@ -25,7 +25,7 @@ You can still use RL_BeginDrawing() and RL_EndDrawing() manually from anywhere.
--- ---
> function log( message ) > function log( logLevel, message )
This function can be used for custom log message handling. This function can be used for custom log message handling.

View File

@@ -32,7 +32,7 @@ apiFile:write( "\n> function draw()\n\
This function will be called every frame after process and it should have all rendering related functions.\ This function will be called every frame after process and it should have all rendering related functions.\
Note: Engine will call Raylib functions 'BeginDrawing()' before this function call and 'EndDrawing()' after it.\ Note: Engine will call Raylib functions 'BeginDrawing()' before this function call and 'EndDrawing()' after it.\
You can still use RL_BeginDrawing() and RL_EndDrawing() manually from anywhere.\n\n---\n" ) You can still use RL_BeginDrawing() and RL_EndDrawing() manually from anywhere.\n\n---\n" )
apiFile:write( "\n> function log( message )\n\ apiFile:write( "\n> function log( logLevel, message )\n\
This function can be used for custom log message handling.\n\n---\n" ) This function can be used for custom log message handling.\n\n---\n" )
-- Globals. -- Globals.

View File

@@ -80,6 +80,12 @@ local function createMap()
tilemap.tiles[1][8] = 6 tilemap.tiles[1][8] = 6
end end
function log( logLevel, message )
if logLevel == LOG_WARNING then
error( "Terminated because of warning" )
end
end
function init() function init()
local monitorPos = Vec2:new( RL_GetMonitorPosition( monitor ) ) local monitorPos = Vec2:new( RL_GetMonitorPosition( monitor ) )
local monitorSize = Vec2:new( RL_GetMonitorSize( monitor ) ) local monitorSize = Vec2:new( RL_GetMonitorSize( monitor ) )

View File

@@ -476,13 +476,13 @@ void defineGlobals() {
} }
// Custom logging funtion // Custom logging funtion
void LogCustom( int msgType, const char *text, va_list args ) { void LogCustom( int logLevel, const char *text, va_list args ) {
char string[ STRING_LEN ] = {'\0'}; char string[ STRING_LEN ] = {'\0'};
char msg[ STRING_LEN ] = {'\0'}; char msg[ STRING_LEN ] = {'\0'};
vsprintf( string, text, args ); vsprintf( string, text, args );
switch ( msgType ) { switch ( logLevel ) {
case LOG_ALL: sprintf( msg, "ALL: %s", string ); break; case LOG_ALL: sprintf( msg, "ALL: %s", string ); break;
case LOG_TRACE: sprintf( msg, "TRACE: %s", string ); break; case LOG_TRACE: sprintf( msg, "TRACE: %s", string ); break;
case LOG_DEBUG: sprintf( msg, "DEBUG: %s", string ); break; case LOG_DEBUG: sprintf( msg, "DEBUG: %s", string ); break;
@@ -499,14 +499,17 @@ void LogCustom( int msgType, const char *text, va_list args ) {
lua_pushcfunction( L, luaTraceback ); lua_pushcfunction( L, luaTraceback );
int tracebackidx = lua_gettop( L ); int tracebackidx = lua_gettop( L );
lua_getglobal( L, "log" ); lua_getglobal( L, "log" );
if ( lua_isfunction( L, -1 ) ) { if ( lua_isfunction( L, -1 ) ) {
lua_pushinteger( L, logLevel );
lua_pushstring( L, msg ); lua_pushstring( L, msg );
if ( lua_pcall( L, 1, 0, tracebackidx ) != 0 ) { if ( lua_pcall( L, 2, 0, tracebackidx ) != 0 ) {
TraceLog( LOG_WARNING, "Lua error: %s", lua_tostring( L, -1 ) ); TraceLog( LOG_ERROR, "Lua error: %s", lua_tostring( L, -1 ) );
state->run = false; state->run = false;
lua_pop( L, -1 );
return; return;
} }
} }
@@ -575,7 +578,7 @@ bool luaCallMain() {
/* Check errors in main.lua */ /* Check errors in main.lua */
if ( lua_tostring( state->luaState, -1 ) ) { if ( lua_tostring( state->luaState, -1 ) ) {
TraceLog( LOG_WARNING, "Lua error: %s\n", lua_tostring( state->luaState, -1 ) ); TraceLog( LOG_ERROR, "Lua error: %s\n", lua_tostring( state->luaState, -1 ) );
} }
lua_pushcfunction( L, luaTraceback ); lua_pushcfunction( L, luaTraceback );
@@ -585,12 +588,12 @@ bool luaCallMain() {
if ( lua_isfunction( L, -1 ) ) { if ( lua_isfunction( L, -1 ) ) {
if ( lua_pcall( L, 0, 0, tracebackidx ) != 0 ) { if ( lua_pcall( L, 0, 0, tracebackidx ) != 0 ) {
TraceLog( LOG_WARNING, "Lua error: %s", lua_tostring( L, -1 ) ); TraceLog( LOG_ERROR, "Lua error: %s", lua_tostring( L, -1 ) );
return false; return false;
} }
} }
else { else {
TraceLog( LOG_WARNING, "%s", "No Lua main found!" ); TraceLog( LOG_ERROR, "%s", "No Lua main found!" );
return false; return false;
} }
/* Apply custom callback here. */ /* Apply custom callback here. */
@@ -611,8 +614,9 @@ void luaCallProcess() {
lua_pushnumber( L, GetFrameTime() ); lua_pushnumber( L, GetFrameTime() );
if ( lua_pcall( L, 1, 0, tracebackidx ) != 0 ) { if ( lua_pcall( L, 1, 0, tracebackidx ) != 0 ) {
TraceLog( LOG_WARNING, "Lua error: %s", lua_tostring( L, -1 ) ); TraceLog( LOG_ERROR, "Lua error: %s", lua_tostring( L, -1 ) );
state->run = false; state->run = false;
lua_pop( L, -1 );
return; return;
} }
} }
@@ -635,8 +639,8 @@ void luaCallDraw() {
BeginDrawing(); BeginDrawing();
if ( lua_pcall( L, 0, 0, tracebackidx ) != 0 ) { if ( lua_pcall( L, 0, 0, tracebackidx ) != 0 ) {
TraceLog( LOG_WARNING, "Lua error: %s", lua_tostring( L, -1 ) ); TraceLog( LOG_ERROR, "Lua error: %s", lua_tostring( L, -1 ) );
// state->run = false; state->run = false;
return; return;
} }