80 lines
2.2 KiB
C
80 lines
2.2 KiB
C
#include "pretty_print.h"
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include "core.h"
|
|
|
|
strview_t pretty__colour[LOG_COL__COUNT] = {
|
|
[LOG_COL_BLACK] = cstrv("black"),
|
|
[LOG_COL_BLUE] = cstrv("blue"),
|
|
[LOG_COL_GREEN] = cstrv("green"),
|
|
[LOG_COL_CYAN] = cstrv("cyan"),
|
|
[LOG_COL_RED] = cstrv("red"),
|
|
[LOG_COL_MAGENTA] = cstrv("magenta"),
|
|
[LOG_COL_YELLOW] = cstrv("yellow"),
|
|
[LOG_COL_GREY] = cstrv("grey"),
|
|
|
|
[LOG_COL_DARK_GREY] = cstrv("dark_grey"),
|
|
[LOG_COL_WHITE] = cstrv("white"),
|
|
[LOG_COL_LIGHT_BLUE] = cstrv("light_blue"),
|
|
[LOG_COL_LIGHT_GREEN] = cstrv("light_green"),
|
|
[LOG_COL_LIGHT_CYAN] = cstrv("light_cyan"),
|
|
[LOG_COL_LIGHT_RED] = cstrv("light_red"),
|
|
[LOG_COL_LIGHT_MAGENTA] = cstrv("light_magenta"),
|
|
[LOG_COL_LIGHT_YELLOW] = cstrv("light_yellow"),
|
|
|
|
[LOG_COL_RESET] = cstrv("/"),
|
|
};
|
|
|
|
strview_t pretty_log_to_colour(os_log_colour_e colour) {
|
|
return pretty__colour[colour];
|
|
}
|
|
|
|
void pretty_print(arena_t scratch, const char *fmt, ...) {
|
|
va_list args;
|
|
va_start(args, fmt);
|
|
pretty_printv(scratch, fmt, args);
|
|
va_end(args);
|
|
}
|
|
|
|
void pretty_printv(arena_t scratch, const char *fmt, va_list args) {
|
|
va_list tmp_args;
|
|
va_copy(tmp_args, args);
|
|
int len = fmt_bufferv(NULL, 0, fmt, tmp_args);
|
|
va_end(tmp_args);
|
|
|
|
char *buf = alloc(&scratch, char, len + 1);
|
|
|
|
fmt_bufferv(buf, len + 1, fmt, args);
|
|
|
|
oshandle_t out = os_stdout();
|
|
instream_t in = istr_init(strv(buf, len));
|
|
while (!istr_is_finished(&in)) {
|
|
strview_t part = istr_get_view(&in, '<');
|
|
bool has_escape = strv_ends_with(part, '\\');
|
|
|
|
if (has_escape) {
|
|
part.len -= 1;
|
|
}
|
|
|
|
os_file_write(out, part.buf, part.len);
|
|
istr_skip(&in, 1);
|
|
|
|
if (has_escape) {
|
|
os_file_putc(out, '<');
|
|
continue;
|
|
}
|
|
|
|
strview_t tag = istr_get_view(&in, '>');
|
|
|
|
for (usize i = 0; i < arrlen(pretty__colour); ++i) {
|
|
if (strv_equals(tag, pretty__colour[i])) {
|
|
os_log_set_colour(i);
|
|
break;
|
|
}
|
|
}
|
|
|
|
istr_skip(&in, 1);
|
|
}
|
|
}
|
|
|