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.
96 lines
2.9 KiB
C
96 lines
2.9 KiB
C
#pragma once
|
|
#include "basalt_defines.h"
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
#ifndef BASALT_LOGGER_MAX_STREAMS
|
|
#define BASALT_LOGGER_MAX_STREAMS 7
|
|
#endif
|
|
|
|
typedef u8 LOG_LEVEL;
|
|
typedef enum LOG_LEVELS {
|
|
LOG_FATAL = 1,
|
|
LOG_ERROR = 2,
|
|
LOG_WARN = 4,
|
|
LOG_INFO = 8,
|
|
LOG_OK = 16,
|
|
LOG_WEIRD = 32,
|
|
LOG_DEBUG = 64,
|
|
LOG_TRACE = 128,
|
|
|
|
LOG_MASK_ERRORS = LOG_FATAL | LOG_ERROR,
|
|
LOG_MASK_DEBUGGING = LOG_WEIRD | LOG_DEBUG | LOG_TRACE,
|
|
LOG_MASK_INFO = LOG_WARN | LOG_INFO | LOG_OK,
|
|
LOG_MASK_ALL = LOG_MASK_ERRORS | LOG_MASK_DEBUGGING | LOG_MASK_INFO,
|
|
LOG_MASK_DEFAULT = LOG_MASK_ERRORS | LOG_MASK_INFO,
|
|
} LOG_LEVELS;
|
|
|
|
typedef u8 LOG_STREAM_FLAG;
|
|
typedef enum LOG_STREAM_FLAGS {
|
|
// Ignore this flag - it is internal to the library
|
|
LOG_STREAM_FLAG_INTERNAL_ALLOCATED_BIT = 1,
|
|
LOG_STREAM_FLAG_SUPPORTS_ANSI_BIT = 2,
|
|
LOG_STREAM_FLAG_INCLUDE_DATETIME_BIT = 4,
|
|
LOG_STREAM_FLAG_DISABLE_PREFIXES = 8,
|
|
} LOG_STREAM_BIT_FLAGS;
|
|
|
|
typedef struct logger_stream_t {
|
|
u64 stream : 48;
|
|
u64 mask : 8 ;
|
|
u64 flags : 8 ;
|
|
} logger_stream_t;
|
|
|
|
void initialize_logger();
|
|
void terminate_logger ();
|
|
|
|
FILE* logger_stream_get_file(logger_stream_t* state);
|
|
u8 logger_add_stream(FILE* output, LOG_LEVEL mask, LOG_STREAM_FLAG flags);
|
|
void logger_remove_stream(u8 index);
|
|
u8 logger_find_stream(FILE* target);
|
|
|
|
void basalt_log(const LOG_LEVEL level, const char* msg, ...);
|
|
void basalt_write(const LOG_LEVEL level, const char* msg, ...);
|
|
|
|
#define BFATAL(msg, ...) basalt_log(LOG_FATAL, msg, ##__VA_ARGS__);
|
|
#define BERROR(msg, ...) basalt_log(LOG_ERROR, msg, ##__VA_ARGS__);
|
|
#define BWARN(msg, ...) basalt_log(LOG_WARN, msg, ##__VA_ARGS__);
|
|
#define BINFO(msg, ...) basalt_log(LOG_INFO, msg, ##__VA_ARGS__);
|
|
#define BOK(msg, ...) basalt_log(LOG_OK, msg, ##__VA_ARGS__);
|
|
#define BWEIRD(msg, ...) basalt_log(LOG_WEIRD, msg, ##__VA_ARGS__);
|
|
#define BDEBUG(msg, ...) basalt_log(LOG_DEBUG, msg, ##__VA_ARGS__);
|
|
#define BTRACE(msg, ...) basalt_log(LOG_TRACE, msg, ##__VA_ARGS__);
|
|
|
|
#define BASSERT_FATAL(cond, msg, ...) \
|
|
if (cond){} \
|
|
else { \
|
|
BFATAL(msg, #cond, __FILE__, __LINE__, ##__VA_ARGS__); \
|
|
debug_break(); \
|
|
exit(-1); \
|
|
}
|
|
#define BASSERT_ERROR(cond, ret, msg, ...) \
|
|
if (cond){} \
|
|
else { \
|
|
BERROR(msg, #cond, __FILE__, __LINE__, ##__VA_ARGS__); \
|
|
ret \
|
|
}
|
|
#define BASSERT_WARN(cond, msg, ...) \
|
|
if (cond){} \
|
|
else { \
|
|
BWARN(msg, #cond, __FILE__, __LINE__, ##__VA_ARGS__); \
|
|
}
|
|
#ifdef BDEBUG
|
|
#define BASSERT_WEIRD(cond, msg, ...) \
|
|
if (cond){} \
|
|
else { \
|
|
BWEIRD(msg, #cond, __FILE__, __LINE__, ##__VA_ARGS__); \
|
|
}
|
|
#define BASSERT_DEBUG(cond, msg, ...) \
|
|
if (cond){} \
|
|
else { \
|
|
BFATAL(msg, #cond, __FILE__, __LINE__, ##__VA_ARGS__); \
|
|
debug_break(); \
|
|
}
|
|
#else
|
|
#define BASSERT_WEIRD(cond, msg, ...)
|
|
#define BASSERT_DEBUG(cond, msg, ...)
|
|
#endif |