@@ -20,14 +20,24 @@ import stdarg local;
2020
2121import color local;
2222
23- bool use_color = false;
2423bool show_debug = false;
2524bool show_timing = false;
2625
2726const u32 BUF_SIZE = 4096;
2827
28+ const char* col_debug = color.Blue;
29+ const char* col_warning = color.Yellow;
30+ const char* col_error = color.Red;
31+ const char* col_timing = color.Blue;
32+ const char* col_normal = color.Normal;
33+
2934public fn void init() {
30- use_color = color.useColor();
35+ color.useColor();
36+ col_debug = color.getConfigColor("debug", color.Blue);
37+ col_warning = color.getConfigColor("warning", color.Yellow);
38+ col_error = color.getConfigColor("error", color.Red);
39+ col_timing = color.getConfigColor("timing", color.Blue);
40+ col_normal = color.normal;
3141}
3242
3343public fn void setDebug(bool enable) {
@@ -46,11 +56,7 @@ public fn void debug(const char* format @(printf_format), ...) {
4656 va_start(args, format);
4757 vsnprintf(buf, sizeof(buf), format, args);
4858 va_end(args);
49- if (use_color) {
50- printf("%s%s%s\n", color.Blue, buf, color.Normal);
51- } else {
52- printf("%s\n", buf);
53- }
59+ printf("%s%s%s\n", col_debug, buf, col_normal);
5460}
5561
5662public fn void log(const char* format @(printf_format), ...) {
@@ -68,11 +74,7 @@ public fn void warn(const char* format @(printf_format), ...) {
6874 va_start(args, format);
6975 vsnprintf(buf, sizeof(buf), format, args);
7076 va_end(args);
71- if (use_color) {
72- fprintf(stderr, "%swarning: %s%s\n", color.Yellow, buf, color.Normal);
73- } else {
74- fprintf(stderr, "warning: %s\n", buf);
75- }
77+ fprintf(stderr, "%swarning: %s%s\n", col_warning, buf, col_normal);
7678}
7779
7880public fn void error(const char* format @(printf_format), ...) {
@@ -81,11 +83,7 @@ public fn void error(const char* format @(printf_format), ...) {
8183 va_start(args, format);
8284 vsprintf(buf, format, args);
8385 va_end(args);
84- if (use_color) {
85- fprintf(stderr, "%serror: %s%s\n", color.Red, buf, color.Normal);
86- } else {
87- fprintf(stderr, "error: %s\n", buf);
88- }
86+ fprintf(stderr, "%serror: %s%s\n", col_error, buf, col_normal);
8987}
9088
9189public fn void error_diag(const char* loc, const char* format @(printf_format), ...) {
@@ -94,19 +92,11 @@ public fn void error_diag(const char* loc, const char* format @(printf_format),
9492 va_start(args, format);
9593 vsprintf(buf, format, args);
9694 va_end(args);
97- if (use_color) {
98- fprintf(stderr, "%s%s: error: %s%s\n", color.Red, loc, buf, color.Normal);
99- } else {
100- fprintf(stderr, "%s: error: %s\n", loc, buf);
101- }
95+ fprintf(stderr, "%s%s: error: %s%s\n", col_error, loc, buf, col_normal);
10296}
10397
10498public fn void log_time(const char* item, u64 duration) {
10599 if (!show_timing) return;
106- if (use_color) {
107- printf("%s%s took %d usec%s\n", color.Blue, item, duration, color.Normal);
108- } else {
109- printf("%s took %d usec\n", item, duration);
110- }
100+ printf("%s%s took %d usec%s\n", col_timing, item, duration, col_normal);
111101}
112102
0 commit comments