Add log framework
This commit is contained in:
+1
-1
@@ -1,4 +1,4 @@
|
|||||||
V0_SRC := v0/main.c v0/token.c v0/parser.c
|
V0_SRC := v0/main.c v0/token.c v0/parser.c v0/log.c
|
||||||
|
|
||||||
# V0_TEST must only include `v0/test.c` itself, as all other test C–source files are
|
# V0_TEST must only include `v0/test.c` itself, as all other test C–source files are
|
||||||
# included directly into `v0/test.c` using `#include "test_xyz.c"`.
|
# included directly into `v0/test.c` using `#include "test_xyz.c"`.
|
||||||
|
|||||||
@@ -0,0 +1,16 @@
|
|||||||
|
#include "log.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
|
||||||
|
static LogError* s_logError = NULL;
|
||||||
|
|
||||||
|
void log_set_output(LogError* destination) {
|
||||||
|
s_logError = destination;
|
||||||
|
}
|
||||||
|
|
||||||
|
void log_error(const char* msg) {
|
||||||
|
if (s_logError != NULL) {
|
||||||
|
s_logError(msg);
|
||||||
|
} else {
|
||||||
|
fprintf(stderr, "Error: %s\n", msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
/**
|
||||||
|
* Contains the logging framework used for logging errors during compilation.
|
||||||
|
*/
|
||||||
|
#ifndef LOG_H
|
||||||
|
#define LOG_H
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A method that can log an error.
|
||||||
|
*/
|
||||||
|
typedef void LogError(const char* msg);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sets the destination for log errors.
|
||||||
|
*/
|
||||||
|
void log_set_output(LogError* destination);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Logs an error to the destination.
|
||||||
|
*/
|
||||||
|
void log_error(const char* msg);
|
||||||
|
|
||||||
|
#endif
|
||||||
@@ -30,6 +30,7 @@ typedef struct {
|
|||||||
|
|
||||||
#include "test_token.c"
|
#include "test_token.c"
|
||||||
#include "test_parser.c"
|
#include "test_parser.c"
|
||||||
|
#include "test_log.c"
|
||||||
|
|
||||||
static int s_totalTests;
|
static int s_totalTests;
|
||||||
static int s_greenTests;
|
static int s_greenTests;
|
||||||
@@ -44,6 +45,7 @@ static TestCase s_tests[] = {
|
|||||||
{"tokenstream_void_function_signature", test_tokenstream_void_function_signature},
|
{"tokenstream_void_function_signature", test_tokenstream_void_function_signature},
|
||||||
{"tokenstream_info", test_tokenstream_info},
|
{"tokenstream_info", test_tokenstream_info},
|
||||||
{"parser_module_name", test_parser_module_name},
|
{"parser_module_name", test_parser_module_name},
|
||||||
|
{"log_error", test_log_error},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,21 @@
|
|||||||
|
#include "test.h"
|
||||||
|
#include "log.h"
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
static char s_lastLoggedError[256];
|
||||||
|
|
||||||
|
static void mock_log(const char* msg) {
|
||||||
|
strncpy(s_lastLoggedError, msg, sizeof(s_lastLoggedError) - 1);
|
||||||
|
s_lastLoggedError[sizeof(s_lastLoggedError) - 1] = '\0';
|
||||||
|
}
|
||||||
|
|
||||||
|
static void test_log_error(void) {
|
||||||
|
log_set_output(mock_log);
|
||||||
|
|
||||||
|
memset(s_lastLoggedError, 0, sizeof(s_lastLoggedError));
|
||||||
|
log_error("test error message");
|
||||||
|
|
||||||
|
assert_str("test error message", s_lastLoggedError, "expected 'test error message'");
|
||||||
|
|
||||||
|
log_set_output(NULL); // Reset to default
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user