(FEA) Featured for Location_Literalisation, Status_Literalisation etc.
This commit is contained in:
@@ -3,16 +3,15 @@
|
||||
|
||||
# include <stdbool.h>
|
||||
# include <stdint.h>
|
||||
# include <stdio.h>
|
||||
# include <stdlib.h>
|
||||
# include <string.h>
|
||||
# include <threads.h>
|
||||
# include <time.h>
|
||||
# include <stdio.h>
|
||||
# include <math.h>
|
||||
|
||||
# include <Compound/common.h>
|
||||
# include <Compound/utils.h>
|
||||
# include <Compound/platform.h>
|
||||
# include <Compound/utils.h>
|
||||
|
||||
/* Status characteristics */
|
||||
typedef enum {
|
||||
@@ -39,14 +38,21 @@ typedef struct {
|
||||
} Location;
|
||||
|
||||
# define __HERE__ (Location){ \
|
||||
.file = (char *)__FILE__, \
|
||||
.line = __LINE__, \
|
||||
.func = (char *)__func__ \
|
||||
.file = (char *)__FILE__, \
|
||||
.line = __LINE__, \
|
||||
.func = (char *)__func__ \
|
||||
}
|
||||
|
||||
# define __GLOBAL__ (Location){ \
|
||||
.file = (char *)__FILE__, \
|
||||
.line = __LINE__, \
|
||||
.func = (char *)"(GLOBAL)" \
|
||||
}
|
||||
|
||||
/* Common return type for reporting functions that require to give more
|
||||
information about the procedure. */
|
||||
typedef struct _Status {
|
||||
char *identity;
|
||||
int value; /* Traditional returning data "int". Only used when the function
|
||||
called and received legacy functions that uses "int" as the
|
||||
returning type that wish to have place to hold the value.
|
||||
@@ -65,17 +71,30 @@ typedef struct _Status {
|
||||
|
||||
*/
|
||||
|
||||
# ifdef __COMPOUND_32__
|
||||
# define STATUS_LITERALISE_LENGTH(stat) \
|
||||
(utils_calc_digits(stat.value) + strlen(stat.description) + 2 + \
|
||||
utils_calc_digits(INT32_DIGITS_DEC))
|
||||
# elif defined __COMPOUND_64__
|
||||
# define STATUS_LITERALISE_LENGTH(stat) \
|
||||
(utils_calc_digits(stat.value) + strlen(stat.description) + 2 + \
|
||||
utils_calc_digits(INT64_DIGITS_DEC))
|
||||
# endif
|
||||
/* line, func, file */
|
||||
// # define LOCATION_LITERALISE_FORMAT "at line %d, in %s, %s"
|
||||
|
||||
# define STATUS_LITERALISE_FORMAT "%d \"%s\" %d %p"
|
||||
/* file, line, func */
|
||||
# define LOCATION_LITERALISE_FORMAT "at %s:%d, in function `%s\'"
|
||||
# define LOCATION_LITERALISE_FORMAT_LENGTH 20
|
||||
|
||||
/* value, description, characteristic, prev */
|
||||
// # define STATUS_LITERALISE_FORMAT "%d \"%s\" %d %p"
|
||||
/* identity, prev->identity, value, characteristic, description, <loc> */
|
||||
// # define STATUS_LITERALISE_FORMAT "%s (prev: %s): $=%d @=%d\n\t\"%s\"\n\t%s"
|
||||
|
||||
// MaximumLiteralisationLengthExceeded (prev: MaximumLengthExceeded): $=1 @=1
|
||||
|
||||
/*
|
||||
MaximumLengthExceeded: "Buffer was too long."
|
||||
predecessor=<ArrayLengthError> value=(1) characteristic=[1]
|
||||
at line 40, in Main, /home/william/Documents/Projects/Compound/test.c
|
||||
|
||||
*/
|
||||
|
||||
// identity, description, prev->identity, value, characteristic, <loc>
|
||||
# define STATUS_LITERALISE_FORMAT \
|
||||
"%s: \"%s\"\n\tpredecessor=<%s> value=(%d) characteristic=[%d]\n\t%s\n"
|
||||
|
||||
typedef enum {
|
||||
REPORT_SENDING_PRIORITY_ALL = 0, // Highest level; least value.
|
||||
@@ -130,20 +149,6 @@ Fri 10 May 03:02:37 CST 2024 [EXCEPTIONAL] InvalidParameter (Nullity): Given buf
|
||||
# define REPORT_LITERALISE_CHAINS_FORMAT " at %s:%d, %s"
|
||||
# define REPORT_LITERALISE_CHAINS_EXCLAIM_FORMAT "!!!!at %s:%d, %s"
|
||||
|
||||
// # define REPORT_LITERALISE_HEADER_FORMAT_LENGTH(buff, PRIORITY, STATUSNAME, \
|
||||
// ORIGINATOR, DESCRIPTION) \
|
||||
// { \
|
||||
// const time_t now = time(NULL); \
|
||||
// (void)strflen(buff, 28, DATETIME_FORMAT, localtime(&now)); \
|
||||
// *length = strlen(buff); \
|
||||
// }
|
||||
|
||||
// # define REPORT_LITERALISE_CHAINS_FORMAT_LENGTH(FILEPATH, LINE, FUNCNAME) \
|
||||
// (strlen(FILEPATH) + utils_calc_digits(LINE) + \
|
||||
// strlen(FUNCNAME) + 10) // Does not count '\0'
|
||||
// # define REPORT_LITERALISE_CHAINS_EXCLAIM_FORMAT_LENGTH \
|
||||
// REPORT_LITERALISE_CHAINS_FORMAT_LENGTH
|
||||
|
||||
typedef enum {
|
||||
REPORT_SENDER_RESULT_FINISHED,
|
||||
REPORT_SENDER_RESULT_PROGRESSING,
|
||||
@@ -200,29 +205,26 @@ typedef struct {
|
||||
|
||||
# define STATUS_BUFFER_MAXIMUM_LENGTH UINT32_MAX
|
||||
|
||||
Status Location_Literalise(Location *inst, char *buff);
|
||||
bool Location_Equals(Location lc1, Location lc2);
|
||||
Status Status_Literalise(Status *inst, char *buff);
|
||||
bool Status_Equals(Status stat1, Status stat2);
|
||||
bool Status_Equal(Status *stat1, Status *stat2);
|
||||
void StatusUtils_Dump(Status *inst, Status *store, int idx);
|
||||
bool StatusUtils_HasPrev(Status *inst);
|
||||
bool StatusUtils_HasPrev(Status inst);
|
||||
bool StatusUtils_IsOkay(Status inst);
|
||||
bool StatusUtils_IsValid(Status inst);
|
||||
bool StatusUtils_IsRecursive(Status inst);
|
||||
int StatusUtils_Depth(Status *inst);
|
||||
|
||||
Status
|
||||
Report_Create(Report *inst, Status *stat, FILE *dest, char *initiator,
|
||||
int priority);
|
||||
bool
|
||||
Report_Equals(Report repo1, Report repo2);
|
||||
Status
|
||||
Report_Literalise(Report *inst, char *buff);
|
||||
Status Report_Create(Report *inst, Status *stat, FILE *dest, char *initiator,
|
||||
int priority);
|
||||
Status Report_CopyOf(Report *inst, Report *other);
|
||||
Status Report_Literalise(Report *inst, char *buff);
|
||||
void Report_Delete(Report *inst);
|
||||
bool Report_Equals(Report repo1, Report repo2);
|
||||
|
||||
|
||||
Status
|
||||
ReportSender_Create(ReportSender *inst, Report *report);
|
||||
Status
|
||||
ReportSender_Send(ReportSender *inst, ReportSendingTask task);
|
||||
Status ReportSender_Create(ReportSender *inst, Report *report);
|
||||
Status ReportSender_Send(ReportSender *inst, ReportSendingTask task);
|
||||
// ReportSendingTaskStatus
|
||||
// ReportSender_GetStatus(ReportSender *inst);
|
||||
|
||||
@@ -254,177 +256,309 @@ Status ReportSenderManager_RemoveTask(ReportSendingManager *inst,
|
||||
|
||||
// ---------------------ELEMENTARY-------------------------
|
||||
|
||||
static Status UnknownStatus = {
|
||||
/*
|
||||
typedef struct _Status {
|
||||
char *identity;
|
||||
int value;
|
||||
char *description;
|
||||
int characteristic;
|
||||
Location loc;
|
||||
struct _Status *prev;
|
||||
} Status;
|
||||
*/
|
||||
|
||||
static const Status UnknownStatus = {
|
||||
.identity = nameof(UnknownStatus),
|
||||
.value = -1,
|
||||
.description = "An unknown status.",
|
||||
.characteristic = STATUS_UNKNOWN,
|
||||
.loc = __GLOBAL__,
|
||||
.prev = NULL
|
||||
};
|
||||
|
||||
static Status NormalStatus = {
|
||||
static const Status NormalStatus = {
|
||||
.identity = nameof(NormalStatus),
|
||||
.value = 0,
|
||||
.description = "A normal status.",
|
||||
.characteristic = STATUS_NORMAL,
|
||||
.loc = __GLOBAL__,
|
||||
.prev = NULL
|
||||
};
|
||||
|
||||
static Status ErrorStatus = {
|
||||
static const Status ErrorStatus = {
|
||||
.identity = nameof(ErrorStatus),
|
||||
.value = 1,
|
||||
.description = "An error status.",
|
||||
.characteristic = STATUS_ERROR,
|
||||
.loc = __GLOBAL__,
|
||||
.prev = NULL
|
||||
};
|
||||
|
||||
// ----------------------EXTENDED--------------------------
|
||||
|
||||
static Status MemoryViolation = {
|
||||
static const Status MemoryViolation = (Status){
|
||||
.identity = nameof(MemoryViolation),
|
||||
.value = 1,
|
||||
.description = "Illegal access on certain memory address.",
|
||||
.characteristic = STATUS_ERROR,
|
||||
.prev = &ErrorStatus
|
||||
.loc = __GLOBAL__,
|
||||
.prev = (Status *)&ErrorStatus
|
||||
};
|
||||
|
||||
static Status NullPointerAccounted = {
|
||||
static const Status NullPointerAccounted = (Status){
|
||||
.identity = nameof(NullPointerAccounted),
|
||||
.value = 1,
|
||||
.description = "An involving null pointer was not accepted.",
|
||||
.characteristic = STATUS_ERROR,
|
||||
.prev = &MemoryViolation
|
||||
.loc = __GLOBAL__,
|
||||
.prev = (Status *)&MemoryViolation
|
||||
};
|
||||
|
||||
static Status InvalidObject = {
|
||||
static const Status InvalidObject = (Status){
|
||||
.identity = nameof(InvalidObject),
|
||||
.value = 1,
|
||||
.description = "An invalid object was presented.",
|
||||
.characteristic = STATUS_ERROR,
|
||||
.prev = &ErrorStatus
|
||||
.loc = __GLOBAL__,
|
||||
.prev = (Status *)&ErrorStatus
|
||||
};
|
||||
|
||||
static Status UnavailableObject = {
|
||||
static const Status UnavailableObject = (Status){
|
||||
.identity = nameof(UnavailableObject),
|
||||
.value = 1,
|
||||
.description = "An unavailable object was presented.",
|
||||
.characteristic = STATUS_ERROR,
|
||||
.prev = &ErrorStatus
|
||||
.loc = __GLOBAL__,
|
||||
.prev = (Status *)&ErrorStatus
|
||||
};
|
||||
|
||||
static Status InvalidParameter = {
|
||||
static const Status InstanceStillAlive = (Status){
|
||||
.identity = nameof(InstanceStillAlive),
|
||||
.value = 1,
|
||||
.description = "Given instance was yet alive.",
|
||||
.characteristic = STATUS_ERROR,
|
||||
.loc = __GLOBAL__,
|
||||
.prev = (Status *)&ErrorStatus
|
||||
};
|
||||
|
||||
static const Status InstanceNotAlive = (Status){
|
||||
.identity = nameof(InstanceNotAlive),
|
||||
.value = 1,
|
||||
.description = "Given instance for reallocation was not alive.",
|
||||
.characteristic = STATUS_ERROR,
|
||||
.loc = __GLOBAL__,
|
||||
.prev = (Status *)&ErrorStatus
|
||||
};
|
||||
|
||||
static const Status InvalidParameter = (Status){
|
||||
.identity = nameof(InvalidParameter),
|
||||
.value = 1,
|
||||
.description = "An invalid parameter was presented.",
|
||||
.characteristic = STATUS_ERROR,
|
||||
.prev = &InvalidObject
|
||||
.loc = __GLOBAL__,
|
||||
.prev = (Status *)&InvalidObject
|
||||
};
|
||||
|
||||
static Status InsufficientMemory = {
|
||||
static const Status InsufficientMemory = (Status){
|
||||
.identity = nameof(InsufficientMemory),
|
||||
.value = 1,
|
||||
.description = "Not enough room for further memory allocations.",
|
||||
.characteristic = STATUS_ERROR,
|
||||
.prev = &ErrorStatus
|
||||
.loc = __GLOBAL__,
|
||||
.prev = (Status *)&ErrorStatus
|
||||
};
|
||||
|
||||
static Status ArithmeticError = {
|
||||
static const Status ArithmeticError = (Status){
|
||||
.identity = nameof(ArithmeticError),
|
||||
.value = 1,
|
||||
.description = "An arithmetic error occurred.",
|
||||
.characteristic = STATUS_ERROR,
|
||||
.prev = &ErrorStatus
|
||||
.loc = __GLOBAL__,
|
||||
.prev = (Status *)&ErrorStatus
|
||||
};
|
||||
|
||||
static Status RuntimeError = {
|
||||
static const Status IntegerOverFlow = (Status){
|
||||
.identity = nameof(IntegerOverFlow),
|
||||
.value = 1,
|
||||
.description = "An integer had overflowed.",
|
||||
.characteristic = STATUS_ERROR,
|
||||
.loc = __GLOBAL__,
|
||||
.prev = (Status *)&ArithmeticError
|
||||
};
|
||||
|
||||
static const Status RuntimeError = (Status){
|
||||
.identity = nameof(RuntimeError),
|
||||
.value = 1,
|
||||
.description = "A runtime error occurred.",
|
||||
.characteristic = STATUS_ERROR,
|
||||
.prev = &ErrorStatus
|
||||
.loc = __GLOBAL__,
|
||||
.prev = (Status *)&ErrorStatus
|
||||
};
|
||||
|
||||
static Status ArrayLengthError = {
|
||||
static const Status ArrayLengthError = (Status){
|
||||
.identity = nameof(ArrayLengthError),
|
||||
.value = 1,
|
||||
.description = "Given array length does not meet the requirement.",
|
||||
.characteristic = STATUS_ERROR,
|
||||
.prev = &ErrorStatus
|
||||
.loc = __GLOBAL__,
|
||||
.prev = (Status *)&ErrorStatus
|
||||
};
|
||||
|
||||
static Status VariableFormatMismatch = {
|
||||
static const Status VariableFormatMismatch = (Status){
|
||||
.identity = nameof(VariableFormatMismatch),
|
||||
.value = 1,
|
||||
.description = "Given format does not match with given subjects.",
|
||||
.characteristic = STATUS_ERROR,
|
||||
.prev = &ErrorStatus
|
||||
.loc = __GLOBAL__,
|
||||
.prev = (Status *)&ErrorStatus
|
||||
};
|
||||
|
||||
static Status ImprecisionError = {
|
||||
static const Status ImprecisionError = (Status){
|
||||
.identity = nameof(ImprecisionError),
|
||||
.value = 1,
|
||||
.description = "Precision was not enough for handling the calculation.",
|
||||
.characteristic = STATUS_ERROR,
|
||||
.prev = &RuntimeError
|
||||
.loc = __GLOBAL__,
|
||||
.prev = (Status *)&RuntimeError
|
||||
};
|
||||
|
||||
// ---------------------USER DEFINED-----------------------
|
||||
|
||||
static Status UnavailableInstance = {
|
||||
static const Status UnavailableInstance = (Status){
|
||||
.identity = nameof(UnavailableInstance),
|
||||
.value = 1,
|
||||
.description = "An unavailable instance was given for initialisation.",
|
||||
.characteristic = STATUS_ERROR,
|
||||
.prev = &NullPointerAccounted
|
||||
.loc = __GLOBAL__,
|
||||
.prev = (Status *)&NullPointerAccounted
|
||||
};
|
||||
|
||||
static Status UnavailableParameter = {
|
||||
static const Status RecreationOnInstanceStillAlive = (Status){
|
||||
.identity = nameof(RecreationOnInstanceStillAlive),
|
||||
.value = 1,
|
||||
.description = "Given instance was still alive, yet, was sent for another "
|
||||
"session of recreation.",
|
||||
.characteristic = STATUS_ERROR,
|
||||
.loc = __GLOBAL__,
|
||||
.prev = (Status *)&InstanceStillAlive\
|
||||
};
|
||||
|
||||
static const Status UnavailableParameter = (Status){
|
||||
.identity = nameof(UnavailableParameter),
|
||||
.value = 1,
|
||||
.description = "An unavailable instance was given as a parameter.",
|
||||
.characteristic = STATUS_ERROR,
|
||||
.prev = &UnavailableInstance
|
||||
.loc = __GLOBAL__,
|
||||
.prev = (Status *)&UnavailableInstance
|
||||
};
|
||||
|
||||
static Status InvalidReportTask = {
|
||||
static const Status InvalidReportTask = (Status){
|
||||
.identity = nameof(InvalidReportTask),
|
||||
.value = 1,
|
||||
.description = "An unavailable or illegal report task was given.",
|
||||
.characteristic = STATUS_ERROR,
|
||||
.prev = &InvalidParameter
|
||||
.loc = __GLOBAL__,
|
||||
.prev = (Status *)&InvalidParameter
|
||||
};
|
||||
|
||||
static Status UnableToThrowError = {
|
||||
static const Status UnableToThrowError = (Status){
|
||||
.identity = nameof(UnableToThrowError),
|
||||
.value = 1,
|
||||
.description = "Unable to report an exceptional situation.",
|
||||
.characteristic = STATUS_ERROR,
|
||||
.prev = &RuntimeError
|
||||
.loc = __GLOBAL__,
|
||||
.prev = (Status *)&RuntimeError
|
||||
};
|
||||
|
||||
static Status ReadWriteError = {
|
||||
static const Status ReadWriteError = (Status){
|
||||
.identity = nameof(ReadWriteError),
|
||||
.value = 1,
|
||||
.description = "Error occurred during IO session.",
|
||||
.characteristic = STATUS_ERROR,
|
||||
.prev = &RuntimeError
|
||||
.loc = __GLOBAL__,
|
||||
.prev = (Status *)&RuntimeError
|
||||
};
|
||||
|
||||
static Status FileNotFound = {
|
||||
static const Status FileNotFound = (Status){
|
||||
.identity = nameof(FileNotFound),
|
||||
.value = 1,
|
||||
.description = "Target file was unavailable and unable to find.",
|
||||
.characteristic = STATUS_ERROR,
|
||||
.prev = &ReadWriteError
|
||||
.loc = __GLOBAL__,
|
||||
.prev = (Status *)&ReadWriteError
|
||||
};
|
||||
|
||||
static Status InvalidFileName = {
|
||||
static const Status InvalidFileName = (Status){
|
||||
.identity = nameof(InvalidFileName),
|
||||
.value = 1,
|
||||
.description = "Given file name was invalid.",
|
||||
.characteristic = STATUS_ERROR,
|
||||
.prev = &ReadWriteError
|
||||
.loc = __GLOBAL__,
|
||||
.prev = (Status *)&ReadWriteError
|
||||
};
|
||||
|
||||
static Status UnavailableFileName = {
|
||||
static const Status UnavailableFileName = (Status){
|
||||
.identity = nameof(UnavailableFileName),
|
||||
.value = 1,
|
||||
.description = "Given file name was unavailable",
|
||||
.characteristic = STATUS_ERROR,
|
||||
.prev = &UnavailableObject
|
||||
.loc = __GLOBAL__,
|
||||
.prev = (Status *)&UnavailableObject
|
||||
};
|
||||
|
||||
static Status ReportThrown = {
|
||||
static const Status ReportThrown = (Status){
|
||||
.identity = nameof(ReportThrown),
|
||||
.value = 1,
|
||||
.description = "This function has thrown a report, "
|
||||
"following instructions aborted.",
|
||||
.characteristic = STATUS_ERROR,
|
||||
.prev = &RuntimeError
|
||||
.loc = __GLOBAL__,
|
||||
.prev = (Status *)&RuntimeError\
|
||||
};
|
||||
|
||||
static Status ReportMessageTooLong = {
|
||||
static const Status ReportMessageTooLong = (Status){
|
||||
.identity = nameof(ReportMessageTooLong),
|
||||
.value = 1,
|
||||
.description = "Given message is too long.",
|
||||
.characteristic = STATUS_ERROR,
|
||||
.prev = &ArrayLengthError
|
||||
.loc = __GLOBAL__,
|
||||
.prev = (Status *)&ArrayLengthError
|
||||
};
|
||||
|
||||
static Status MaximumLengthExceeded = {
|
||||
static const Status MaximumLengthExceeded = (Status){
|
||||
.identity = nameof(MaximumLengthExceeded),
|
||||
.value = 1,
|
||||
.description = "Buffer was too long.",
|
||||
.characteristic = STATUS_ERROR,
|
||||
.prev = &ArrayLengthError
|
||||
.loc = __GLOBAL__,
|
||||
.prev = (Status *)&ArrayLengthError
|
||||
};
|
||||
|
||||
static Status MaximumLiteralisationLengthExceeded = {
|
||||
static const Status MaximumLiteralisationLengthExceeded = (Status){
|
||||
.identity = nameof(MaximumLiteralisationLengthExceeded),
|
||||
.value = 1,
|
||||
.description = "Literalisation was too long.",
|
||||
.characteristic = STATUS_ERROR,
|
||||
.prev = &MaximumLengthExceeded
|
||||
.loc = __GLOBAL__,
|
||||
.prev = (Status *)&MaximumLengthExceeded
|
||||
};
|
||||
|
||||
static Status UnavailableBuffer = {
|
||||
static const Status UnavailableBuffer = (Status){
|
||||
.identity = nameof(UnavailableBuffer),
|
||||
.value = 1,
|
||||
.description = "Given buffer was unavailable.",
|
||||
.characteristic = STATUS_ERROR,
|
||||
.prev = &UnavailableInstance
|
||||
.loc = __GLOBAL__,
|
||||
.prev = (Status *)&UnavailableInstance
|
||||
};
|
||||
|
||||
static Status InvalidLiteralisingBuffer = {
|
||||
static const Status InvalidLiteralisingBuffer = (Status){
|
||||
.identity = nameof(InvalidLiteralisingBuffer),
|
||||
.value = 1,
|
||||
.description = "Given buffer does not have a good integrity on its length.",
|
||||
.characteristic = STATUS_ERROR,
|
||||
.prev = &InvalidObject
|
||||
.loc = __GLOBAL__,
|
||||
.prev = (Status *)&InvalidObject
|
||||
};
|
||||
|
||||
// ========================================================
|
||||
|
@@ -2,69 +2,89 @@
|
||||
#include <Compound/status.h>
|
||||
#include <Compound/utils.h>
|
||||
|
||||
bool Location_Equal(Location lc1, Location lc2)
|
||||
/*
|
||||
typedef struct {
|
||||
char *file;
|
||||
int line;
|
||||
char *func;
|
||||
} Location;
|
||||
*/
|
||||
Status Location_Literalise(Location *inst, char *buff)
|
||||
{
|
||||
return ((!strcmp(lc1.file, lc2.file)) && (lc1.line == lc2.line) &&
|
||||
(!strcmp(lc1.func, lc2.func)));
|
||||
fails(inst, UnavailableInstance);
|
||||
fails(buff, UnavailableBuffer);
|
||||
|
||||
/* Literalise line. */
|
||||
char line_buff[LITERALISATION_LENGTH_MAXIMUM];
|
||||
Utils_LiteraliseInteger(inst->line, line_buff);
|
||||
|
||||
/* Concatenate every buff. */
|
||||
const long total_len = strlen(inst->file) + strlen(line_buff)
|
||||
+ strlen(inst->func)
|
||||
+ LOCATION_LITERALISE_FORMAT_LENGTH;
|
||||
state(total_len > LITERALISATION_LENGTH_MAXIMUM,
|
||||
MaximumLiteralisationLengthExceeded);
|
||||
|
||||
/* Copy and assign. */
|
||||
return (Status) {
|
||||
.value = !sprintf(buff, LOCATION_LITERALISE_FORMAT,
|
||||
inst->file, inst->line, inst->func),
|
||||
.description = NormalStatus.description,
|
||||
.characteristic = NormalStatus.characteristic,
|
||||
.loc = __HERE__,
|
||||
.prev = NormalStatus.prev
|
||||
};
|
||||
}
|
||||
|
||||
bool Status_Equals(Status stat1, Status stat2)
|
||||
bool Location_Equal(Location *lc1, Location *lc2)
|
||||
{
|
||||
state(lc1 == NULL || lc2 == NULL, false);
|
||||
|
||||
/* Skip when both stat1 and stat2 are empty. */
|
||||
state((stat1.value == 0 && stat2.value == 0 && stat1.description == 0x0 &&
|
||||
stat2.description == 0x0 && stat1.characteristic == 0 &&
|
||||
stat2.characteristic == 0 && stat1.prev == 0x0 && stat2.prev == 0x0),
|
||||
true);
|
||||
return ((!strcmp(lc1->file, lc2->file)) && (lc1->line == lc2->line) &&
|
||||
(!strcmp(lc1->func, lc2->func)));
|
||||
}
|
||||
|
||||
/* True for equality; False for inequality. */
|
||||
return ((stat1.value == stat2.value) &&
|
||||
(!strcmp(stat1.description, stat2.description)) &&
|
||||
(stat1.characteristic == stat2.characteristic) &&
|
||||
(Status_Equals(*stat1.prev, *stat2.prev)));
|
||||
bool Status_Equal(Status *stat1, Status *stat2)
|
||||
{
|
||||
state(stat1 == NULL || stat2 == NULL, false);
|
||||
|
||||
return (
|
||||
stat1->value == stat2->value &&
|
||||
!strcmp(stat1->description, stat2->description) &&
|
||||
stat1->characteristic == stat2->characteristic &&
|
||||
Location_Equal(&stat1->loc, &stat2->loc) &&
|
||||
((StatusUtils_HasPrev(*stat1) && StatusUtils_HasPrev(*stat2))
|
||||
? Status_Equal(stat1->prev, stat2->prev)
|
||||
: true)
|
||||
);
|
||||
}
|
||||
|
||||
Status Status_Literalise(Status *inst, char *buff)
|
||||
{
|
||||
/* Skip unavailable or invalid parameters. */
|
||||
/* Skip unavailable instance and invalid parameter. */
|
||||
fails(inst, UnavailableInstance);
|
||||
fails(buff, UnavailableBuffer);
|
||||
// state(strlen(buff) != LITERALISATION_LENGTH_MAXIMUM,
|
||||
// InvalidLiteralisingBuffer);
|
||||
|
||||
/* Set up idx for counting final literalisation length to ensure the
|
||||
string copying is index access safe. */
|
||||
int idx = 0;
|
||||
const int status_dump_buffer_len = StatusUtils_Depth(inst);
|
||||
|
||||
Status status_dump_buffer[status_dump_buffer_len];
|
||||
|
||||
/* Literalise every status that flattened on status_dump_buffer. */
|
||||
for (register int i = 0; i < status_dump_buffer_len; i++) {
|
||||
char status_literalising_buffer[LITERALISATION_LENGTH_MAXIMUM];
|
||||
(void)Status_Literalise(&status_dump_buffer[i], status_literalising_buffer);
|
||||
|
||||
/* Append to buff. */
|
||||
/* Prevent buffer-out-of-bound access. */
|
||||
const int status_literalising_buffer_len = strlen(status_literalising_buffer);
|
||||
if (idx + status_literalising_buffer_len >= LITERALISATION_LENGTH_MAXIMUM) {
|
||||
buff = NULL;
|
||||
return MaximumLiteralisationLengthExceeded;
|
||||
}
|
||||
|
||||
idx += status_literalising_buffer_len;
|
||||
(void)strcat(buff, status_literalising_buffer);
|
||||
}
|
||||
/* Literalise loc. */
|
||||
char loc_buff[LITERALISATION_LENGTH_MAXIMUM];
|
||||
notok(Location_Literalise(&inst->loc, loc_buff), {
|
||||
return error(_, "Failed on literalising the \"loc\" of a status.");
|
||||
});
|
||||
|
||||
/* Concatenate every buffer. */
|
||||
state(!sprintf(buff, STATUS_LITERALISE_FORMAT,
|
||||
inst->identity, inst->description,
|
||||
(!inst->prev ? "(null)" : (inst->prev->identity)),
|
||||
inst->value, inst->characteristic, loc_buff),
|
||||
error(RuntimeError, "Returned 0 byte written on concatenating buffers "
|
||||
"during literalisation of a status using \"sprintf\"."));
|
||||
|
||||
return NormalStatus;
|
||||
}
|
||||
|
||||
bool StatusUtils_HasPrev(Status *stat)
|
||||
bool StatusUtils_HasPrev(Status stat)
|
||||
{
|
||||
/* Skip when stat is unavailable for accessing. */
|
||||
state(Status_Equals(*stat, (Status){}), false);
|
||||
|
||||
return (stat->prev != NULL);
|
||||
return (stat.prev != NULL);
|
||||
}
|
||||
|
||||
bool StatusUtils_IsOkay(Status stat)
|
||||
@@ -74,20 +94,20 @@ bool StatusUtils_IsOkay(Status stat)
|
||||
|
||||
bool StatusUtils_IsValid(Status stat)
|
||||
{
|
||||
return (!strcmp(stat.description, "") && stat.characteristic >= 0 &&
|
||||
stat.prev != NULL);
|
||||
return (!strcmp(stat.description, "") && stat.characteristic >= 0
|
||||
&& !stat.prev);
|
||||
}
|
||||
|
||||
bool StatusUtils_IsRecursive(Status stat)
|
||||
{
|
||||
return (stat.prev != NULL && stat.prev == &stat);
|
||||
return (stat.prev && stat.prev == &stat);
|
||||
}
|
||||
|
||||
void StatusUtils_Dump(Status *inst, Status *store, int idx)
|
||||
{
|
||||
|
||||
/* Skip when either stat or stat.prev is unavailable, or, idx is invalid. */
|
||||
solve((store == NULL || !StatusUtils_HasPrev(inst) || idx < 0), return;);
|
||||
solve((!store || !StatusUtils_HasPrev(*inst) || idx < 0), return;);
|
||||
|
||||
store[idx] = *inst;
|
||||
|
||||
@@ -97,17 +117,13 @@ void StatusUtils_Dump(Status *inst, Status *store, int idx)
|
||||
int StatusUtils_Depth(Status *stat)
|
||||
{
|
||||
/* Skip unavailable stat. */
|
||||
state((stat == NULL), -1);
|
||||
state((!stat || !stat->prev), -1);
|
||||
|
||||
Status *p = stat; // Include this layer of Status.
|
||||
int cnt = 1;
|
||||
while (p != NULL) {
|
||||
if (StatusUtils_IsRecursive(*p) || !StatusUtils_HasPrev(stat)) break;
|
||||
|
||||
p = p->prev;
|
||||
cnt += 1;
|
||||
}
|
||||
|
||||
register int cnt;
|
||||
for (cnt = 0; (!StatusUtils_IsRecursive(*p)
|
||||
&& StatusUtils_HasPrev(*p)); cnt++) p = p->prev;
|
||||
|
||||
return cnt;
|
||||
}
|
||||
|
||||
@@ -116,21 +132,51 @@ Status Report_Create(Report *inst, Status *stat, FILE *dest, char *initiator,
|
||||
{
|
||||
/* Skip unavailable parameters. */
|
||||
fails(inst, UnavailableInstance);
|
||||
fails(stat, error(InvalidParameter, "Given initiator was null."));
|
||||
fails(stat, error(InvalidParameter, "Given stat was null."));
|
||||
fails(initiator, error(InvalidParameter, "Given initiator was null."));
|
||||
state(priority < 0, error(InvalidParameter, "Given priority was negative."));
|
||||
|
||||
/* Copy and assign. */
|
||||
inst->status = *stat;
|
||||
inst->initiator = initiator;
|
||||
inst->initiator = calloc(strlen(initiator), sizeof(char));
|
||||
(void)strcpy(inst->initiator, initiator);
|
||||
inst->time = time(NULL);
|
||||
inst->priority = priority;
|
||||
inst->task_status = REPORT_SENDING_TASK_STATUS_PENDING;
|
||||
inst->dest = (dest == NULL ? stderr : dest);
|
||||
inst->dest = (dest == NULL ? stdout : dest);
|
||||
|
||||
return NormalStatus;
|
||||
}
|
||||
|
||||
Status Report_CopyOf(Report *inst, Report *other)
|
||||
{
|
||||
fails(inst, UnavailableInstance);
|
||||
fails(other, error(InvalidParameter, "Given report is unavailable."));
|
||||
|
||||
// Status status;
|
||||
// char *initiator;
|
||||
// time_t time;
|
||||
// ReportSendingPriority priority;
|
||||
// ReportSendingTaskStatus task_status;
|
||||
// FILE *dest;
|
||||
inst->status = other->status;
|
||||
|
||||
}
|
||||
|
||||
void Report_Delete(Report *inst)
|
||||
{
|
||||
svoid(inst);
|
||||
|
||||
free(inst->initiator);
|
||||
inst->initiator = NULL;
|
||||
inst->dest = NULL;
|
||||
inst->priority = 0;
|
||||
inst->status = (Status){};
|
||||
inst->task_status = REPORT_SENDING_TASK_STATUS_NOTFOUND;
|
||||
inst->time = 0;
|
||||
inst = NULL;
|
||||
}
|
||||
|
||||
Status Report_Literalise(Report *inst, char *buff)
|
||||
{
|
||||
fails(inst, UnavailableInstance);
|
||||
@@ -195,7 +241,9 @@ Status ReportSender_Create(ReportSender *inst, Report *report)
|
||||
fails(report, error(UnavailableParameter, "Given report was unavailable."));
|
||||
|
||||
thrd_create(&inst->thread, &HANDLER, report);
|
||||
*inst->report = *report;
|
||||
notok(Report_CopyOf(inst->report, report),
|
||||
return error(ErrorStatus, "Cannot copy to create new instance of report.");
|
||||
) // *inst->report = *report;
|
||||
inst->elapsed = 0;
|
||||
inst->result = REPORT_SENDER_RESULT_PENDING;
|
||||
inst->successful = false;
|
||||
@@ -294,4 +342,3 @@ int HANDLER(void *report)
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user