From 18a66011efb92304418622bf912e42aeb8ec68b4 Mon Sep 17 00:00:00 2001 From: Eric-Paul Ickhorn Date: Wed, 13 Sep 2023 19:21:33 +0200 Subject: [PATCH] Added Logger --- inc/logging.h | 47 +++++++++++++++++++++++++++++++++++++++++++++++ inc/utility.h | 36 ++++++++++++++++++++++++++++++++++++ src/logging.c | 37 +++++++++++++++++++++++++++++++++++++ src/utility.c | 11 +++++++++++ 4 files changed, 131 insertions(+) create mode 100644 inc/logging.h create mode 100644 inc/utility.h create mode 100644 src/logging.c create mode 100644 src/utility.c diff --git a/inc/logging.h b/inc/logging.h new file mode 100644 index 0000000..663bb5e --- /dev/null +++ b/inc/logging.h @@ -0,0 +1,47 @@ + +#ifndef CARROT_LOGGING_H +#define CARROT_LOGGING_H + +#include + +typedef enum +{ + CARROT_PARSER_ERROR_SEVERITY_NOTE, + CARROT_PARSER_ERROR_SEVERITY_WARNING, + CARROT_PARSER_ERROR_SEVERITY_ERROR, + CARROT_PARSER_ERROR_SEVERITY_UNRECOVERABLE_ERROR + +} carrot_error_severity_e; + +typedef enum +{ + CARROT_PARSER_LOG_ITEM, + CARROT_SYNTAX_LOG_ITEM + +} carrot_log_item_type_e; + +typedef struct carrot_log carrot_log_s; +typedef struct carrot_log_item carrot_log_item_s; +typedef struct carrot_log_message carrot_log_message_s; + +struct carrot_log_message +{ + carrot_error_severity_e severity; + cstr_s text; +}; + +struct carrot_log +{ + u32_t capacity; + u32_t length; + carrot_log_message_s *items; +}; + +carrot_log_s carrot_create_log (); + +void carrot_issue_note (carrot_log_s *log, char *message); +void carrot_issue_warning (carrot_log_s *log, char *message); +void carrot_issue_error (carrot_log_s *log, char *message); +void carrot_issue_unrecoverable_error (carrot_log_s *log, char *message); + +#endif // CARROT_LOGGING_H diff --git a/inc/utility.h b/inc/utility.h new file mode 100644 index 0000000..fd4169e --- /dev/null +++ b/inc/utility.h @@ -0,0 +1,36 @@ + +#ifndef CARROT_UTILITY_H +#define CARROT_UTILITY_H + +#define FALSE ((bool_t) 0) +#define TRUE ((bool_t) 1) + +#define NULL ((void *) 0) + + +typedef signed char s8_t; +typedef signed short s16_t; +typedef signed int s32_t; +typedef signed long s64_t; + +typedef unsigned char u8_t; +typedef unsigned short u16_t; +typedef unsigned int u32_t; +typedef unsigned long u64_t; + +typedef u8_t bool_t; +typedef struct str_s +{ + u32_t len_text; + char *text; +} cstr_s; + +// Not implemented +cstr_s cstr_join (cstr_s string1, cstr_s string2); + +// Not implemented +bool_t cstr_split (cstr_s string, u32_t middle, cstr_s *first, cstr_s *second); + +cstr_s carrot_wrap_chars (char *chars) + +#endif diff --git a/src/logging.c b/src/logging.c new file mode 100644 index 0000000..d653ef6 --- /dev/null +++ b/src/logging.c @@ -0,0 +1,37 @@ +#include + + +carrot_log_s carrot_create_log() +{ + carrot_log_s logger; + logger.capacity = 8192; + logger.length = 0; + logger.items = calloc(sizeof(carrot_log_message_s), logger.capacity); + return logger; +} + + +void carrot_issue_note(carrot_log_s *log, char *message) +{ + log->items[log->length].text = carrot_wrap_chars(message); + log->items[log->length].severity = CARROT_PARSER_ERROR_SEVERITY_NOTE; +} + +void carrot_issue_warning(carrot_log_s *log, char *message) +{ + log->items[log->length].text = carrot_wrap_chars(message); + log->items[log->length].severity = CARROT_PARSER_ERROR_SEVERITY_WARNING; +} + +void carrot_issue_error(carrot_log_s *log, char *message) +{ + log->items[log->length].text = carrot_wrap_chars(message); + log->items[log->length].severity = CARROT_PARSER_ERROR_SEVERITY_ERROR; +} + +void carrot_issue_unrecoverable_error(carrot_log_s *log, char *message) +{ + log->items[log->length].text = carrot_wrap_chars(message); + log->items[log->length].severity = CARROT_PARSER_ERROR_SEVERITY_UNRECOVERABLE_ERROR; +} + diff --git a/src/utility.c b/src/utility.c new file mode 100644 index 0000000..6755ec7 --- /dev/null +++ b/src/utility.c @@ -0,0 +1,11 @@ +#include + +cstr_s carrot_wrap_chars(char *chars) +{ + cstr_s result; + result.len_text = strlen(chars); + result.text = malloc(result.len_text); + memcpy(result.text, chars, result.len_text); + + return result; +}