Add colors to debuglog, make smlua use log to terminal instead of printf, and add colors to it aswell

This commit is contained in:
EmeraldLockdown 2026-03-25 15:26:37 -05:00
parent 7fec799bfd
commit f38f2cee77
4 changed files with 47 additions and 30 deletions

View file

@ -126,6 +126,19 @@ function update_chat_command_description(command, description)
-- ...
end
--- @param command string The command to run. Should be easy to type
--- @param description string Should describe what the command does and how to use it
--- @param func fun(msg:string): boolean Run upon activating the command. Return `true` to confirm the command has succeeded
function hook_console_command(command, description, func)
-- ...
end
--- @param command string The command to change the description of
--- @param description string The description to change to
function update_console_command_description(command, description)
-- ...
end
--- @param hookEventType LuaHookedEventType When a function should run
--- @param func fun(...: any): any?, any? The function to run
--- Different hooks can pass in different parameters and have different return values. Be sure to read the hooks guide for more information.
@ -407,13 +420,13 @@ end
--- @param command string
--- @vararg integer | string | Gfx | Texture | Vtx Parameters for the command
--- Sets a display list command on the display list given.
---
---
--- If `command` includes parameter specifiers (subsequences beginning with `%`), the additional arguments
--- following `command` are converted and inserted in `command` replacing their respective specifiers.
---
---
--- The number of provided parameters must be equal to the number of specifiers in `command`,
--- and the order of parameters must be the same as the specifiers.
---
---
--- The following specifiers are allowed:
--- - `%i` for an `integer` parameter
--- - `%s` for a `string` parameter

View file

