Implemented a basic logger

This commit is contained in:
Eric-Paul Ickhorn 2023-11-28 21:25:53 +01:00
parent b058cbe288
commit 20f1d2b5ac
2 changed files with 150 additions and 0 deletions

105
code/inc/logger.h Normal file
View File

@ -0,0 +1,105 @@
#ifndef PARCEL_LOGGER_H
#define PARCEL_LOGGER_H
#include <utility.h>
typedef enum
{
PAC_SYNTAX_ERROR,
PAC_NAMING_ERROR,
PAC_INTERNAL_ERROR
} pac_error_e;
typedef enum
{
PAC_SYNTAX_ERROR_UNSPECIFIED,
PAC_SYNTAX_ERROR_STRAY,
PAC_SYNTAX_ERROR_MISSING_TOKEN,
PAC_SYNTAX_ERROR_ODD_TOKEN
} pac_syntax_error_e;
typedef enum
{
PAC_RESTRICTED_ERROR_INVALID_RULE_NAME
} pac_naming_error_e;
typedef struct pac_syntax_error
{
usz_t line;
usz_t column;
pac_syntax_error_e type;
union pac_syntax_error_specifics
{
struct pac_syntax_error_stray
{
rune_t sign;
} stray;
struct pac_syntax_error_missing_token
{
char *wanted_token;
char *hint;
} missing_token;
struct pac_syntax_error_odd_token
{
usz_t num_valid_options;
char *valid_options[16];
char *found_token;
char *hint;
} odd_token;
} specifics;
} pac_syntax_error_s;
typedef struct pac_naming_error
{
usz_t line;
usz_t column;
pac_naming_error_e type;
union pac_naming_error_specifics
{
struct pac_naming_error_invalid_rule_name
{
char *given_rule_name;
} invalid_rule_name;
} specifics;
} pac_naming_error_s;
typedef struct pac_error
{
pac_error_e type;
union pac_error_data
{
pac_syntax_error_s syntax_error;
pac_naming_error_s naming_error;
} specifics;
} pac_error_s;
typedef struct pac_logger
{
usz_t allocated_errors;
usz_t num_errors;
pac_error_s *errors;
pac_arena_s string_arena;
} pac_logger_s;
pac_logger_s pac_create_logger ();
void pac_log_syntax_error (pac_logger_s *logger, pac_syntax_error_s error);
void pac_log_naming_error (pac_logger_s *logger, pac_naming_error_s error);
void * pac_log_alloc (pac_logger_s *logger, usz_t num_bytes);
#endif // PARCEL_LOGGER_H

45
code/src/logger.c Normal file
View File

@ -0,0 +1,45 @@
#include <logger.h>
pac_logger_s pac_create_logger()
{
pac_logger_s logger;
logger.allocated_errors = 1024;
logger.num_errors = 0;
logger.errors = calloc(sizeof(pac_error_s), logger.allocated_errors);
logger.string_arena = pac_create_arena(32768);
return logger;
}
void pac_resize_log_if_needed(pac_logger_s *logger)
{
if(logger->num_errors >= logger->allocated_errors)
{
logger->allocated_errors *= 2;
logger->errors =
realloc(logger->errors, sizeof(pac_error_s) * logger->allocated_errors);
}
}
void pac_log_syntax_error(pac_logger_s *logger, pac_syntax_error_s error)
{
pac_resize_log_if_needed(logger);
logger->errors[logger->num_errors].type = PAC_SYNTAX_ERROR;
logger->errors[logger->num_errors].specifics.syntax_error = error;
++logger->num_errors;
}
void pac_log_naming_error(pac_logger_s *logger, pac_naming_error_s error)
{
pac_resize_log_if_needed(logger);
logger->errors[logger->num_errors].type = PAC_NAMING_ERROR;
logger->errors[logger->num_errors].specifics.naming_error = error;
++logger->num_errors;
}
void * pac_log_alloc(pac_logger_s *logger, usz_t num_bytes)
{
return ac_arena_alloc(logger->string_arena, num_bytes);
}