Feat: lab 4
This commit is contained in:
95
include/logger.h
Normal file
95
include/logger.h
Normal file
@@ -0,0 +1,95 @@
|
||||
#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; \
|
||||
}
|
||||
|
||||
#if LOGLEVEL >= 0
|
||||
#define ERROR(val) { \
|
||||
LOG(val); \
|
||||
FLUSH("[ERROR]: "); \
|
||||
}
|
||||
#else
|
||||
#define ERROR(val)
|
||||
#endif
|
||||
|
||||
#if LOGLEVEL >= 1
|
||||
#define INFOR(val) { \
|
||||
LOG(val); \
|
||||
FLUSH("[INFOR]: "); \
|
||||
}
|
||||
#else
|
||||
#define INFOR(val)
|
||||
#endif
|
||||
|
||||
#if LOGLEVEL >= 2
|
||||
#define DEBUG(val) { \
|
||||
LOG(val); \
|
||||
FLUSH("[DEBUG]: "); \
|
||||
}
|
||||
#define DEBUG_DTB(val) DEBUG(val)
|
||||
#define DEBUG_MEM(val) DEBUG(val)
|
||||
#else // #if LOGLEVEL >= 2
|
||||
#define DEBUG(val)
|
||||
#define DEBUG_DTB(val)
|
||||
#define DEBUG_MEM(val)
|
||||
#endif // #if LOGLEVEL >= 2
|
||||
|
||||
extern char logger_buf[LOGGER_BUFLEN];
|
||||
extern char *logger_cur;
|
||||
Reference in New Issue
Block a user