diff --git a/Status/include/status.h b/Status/include/status.h index 987c2d8..dbdefa4 100644 --- a/Status/include/status.h +++ b/Status/include/status.h @@ -78,78 +78,6 @@ typedef struct _Status { # 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. - REPORT_SENDING_PRIORITY_FATAL, - REPORT_SENDING_PRIORITY_EXCEPTIONAL, - REPORT_SENDING_PRIORITY_CRITICAL, - REPORT_SENDING_PRIORITY_MAJOR, - REPORT_SENDING_PRIORITY_NORMAL, - REPORT_SENDING_PRIORITY_MINOR, - REPORT_SENDING_PRIORITY_DEBUG, - REPORT_SENDING_PRIORITY_NONE, // Lowest level, greatest value. -} ReportLevel; - -typedef enum { - REPORT_SENDING_TASK_STATUS_FINISHED = 0, - REPORT_SENDING_TASK_STATUS_PENDING, - REPORT_SENDING_TASK_STATUS_PROCEEDING, - REPORT_SENDING_TASK_STATUS_PAUSED, - REPORT_SENDING_TASK_STATUS_NOTFOUND -} ReportStatus; - -/* "Report" recollects essential informations, included but not limited to - Status and others for making an report for debugging and such. */ -typedef struct { - Status content; - char *initiator; - time_t time; - ReportLevel level; - ReportStatus status; - FILE *dst; // The destination where the report is sending to. -} Report; - -/* -DATETIME [PRIORITY] STATUSNAME (ORIGINATOR): STATUS.DESCRIPTION - at LOCATION.FILE:LOCATION.LINE, LOCATION.FUNC - at LOCATION.FILE:LOCATION.LINE, LOCATION.FUNC - at LOCATION.FILE:LOCATION.LINE, LOCATION.FUNC - at LOCATION.FILE:LOCATION.LINE, LOCATION.FUNC - - -Fri 10 May 03:02:37 CST 2024 [URGENT] InvalidParameter (Nullity): Given buffer was unavailable. - at /external/Documents/Projects/Compound/Status/src/status.c:104, Report_Literalise - at /external/Documents/Projects/Compound/Status/src/status.c:114, ReportSender_Send - at /external/Documents/Projects/Compound/Status/src/status.c:69, _throw -!!!!at /external/Documents/Projects/Compound/Array/src/array.c:16, array_create - at /external/Documents/Projects/Compound/test.c:24, main - -*/ - -// DATETIME [LEVEL] STATUS.IDENTITY (INITIATOR): STATUS.DESCRIPTION -# define REPORT_LITERALISE_FORMAT_HEADER "%s [%d] %s (%s): %s\n\tat %s:%d, %s\n%s" - -// STATUS.IDENTITY, STATUS.PREV.IDENTITY, STATUS.VALUE, STATUS.CHARACTERISTIC, -// FILE, LINE, FUNC -# define REPORT_LITERALISE_FORMAT_DETAIL "\t%s(%s, %d, %d) at %s:%d, %s\n" - -typedef enum { - REPORT_RESULT_SUCCEEDED, - REPORT_RESULT_FAILED, - REPORT_RESULT_PROGRESSING, - REPORT_RESULT_PENDING, -} ReportResult; - -typedef struct { - thrd_t thread; - Report report; - time_t elapsed; - ReportResult result; -} ReportSender; - -typedef int (*ReportTask)(Report *); -typedef int ReportTaskID; - // typedef thrd_start_t ArgueStart; // typedef struct { @@ -194,20 +122,6 @@ bool StatusUtils_IsOkay(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); -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, thrd_start_t *handler); -Status ReportSender_Create(ReportSender *inst, Report *report); -Status ReportSender_Send(ReportSender *inst, ReportTask task); - -// ReportTaskStatus -// ReportSender_GetStatus(ReportSender *inst); - // Status // arguestarter_create(ArgueStartParam *inst, void *external_param); // Status @@ -446,18 +360,4 @@ static inline void PrintStatusDump(Status s) } } -// ======================================================== - -/* Add location parameter requirement in order to give proper information - * before throwing the report out. */ -// # define throw(report) THROW(report, __HERE__) - -/* Useless in C, only for human to see. - Probably rewrite this in Classify. */ -# define throws(e) - -// ReportTaskID THROW(Report report, Location loc); -// Report CATCH(ReportTaskID taskid, Status (*handler)()); -int HANDLER(void *report); - #endif /* COMPOUND_STATUS_H */ diff --git a/Status/src/status.c b/Status/src/status.c index dba2d28..a3dd965 100644 --- a/Status/src/status.c +++ b/Status/src/status.c @@ -166,156 +166,6 @@ int StatusUtils_Depth(Status *stat) // return cnt; } -Status Report_Create(Report *inst, Status *stat, FILE *dest, char *initiator, - int priority) -{ - /* Skip unavailable parameters. */ - nonull(inst, apply(UnavailableInstance)); - nonull(stat, apply(error(InvalidParameter, "Given stat was null."))); - nonull(initiator, apply(error(InvalidParameter, "Given initiator was null."))); - state(priority < 0, apply(error(InvalidParameter, "Given priority was negative."))); - - /* Copy and assign. */ - inst->content = *stat; - inst->initiator = calloc(strlen(initiator), sizeof(char)); - (void)strcpy(inst->initiator, initiator); - inst->time = time(NULL); - inst->level = priority; - inst->status = REPORT_SENDING_TASK_STATUS_PENDING; - inst->dst = (dest == NULL ? stdout : dest); - - return apply(NormalStatus); -} - -Status Report_CopyOf(Report *inst, Report *other) -{ - nonull(inst, apply(UnavailableInstance)); - nonull(other, apply(error(InvalidParameter, "Given report is unavailable."))); - - // Status status; - // char *initiator; - // time_t time; - // ReportLevel priority; - // ReportTaskStatus taskprint_status; - // FILE *dest; - inst->content = other->content; - - return apply(NormalStatus); -} - -void Report_Delete(Report *inst) -{ - svoid(inst); - - free(inst->initiator); - inst->initiator = NULL; - inst->dst = NULL; - inst->level = 0; - inst->content = (Status){}; - inst->status = REPORT_SENDING_TASK_STATUS_NOTFOUND; - inst->time = 0; - inst = NULL; -} - -/* - Status status; - char *initiator; - time_t time; - ReportLevel priority; - ReportTaskStatus taskprint_status; - FILE *dest; -*/ -Status Report_Literalise(Report *inst, char *buff) -{ - nonull(inst, apply(UnavailableInstance)); - nonull(buff, apply(UnavailableBuffer)); - - /* Report literalisation. */ - char report_literalising[LITERALISATION_LENGTH_MAXIMUM] = EMPTY; - - /** Status literalisation. **/ - char status_literalising[LITERALISATION_LENGTH_MAXIMUM] = EMPTY; - - /* Fault detection on status literalisation. */ - // settle(Status_LiteraliseForReport(&inst->status, status_literalising), - // _.characteristic == STATUS_UNKNOWN, { - // nest(_, __, { - // /* Skip when ZERO byte were written. (failed to write) */ - // state(!__.value, apply( - // error(value(TraditionalFunctionReturn, __.value), - // "ZERO byte were written.") - // )); - // }) - // }); - - /* Traditional function returning handling. */ - settle(Status_LiteraliseForReport(&inst->content, status_literalising), - !_.value, { - return apply(annot(RuntimeError, "Failed to literalise status for report.")); - }); - - /* Write result to buffer. */ - /* Write the report "header". */ - /* Literalise current time and date. */ - char datetime[LITERALISATION_LENGTH_MAXIMUM]; - // settle(strftime(datetime, 64, "%c", localtime(&inst->time)), ) - - // DATETIME [LEVEL] STATUS.IDENTITY (INITIATOR): STATUS.DESCRIPTION - state(!snprintf(report_literalising, LITERALISATION_LENGTH_MAXIMUM, - REPORT_LITERALISE_FORMAT_HEADER, datetime, inst->level, - inst->content.identity, inst->initiator, inst->content.description), - apply(annot(NoBytesWereWritten, "Failed to literalise date and time.")) - ); - - /* Write the report "detail". */ - - return apply(NormalStatus); -} - -/* - thrd_t thread; - Report report; - time_t elapsed; - ReportResult result; -*/ -Status ReportSender_Create(ReportSender *inst, Report *report) -{ - nonull(inst, apply(UnavailableInstance)); - nonull(report, error(UnavailableParameter, "Given report was unavailable.")); - - thrd_create(&inst->thread, &HANDLER, report); - notok(Report_CopyOf(&inst->report, report), - return apply(annot(InstanceCreatingFailure, - "Cannot copy to create new instance of report.")); - ); - inst->report = *report; - inst->elapsed = 0; - inst->result = REPORT_RESULT_PENDING; - - return apply(NormalStatus); -} - -Status ReportSender_Send(ReportSender *inst, ReportTask task) -{ - // /* Skip when inst or task is unavailable. */ - // nonull(inst, - // error(UnavailableInstance, "Report sender was given unavailable.")); - // nonull(task, InvalidReportTask); - - // /* Assign for dest. */ - // const FILE *dest = (inst->report->dest == NULL ? stdout : inst->report->dest); - // // char buff[]; - // // TODO(william): HERE, Report_Literalise - - // /* Write/Send data. */ - // inst->report->taskprint_status = REPORT_SENDING_TASK_STATUS_PROCEEDING; - // if (!fprintf(dest, buff)) { - // } - - // /* Sent successfully! Mark down properties. */ - return apply(NormalStatus); -} - // bool arguestarter_equal(ArgueStarter *inst1, ArgueStarter *inst2) // { // /* Skip when either inst1 or inst2 is unavailable. */ diff --git a/preinstall b/preinstall index 59d081f..2835f8d 100755 --- a/preinstall +++ b/preinstall @@ -30,6 +30,6 @@ done # Install individual headers. cp -v "common.h" "const.h" "platform.h"\ "name.h" "namescope.h" "type.h" "catlog.h"\ - "attr.h" "registry.h" "class.h" "function.h" "$DST" + "attr.h" "registry.h" "class.h" "function.h" "report.h" "$DST" printf "\nDone\n" diff --git a/report.c b/report.c new file mode 100644 index 0000000..c9ac737 --- /dev/null +++ b/report.c @@ -0,0 +1,151 @@ +#include + +Status Report_Create(Report *inst, Status *stat, FILE *dest, char *initiator, + int priority) +{ + /* Skip unavailable parameters. */ + nonull(inst, apply(UnavailableInstance)); + nonull(stat, apply(error(InvalidParameter, "Given stat was null."))); + nonull(initiator, apply(error(InvalidParameter, "Given initiator was null."))); + state(priority < 0, apply(error(InvalidParameter, "Given priority was negative."))); + + /* Copy and assign. */ + inst->content = *stat; + inst->initiator = calloc(strlen(initiator), sizeof(char)); + (void)strcpy(inst->initiator, initiator); + inst->time = time(NULL); + inst->level = priority; + inst->status = REPORT_SENDING_TASK_STATUS_PENDING; + inst->dst = (dest == NULL ? stdout : dest); + + return apply(NormalStatus); +} + +Status Report_CopyOf(Report *inst, Report *other) +{ + nonull(inst, apply(UnavailableInstance)); + nonull(other, apply(error(InvalidParameter, "Given report is unavailable."))); + + // Status status; + // char *initiator; + // time_t time; + // ReportLevel priority; + // ReportTaskStatus taskprint_status; + // FILE *dest; + inst->content = other->content; + + return apply(NormalStatus); +} + +void Report_Delete(Report *inst) +{ + svoid(inst); + + free(inst->initiator); + inst->initiator = NULL; + inst->dst = NULL; + inst->level = 0; + inst->content = (Status){}; + inst->status = REPORT_SENDING_TASK_STATUS_NOTFOUND; + inst->time = 0; + inst = NULL; +} + +/* + Status status; + char *initiator; + time_t time; + ReportLevel priority; + ReportTaskStatus taskprint_status; + FILE *dest; +*/ +Status Report_Literalise(Report *inst, char *buff) +{ + nonull(inst, apply(UnavailableInstance)); + nonull(buff, apply(UnavailableBuffer)); + + /* Report literalisation. */ + char report_literalising[LITERALISATION_LENGTH_MAXIMUM] = EMPTY; + + /** Status literalisation. **/ + char status_literalising[LITERALISATION_LENGTH_MAXIMUM] = EMPTY; + + /* Fault detection on status literalisation. */ + // settle(Status_LiteraliseForReport(&inst->status, status_literalising), + // _.characteristic == STATUS_UNKNOWN, { + // nest(_, __, { + // /* Skip when ZERO byte were written. (failed to write) */ + // state(!__.value, apply( + // error(value(TraditionalFunctionReturn, __.value), + // "ZERO byte were written.") + // )); + // }) + // }); + + /* Traditional function returning handling. */ + settle(Status_LiteraliseForReport(&inst->content, status_literalising), + !_.value, { + return apply(annot(RuntimeError, "Failed to literalise status for report.")); + }); + + /* Write result to buffer. */ + /* Write the report "header". */ + /* Literalise current time and date. */ + char datetime[LITERALISATION_LENGTH_MAXIMUM]; + // settle(strftime(datetime, 64, "%c", localtime(&inst->time)), ) + + // DATETIME [LEVEL] STATUS.IDENTITY (INITIATOR): STATUS.DESCRIPTION + state(!snprintf(report_literalising, LITERALISATION_LENGTH_MAXIMUM, + REPORT_LITERALISE_FORMAT_HEADER, datetime, inst->level, + inst->content.identity, inst->initiator, inst->content.description), + apply(annot(NoBytesWereWritten, "Failed to literalise date and time.")) + ); + + /* Write the report "detail". */ + + return apply(NormalStatus); +} + +/* + thrd_t thread; + Report report; + time_t elapsed; + ReportResult result; +*/ +Status ReportSender_Create(ReportSender *inst, Report *report) +{ + nonull(inst, apply(UnavailableInstance)); + nonull(report, error(UnavailableParameter, "Given report was unavailable.")); + + thrd_create(&inst->thread, &HANDLER, report); + notok(Report_CopyOf(&inst->report, report), + return apply(annot(InstanceCreatingFailure, + "Cannot copy to create new instance of report.")); + ); + inst->report = *report; + inst->elapsed = 0; + inst->result = REPORT_RESULT_PENDING; + + return apply(NormalStatus); +} + +Status ReportSender_Send(ReportSender *inst, ReportTask task) +{ + // /* Skip when inst or task is unavailable. */ + // nonull(inst, + // error(UnavailableInstance, "Report sender was given unavailable.")); + // nonull(task, InvalidReportTask); + + // /* Assign for dest. */ + // const FILE *dest = (inst->report->dest == NULL ? stdout : inst->report->dest); + // // char buff[]; + // // TODO(william): HERE, Report_Literalise + + // /* Write/Send data. */ + // inst->report->taskprint_status = REPORT_SENDING_TASK_STATUS_PROCEEDING; + // if (!fprintf(dest, buff)) { + // } + + // /* Sent successfully! Mark down properties. */ + return apply(NormalStatus); +} diff --git a/report.h b/report.h new file mode 100644 index 0000000..17ec409 --- /dev/null +++ b/report.h @@ -0,0 +1,104 @@ +#ifndef COMPOUND_REPORT_H +# define COMPOUND_REPORT_H + +# include + +typedef enum { + REPORT_SENDING_PRIORITY_ALL = 0, // Highest level; least value. + REPORT_SENDING_PRIORITY_FATAL, + REPORT_SENDING_PRIORITY_EXCEPTIONAL, + REPORT_SENDING_PRIORITY_CRITICAL, + REPORT_SENDING_PRIORITY_MAJOR, + REPORT_SENDING_PRIORITY_NORMAL, + REPORT_SENDING_PRIORITY_MINOR, + REPORT_SENDING_PRIORITY_DEBUG, + REPORT_SENDING_PRIORITY_NONE, // Lowest level, greatest value. +} ReportLevel; + +typedef enum { + REPORT_SENDING_TASK_STATUS_FINISHED = 0, + REPORT_SENDING_TASK_STATUS_PENDING, + REPORT_SENDING_TASK_STATUS_PROCEEDING, + REPORT_SENDING_TASK_STATUS_PAUSED, + REPORT_SENDING_TASK_STATUS_NOTFOUND +} ReportStatus; + +/* "Report" recollects essential informations, included but not limited to + Status and others for making an report for debugging and such. */ +typedef struct { + Status content; + char *initiator; + time_t time; + ReportLevel level; + ReportStatus status; + FILE *dst; // The destination where the report is sending to. +} Report; + +/* +DATETIME [PRIORITY] STATUSNAME (ORIGINATOR): STATUS.DESCRIPTION + at LOCATION.FILE:LOCATION.LINE, LOCATION.FUNC + at LOCATION.FILE:LOCATION.LINE, LOCATION.FUNC + at LOCATION.FILE:LOCATION.LINE, LOCATION.FUNC + at LOCATION.FILE:LOCATION.LINE, LOCATION.FUNC + + +Fri 10 May 03:02:37 CST 2024 [URGENT] InvalidParameter (Nullity): Given buffer was unavailable. + at /external/Documents/Projects/Compound/Status/src/status.c:104, Report_Literalise + at /external/Documents/Projects/Compound/Status/src/status.c:114, ReportSender_Send + at /external/Documents/Projects/Compound/Status/src/status.c:69, _throw +!!!!at /external/Documents/Projects/Compound/Array/src/array.c:16, array_create + at /external/Documents/Projects/Compound/test.c:24, main + +*/ + +// DATETIME [LEVEL] STATUS.IDENTITY (INITIATOR): STATUS.DESCRIPTION +# define REPORT_LITERALISE_FORMAT_HEADER "%s [%d] %s (%s): %s\n\tat %s:%d, %s\n%s" + +// STATUS.IDENTITY, STATUS.PREV.IDENTITY, STATUS.VALUE, STATUS.CHARACTERISTIC, +// FILE, LINE, FUNC +# define REPORT_LITERALISE_FORMAT_DETAIL "\t%s(%s, %d, %d) at %s:%d, %s\n" + +typedef enum { + REPORT_RESULT_SUCCEEDED, + REPORT_RESULT_FAILED, + REPORT_RESULT_PROGRESSING, + REPORT_RESULT_PENDING, +} ReportResult; + +typedef struct { + thrd_t thread; + Report report; + time_t elapsed; + ReportResult result; +} ReportSender; + +typedef int (*ReportTask)(Report *); +typedef int ReportTaskID; + +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, thrd_start_t *handler); +Status ReportSender_Create(ReportSender *inst, Report *report); +Status ReportSender_Send(ReportSender *inst, ReportTask task); + +// ReportTaskStatus +// ReportSender_GetStatus(ReportSender *inst); + +/* Add location parameter requirement in order to give proper information + * before throwing the report out. */ +// # define throw(report) THROW(report, __HERE__) + +/* Useless in C, only for human to see. + Probably rewrite this in Classify. */ +# define throws(e) + +// ReportTaskID THROW(Report report, Location loc); +// Report CATCH(ReportTaskID taskid, Status (*handler)()); +int HANDLER(void *report); + +#endif /* COMPOUND_REPORT_H */