Better logging in tokenstream
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdarg.h>
|
||||
|
||||
static LogError* s_logError = NULL;
|
||||
|
||||
@@ -17,17 +18,24 @@ void log_error(const char* msg) {
|
||||
}
|
||||
}
|
||||
|
||||
void log_on_line(const char* filename, const char* line_text, int line, int from, int to, const char* msg) {
|
||||
void log_on_line(const char* filename, const char* line_text, int line, int from, int to, const char* msg, ...) {
|
||||
char line_prefix[32];
|
||||
int prefix_len = snprintf(line_prefix, sizeof(line_prefix), "%d| ", line);
|
||||
|
||||
int caret_len = to - from + 1;
|
||||
if (caret_len < 1) caret_len = 1;
|
||||
|
||||
// Format the message
|
||||
va_list args;
|
||||
va_start(args, msg);
|
||||
char formatted_msg[256];
|
||||
vsnprintf(formatted_msg, sizeof(formatted_msg), msg, args);
|
||||
va_end(args);
|
||||
|
||||
size_t total_size = strlen(filename) + 16 + // --- filename ---
|
||||
prefix_len + strlen(line_text) + 2 + // line| text\n
|
||||
prefix_len + from - 1 + caret_len + 2 + // indent + ^^\n
|
||||
prefix_len + strlen(msg) + 2 + // indent + msg\n
|
||||
prefix_len + strlen(formatted_msg) + 2 + // indent + msg\n
|
||||
1;
|
||||
|
||||
char* buffer = (char*)malloc(total_size);
|
||||
@@ -44,7 +52,7 @@ void log_on_line(const char* filename, const char* line_text, int line, int from
|
||||
|
||||
// Message line
|
||||
for (int i = 0; i < prefix_len; i++) *p++ = ' ';
|
||||
p += sprintf(p, "%s\n", msg);
|
||||
p += sprintf(p, "%s\n", formatted_msg);
|
||||
|
||||
*p = '\0';
|
||||
|
||||
|
||||
Reference in New Issue
Block a user