Fixed compilation error in basalt_logger where it whined about or'ing together two enum values for the log level.

Fixed a compilation error where the log level mask was being shadowed by log level index in basalt_log and basalt_write.
Fixed a bug where logger_add_stream was checking the lower 48-bits of the FILE* rather than the upper 16 were zero.
Fixed a bug where logger_add_stream did not mark a stream as allocated in its flags.
Fixed a bug where logger_remove_stream did not check if a stream was allocated.
Addressed warnings of potential saftey issues when logging the prefixes as if they were format strings by having them be format-specified. I suspect this is marginally slower and realistically had no security risk in the first place but was fixed anyway.
This commit is contained in:
2025-06-23 00:41:41 +10:00
parent f07977ff06
commit 87f66e291b
2 changed files with 19 additions and 15 deletions

View File

@@ -43,18 +43,18 @@ inline FILE* logger_stream_get_file(logger_stream_t* state)
return (FILE*)((state->stream & 0xffffffffffff) | ((u64)stdout & 0xffff0000000000));
}
u8 logger_add_stream(FILE* output, LOG_LEVEL mask, LOG_STREAM_FLAGS flags)
u8 logger_add_stream(FILE* output, LOG_LEVEL mask, LOG_STREAM_FLAG flags)
{
if (logger_state.num_streams == BASALT_LOGGER_MAX_STREAMS)
return -1;
if ((((u64)output) & 0xffffffffffff) != 0)
if ((((u64)output) & (~0xffffffffffff)) != 0)
__builtin_trap();
for (u8 i = 0; i < BASALT_LOGGER_MAX_STREAMS; ++i)
{
if ((logger_state.streams[i].flags & LOG_STREAM_FLAG_INTERNAL_ALLOCATED_BIT) == 0)
{
logger_state.streams[i].stream = (u64)output;
logger_state.streams[i].flags = flags;
logger_state.streams[i].flags = flags | LOG_STREAM_FLAG_INTERNAL_ALLOCATED_BIT;
logger_state.streams[i].mask = mask;
logger_state.num_streams++;
return i;
@@ -68,6 +68,8 @@ void logger_remove_stream(u8 index)
if (index >= sizeof(logger_state.streams) / sizeof(logger_state.streams[0]))
return;
logger_stream_t* stream = logger_state.streams+index;
if ((stream->flags & LOG_STREAM_FLAG_INTERNAL_ALLOCATED_BIT) == 0)
return;
FILE* f = logger_stream_get_file(stream);
if (f != stdout && f != stderr)
fclose(f);
@@ -94,8 +96,8 @@ void basalt_log(const LOG_LEVEL level, const char* msg, ...)
const char* log_ansi_prefixes[8] = { "\033[38;5;0m\033[48;5;9m", "\033[38;5;9m", "\033[38;5;11m", "\033[38;5;15m", "\033[38;5;10m", "\033[38;5;13m", "\033[38;5;12m", "\033[38;5;6m" };
if (level == 0)
return;
const u8 level = (u8)__builtin_ctz(level);
if (level > 8)
const u8 err_level = (u8)__builtin_ctz(level);
if (err_level > 8)
{
debug_break();
return;
@@ -115,7 +117,7 @@ void basalt_log(const LOG_LEVEL level, const char* msg, ...)
continue;
FILE* out = logger_stream_get_file(stream);
if (stream->flags & LOG_STREAM_FLAG_SUPPORTS_ANSI_BIT)
fprintf(out, log_ansi_prefixes[level]);
fprintf(out, "%s", log_ansi_prefixes[err_level]);
if ((stream->flags & LOG_STREAM_FLAG_DISABLE_PREFIXES) == 0)
{
if (stream->flags & LOG_STREAM_FLAG_INCLUDE_DATETIME_BIT)
@@ -127,13 +129,13 @@ void basalt_log(const LOG_LEVEL level, const char* msg, ...)
if (!err)
{
int n_chars = (int)strftime(time_buffer, sizeof(time_buffer), "%d-%m-%y %R:%S", &tm_info);
fprintf(out, "[%*s %*s]: ", sizeof(log_prefixes[0]), log_prefixes[level], n_chars, time_buffer);
fprintf(out, "[%*s %*s]: ", (int)sizeof(log_prefixes[0]), log_prefixes[err_level], n_chars, time_buffer);
}
else
fprintf(out, "[%*s]: ", sizeof(log_prefixes[0]), log_prefixes[level]);
fprintf(out, "[%*s]: ", (int)sizeof(log_prefixes[0]), log_prefixes[err_level]);
}
else
fprintf(out, "[%*s]: ", sizeof(log_prefixes[0]), log_prefixes[level]);
fprintf(out, "[%*s]: ", (int)sizeof(log_prefixes[0]), log_prefixes[err_level]);
}
__builtin_va_list argstart;
va_start(argstart, msg);
@@ -151,8 +153,8 @@ void basalt_write(const LOG_LEVEL level, const char* msg, ...)
const char* log_ansi_prefixes[8] = { "\033[38;5;0m\033[48;5;9m", "\033[38;5;9m", "\033[38;5;11m", "\033[38;5;15m", "\033[38;5;10m", "\033[38;5;13m", "\033[38;5;12m", "\033[38;5;6m" };
if (level == 0)
return;
const u8 level = (u8)__builtin_ctz(level);
if (level > 8)
const u8 err_level = (u8)__builtin_ctz(level);
if (err_level > 8)
{
debug_break();
return;
@@ -172,7 +174,7 @@ void basalt_write(const LOG_LEVEL level, const char* msg, ...)
continue;
FILE* out = logger_stream_get_file(stream);
if (stream->flags & LOG_STREAM_FLAG_SUPPORTS_ANSI_BIT)
fprintf(out, log_ansi_prefixes[level]);
fprintf(out, "%s", log_ansi_prefixes[err_level]);
__builtin_va_list argstart;
va_start(argstart, msg);