@ -39,12 +39,16 @@ static int _debuglog_print_short_filename(const char* filename, char* buffer, si
}
}
static inline void _debuglog_print_log(const char* logType, const char* filename, const char* fmt, ...) {
static inline void _debuglog_print_log(const char* color, const char* logType, const char* filename, const char* fmt, ...) {
char log[MAX_LOG_SIZE];
size_t capacity = MAX_LOG_SIZE;
char* buffer = log;
int len;
int len = 0;
len = snprintf(buffer, capacity, "%s", color);
if (len < 0 || (size_t)len >= capacity) return;
buffer += len; capacity -= len;
len = _debuglog_print_timestamp(buffer, capacity);
if (len < 0 || (size_t)len >= capacity) return;
@ -71,7 +75,7 @@ static inline void _debuglog_print_log(const char* logType, const char* filename
if (len < 0) return;
log_to_terminal("%s\n", log);
log_to_terminal("%s\x1b[0m\n", log);
}
#if defined(DISABLE_MODULE_LOG)
@ -79,9 +83,9 @@ static inline void _debuglog_print_log(const char* logType, const char* filename
#define LOG_INFO(...)
#define LOG_ERROR(...)
#else
#define LOG_DEBUG(...) (configDebugPrint ? ( _debuglog_print_log("DEBUG", __FILE__, __VA_ARGS__) ) : 0)
#define LOG_INFO(...) ((configDebugInfo || gCLIOpts.headless) ? ( _debuglog_print_log("INFO", __FILE__, __VA_ARGS__) ) : 0)
#define LOG_ERROR(...) (configDebugError ? ( _debuglog_print_log("ERROR", __FILE__, __VA_ARGS__) ) : 0)
#define LOG_DEBUG(...) (configDebugPrint ? ( _debuglog_print_log("", "DEBUG", __FILE__, __VA_ARGS__) ) : 0)
#define LOG_INFO(...) ((configDebugInfo || gCLIOpts.headless) ? ( _debuglog_print_log("", "INFO", __FILE__, __VA_ARGS__) ) : 0)
#define LOG_ERROR(...) (configDebugError ? ( _debuglog_print_log("\x1b[31m", "ERROR", __FILE__, __VA_ARGS__) ) : 0)
#endif
#define LOG_CONSOLE(...) { snprintf(gDjuiConsoleTmpBuffer, CONSOLE_MAX_TMP_BUFFER, __VA_ARGS__), djui_console_message_create(gDjuiConsoleTmpBuffer, CONSOLE_MESSAGE_INFO); }

View file

@ -19,9 +19,9 @@
#include "pc/debuglog.h"
#include "pc/djui/djui_console.h"
#define LOG_LUA(...) { if (!gSmLuaSuppressErrors) { printf("[LUA] "), printf(__VA_ARGS__), printf("\n"), smlua_mod_error(), snprintf(gDjuiConsoleTmpBuffer, CONSOLE_MAX_TMP_BUFFER, __VA_ARGS__), sys_swap_backslashes(gDjuiConsoleTmpBuffer), djui_console_message_create(gDjuiConsoleTmpBuffer, CONSOLE_MESSAGE_ERROR); } }
#define LOG_LUA_LINE(...) { if (!gSmLuaSuppressErrors) { printf("[LUA] "), printf(__VA_ARGS__), printf("\n"), smlua_mod_error(); snprintf(gDjuiConsoleTmpBuffer, CONSOLE_MAX_TMP_BUFFER, __VA_ARGS__), sys_swap_backslashes(gDjuiConsoleTmpBuffer), djui_console_message_create(gDjuiConsoleTmpBuffer, CONSOLE_MESSAGE_ERROR), smlua_logline(); } }
#define LOG_LUA_LINE_WARNING(...) { if (!gLuaActiveMod->showedScriptWarning) { gLuaActiveMod->showedScriptWarning = true; smlua_mod_warning(); snprintf(gDjuiConsoleTmpBuffer, CONSOLE_MAX_TMP_BUFFER, __VA_ARGS__), sys_swap_backslashes(gDjuiConsoleTmpBuffer), djui_console_message_create(gDjuiConsoleTmpBuffer, CONSOLE_MESSAGE_WARNING); } }
#define LOG_LUA(...) { if (!gSmLuaSuppressErrors) { log_to_terminal("\x1b[31m[LUA] "); log_to_terminal(__VA_ARGS__); log_to_terminal("\x1b[0m\n"); smlua_mod_error(); snprintf(gDjuiConsoleTmpBuffer, CONSOLE_MAX_TMP_BUFFER, __VA_ARGS__); sys_swap_backslashes(gDjuiConsoleTmpBuffer); djui_console_message_create(gDjuiConsoleTmpBuffer, CONSOLE_MESSAGE_ERROR); } }
#define LOG_LUA_LINE(...) { if (!gSmLuaSuppressErrors) { log_to_terminal("\x1b[31m[LUA] "); log_to_terminal(__VA_ARGS__); log_to_terminal("\x1b[0m\n"); smlua_mod_error(); snprintf(gDjuiConsoleTmpBuffer, CONSOLE_MAX_TMP_BUFFER, __VA_ARGS__); sys_swap_backslashes(gDjuiConsoleTmpBuffer); djui_console_message_create(gDjuiConsoleTmpBuffer, CONSOLE_MESSAGE_ERROR); smlua_logline(); } }
#define LOG_LUA_LINE_WARNING(...) { if (!gLuaActiveMod->showedScriptWarning) { gLuaActiveMod->showedScriptWarning = true; log_to_terminal("\x1b[33m[LUA] "); log_to_terminal(__VA_ARGS__); log_to_terminal("\x1b[0m\n"); smlua_mod_warning(); snprintf(gDjuiConsoleTmpBuffer, CONSOLE_MAX_TMP_BUFFER, __VA_ARGS__); sys_swap_backslashes(gDjuiConsoleTmpBuffer); djui_console_message_create(gDjuiConsoleTmpBuffer, CONSOLE_MESSAGE_WARNING); } }
#ifdef DEVELOPMENT
#define LUA_STACK_CHECK_BEGIN_NUM(state, n) int __LUA_STACK_TOP = lua_gettop(state) + (n)

View file

@ -743,33 +743,33 @@ const char* smlua_lnt_to_str(struct LSTNetworkType* lnt) {
void smlua_dump_stack(void) {
lua_State* L = gLuaState;
int top = lua_gettop(L);
printf("--------------\n");
log_to_terminal("--------------\n");
for (int i = 1; i <= top; i++) {
printf("%d\t%s\t", i, luaL_typename(L, i));
log_to_terminal("%d\t%s\t", i, luaL_typename(L, i));
switch (lua_type(L, i)) {
case LUA_TNUMBER:
printf("%g\n", lua_tonumber(L, i));
log_to_terminal("%g\n", lua_tonumber(L, i));
break;
case LUA_TSTRING:
printf("%s\n", lua_tostring(L, i));
log_to_terminal("%s\n", lua_tostring(L, i));
break;
case LUA_TBOOLEAN:
printf("%s\n", (lua_toboolean(L, i) ? "true" : "false"));
log_to_terminal("%s\n", (lua_toboolean(L, i) ? "true" : "false"));
break;
case LUA_TNIL:
printf("%s\n", "nil");
log_to_terminal("%s\n", "nil");
break;
default:
printf("%p\n", lua_topointer(L, i));
log_to_terminal("%p\n", lua_topointer(L, i));
break;
}
}
printf("--------------\n");
log_to_terminal("--------------\n");
}
void smlua_dump_globals(void) {
lua_State* L = gLuaState;
printf("--------------\n");
log_to_terminal("--------------\n");
lua_pushglobaltable(L);
// table is in the stack at index 't'
@ -777,12 +777,12 @@ void smlua_dump_globals(void) {
while (lua_next(L, -2) != 0) {
// uses 'key' (at index -2) and 'value' (at index -1)
if (lua_type(L, -2) == LUA_TSTRING) {
printf("%s - %s\n",
log_to_terminal("%s - %s\n",
lua_tostring(L, -2),
lua_typename(L, lua_type(L, -1)));
}
else {
printf("%s - %s\n",
log_to_terminal("%s - %s\n",
lua_typename(L, lua_type(L, -2)),
lua_typename(L, lua_type(L, -1)));
}
@ -790,23 +790,23 @@ void smlua_dump_globals(void) {
lua_pop(L, 1);
}
lua_pop(L, 1); // remove global table(-1)
printf("--------------\n");
log_to_terminal("--------------\n");
}
void smlua_dump_table(int index) {
lua_State* L = gLuaState;
printf("--------------\n");
log_to_terminal("--------------\n");
if (lua_getmetatable(L, index)) {
lua_pushnil(L); // first key
while (lua_next(L, -2) != 0) {
if (lua_type(L, -2) == LUA_TSTRING) {
printf("[meta] %s - %s\n",
log_to_terminal("[meta] %s - %s\n",
lua_tostring(L, -2),
lua_typename(L, lua_type(L, -1)));
}
else {
printf("[meta] %s - %s\n",
log_to_terminal("[meta] %s - %s\n",
lua_typename(L, lua_type(L, -2)),
lua_typename(L, lua_type(L, -1)));
}
@ -820,19 +820,19 @@ void smlua_dump_table(int index) {
while (lua_next(L, index) != 0) {
// uses 'key' (at index -2) and 'value' (at index -1)
if (lua_type(L, -2) == LUA_TSTRING) {
printf("%s - %s\n",
log_to_terminal("%s - %s\n",
lua_tostring(L, -2),
lua_typename(L, lua_type(L, -1)));
}
else {
printf("%s - %s\n",
log_to_terminal("%s - %s\n",
lua_typename(L, lua_type(L, -2)),
lua_typename(L, lua_type(L, -1)));
}
// removes 'value'; keeps 'key' for next iteration
lua_pop(L, 1);
}
printf("--------------\n");
log_to_terminal("--------------\n");
}
void smlua_logline(void) {