Files
osc2025/include/logger.h
2025-03-24 10:48:35 +08:00

108 lines
2.1 KiB
C

#pragma once
#include <stddef.h>
#include <uart.h>
#ifndef LOGLEVEL
#define LOGLEVEL 3
#endif
#define LOGGER_BUFLEN 0x100
static inline char *_do_nothing(char *dest, const char *) { return dest; }
static inline char *_logger_string(char *dest, const char *src)
{
while (*src != '\0')
*dest++ = *src++;
*dest = '\0';
return dest;
}
static inline char *_logger_hex(char *dest, uint64_t x)
{
for (int n, c = 28; c >= 0; c -= 4) {
n = (x >> c) & 0xf;
n += (n > 9) ? 0x37 : 0x30;
*dest++ = (char)n;
}
*dest = '\0';
return dest;
}
static inline char *_logger_pointer(char *dest, void *x)
{
*dest++ = '0';
*dest++ = 'x';
return _logger_hex(dest, (uint64_t)x);
}
#define _I_HATE_C_LANG(msg) \
logger_cur = _Generic((msg), \
char * : _do_nothing, \
const char *: _do_nothing, \
default : _logger_string \
)(logger_cur, #msg " = "); \
logger_cur = _Generic((msg), \
char * : _logger_string, \
const char *: _logger_string, \
uint64_t : _logger_hex, \
default : _logger_pointer \
)(logger_cur, msg)
#define LOG(val) { \
if (logger_cur != logger_buf) \
logger_cur = _logger_string(logger_cur, ", "); \
_I_HATE_C_LANG(val); \
}
#define FLUSH(prefix) { \
uart_puts(prefix); \
uart_puts(logger_buf); \
uart_puts(ENDL); \
logger_cur = logger_buf; \
}
#define CLEAN { \
logger_cur = logger_buf; \
}
#if LOGLEVEL >= 0
#define ERROR(val) { \
LOG(val); \
FLUSH("[ERROR]: "); \
}
#else
#define ERROR(val) CLEAN
#endif
#if LOGLEVEL >= 1
#define INFOR(val) { \
LOG(val); \
FLUSH("[INFOR]: "); \
}
#else
#define INFOR(val) CLEAN
#endif
#if LOGLEVEL >= 2
#define DEBUG(val) { \
LOG(val); \
FLUSH("[DEBUG]: "); \
}
#else // #if LOGLEVEL >= 2
#define DEBUG(val) CLEAN
#endif // #if LOGLEVEL >= 2
// #define DEBUG_DTB(val) DEBUG(val)
#define DEBUG_DTB(val) CLEAN
#define DEBUG_EXCEP(val) DEBUG(val)
// #define DEBUG_EXCEP(val) CLEAN
// #define DEBUG_MEM(val) DEBUG(val)
#define DEBUG_MEM(val) CLEAN
// #define DEBUG_INITRD(val) DEBUG(val)
#define DEBUG_INITRD(val) CLEAN
extern char logger_buf[LOGGER_BUFLEN];
extern char *logger_cur;