From 95a49ebefa984e54079040c39b09c471d393d418 Mon Sep 17 00:00:00 2001 From: William Date: Sat, 8 Jun 2024 23:38:38 +0800 Subject: [PATCH] (FIX) Fixed problem of multiple same output for one Status from PrintStatus. --- Status/include/status.h | 45 ++++++++++++++++++++++++++++++++---- Status/src/status.c | 42 +++++++++++++++------------------ catlog.c | 2 +- common.h | 51 +++++++++++++++++++++++------------------ 4 files changed, 90 insertions(+), 50 deletions(-) diff --git a/Status/include/status.h b/Status/include/status.h index 76809bb..421adb8 100644 --- a/Status/include/status.h +++ b/Status/include/status.h @@ -222,7 +222,6 @@ bool Status_Equal(Status *stat1, Status *stat2); void StatusUtils_Dump(Status *inst, Status *store, int idx); 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); @@ -331,6 +330,11 @@ DEFSTATUS(ImprecisionError, 1, // ---------------------USER DEFINED----------------------- +// The meaning of value is defined by the function itself. +DEFSTATUS(TraditionalFunctionReturn, 0, + "Function has returned an integer.", + STATUS_UNKNOWN, &UnknownStatus); + DEFSTATUS(UnavailableInstance, 1, "An unavailable instance was given for initialisation.", STATUS_ERROR, &NullPointerAccounted); @@ -399,6 +403,39 @@ DEFSTATUS(InvalidLiteralisingBuffer, 1, "Given buffer does not have a good integrity on its length.", STATUS_ERROR, &InvalidObject); +// ======================================================== + +static inline Status PrintStatus(Status s) +{ + char buff[LITERALISATION_LENGTH_MAXIMUM]; + (void)Status_Literalise(&s, buff); + + where(fprintf(stderr, "%s\n", buff), { + return apply(value(TraditionalFunctionReturn, _)); + }) +} + +static inline void PrintStatusDump(Status s) +{ + Status stat = s; + const int dump_len = StatusUtils_Depth(&stat); + Status dump[dump_len]; + StatusUtils_Dump(&stat, dump, 0); + /* TODO(william): Replace following line with coloured-term-output function. */ + (void)printf("\e[1m======== STATUS STACK DUMP: %s ========\e[0m\n", + stat.identity); + for (register int i = 0; i < dump_len; i++) { + /* TODO(william): Replace following line with coloured-term-output function. */ + (void)printf("\e[1m[%d/%d]\e[0m\n", (dump_len - i), dump_len); + seek(PrintStatus(dump[i]), { + solve(!_.value, { + (void)fprintf(stderr, "\e[38;5;9m!!!!!!!! PRINT DUMP FAILED !!!!!!!!" + "\e[0m\n"); + (void)PrintStatus(_); + }) + }) + } +} // ======================================================== @@ -424,14 +461,14 @@ DEFSTATUS(InvalidLiteralisingBuffer, 1, /* Add location parameter requirement in order to give proper information * before throwing the report out. */ -# define throw(report) __throw(report, __HERE__) +# define throw(report) THROW(report, __HERE__) /* Useless in C, only for human to see. Probably rewrite this in Classify. */ # define throws(e) -ReportSendingTaskID __throw(Report report, Location loc); -Report catch(ReportSendingTaskID taskid); +ReportSendingTaskID THROW(Report report, Location loc); +Report CATCH(ReportSendingTaskID taskid); int HANDLER(void *report); #endif /* COMPOUND_STATUS_H */ diff --git a/Status/src/status.c b/Status/src/status.c index df91541..d3257c7 100644 --- a/Status/src/status.c +++ b/Status/src/status.c @@ -11,8 +11,8 @@ Status Location_Literalise(Location *inst, char *buff) /* Concatenate every buff. */ const long total_len = strlen(strnil(inst->file)) + strlen(strnil(line_buff)) - + strlen(strnil(inst->func)) - + LOCATION_LITERALISE_FORMAT_LENGTH; + + strlen(strnil(inst->func)) + + LOCATION_LITERALISE_FORMAT_LENGTH; state(total_len > LITERALISATION_LENGTH_MAXIMUM, apply(MaximumLiteralisationLengthExceeded)); @@ -62,30 +62,31 @@ Status Status_Literalise(Status *inst, char *buff) /* Literalise loc. */ char loc_buff[LITERALISATION_LENGTH_MAXIMUM]; notok(Location_Literalise(&inst->loc, loc_buff), { - return _; + return apply(_); }); /* Styling output. */ + // TODO(william): Replace following lines with const char *fmt; if (inst->characteristic == STATUS_ERROR) { // TODO(william): Replace following line with coloured-term-output function. - fmt = "\e[38;5;9m\e[4m\e[1m"STATUS_LITERALISE_FORMAT"\e[0m"; + fmt = "\e[38;5;9m\e[1m"STATUS_LITERALISE_FORMAT"\e[0m"; } else if (inst->characteristic == STATUS_UNKNOWN) { // TODO(william): Replace following line with coloured-term-output function. - fmt = "\e[38;5;11m\e[4m"STATUS_LITERALISE_FORMAT"\e[0m"; + fmt = "\e[38;5;11m"STATUS_LITERALISE_FORMAT"\e[0m"; } else { // TODO(william): Replace following line with coloured-term-output function. - fmt = "\e[38;5;10m\e[2m"STATUS_LITERALISE_FORMAT"\e[0m"; + fmt = "\e[38;5;10m"STATUS_LITERALISE_FORMAT"\e[0m"; } /* Concatenate every buffer. */ - state(!sprintf(buff, fmt, - inst->identity, inst->description, - (!inst->prev ? "(null)" : (inst->prev->identity)), - inst->value, inst->characteristic, loc_buff), - apply(error(ReadWriteError, "No bytes were written into buffer."))); - - return apply(NormalStatus); + where(sprintf(buff, fmt, inst->identity, inst->description, + (!inst->prev + ? "(null)" + : (inst->prev->identity)), + inst->value, inst->characteristic, loc_buff), { + return apply(value(TraditionalFunctionReturn, _)); + }) } bool StatusUtils_HasPrev(Status stat) @@ -98,12 +99,6 @@ bool StatusUtils_IsOkay(Status stat) return (!stat.characteristic); } -bool StatusUtils_IsValid(Status stat) -{ - return (!strcmp(stat.description, "") && stat.characteristic >= 0 - && !stat.prev); -} - bool StatusUtils_IsRecursive(Status stat) { return (stat.prev && stat.prev == &stat); @@ -111,9 +106,8 @@ bool StatusUtils_IsRecursive(Status stat) void StatusUtils_Dump(Status *inst, Status *store, int idx) { - /* Skip when either stat or stat.prev is unavailable, or, idx is invalid. */ - svoid((!store || !StatusUtils_HasPrev(*inst) || idx < 0)); + svoid((!inst || !store || idx < 0)); store[idx] = *inst; @@ -128,7 +122,9 @@ int StatusUtils_Depth(Status *stat) Status *p = stat; // Include this layer of Status. register int cnt; for (cnt = 0; (!StatusUtils_IsRecursive(*p) - && StatusUtils_HasPrev(*p)); cnt++) p = p->prev; + && StatusUtils_HasPrev(*p)); cnt++) { + p = p->prev; + } return cnt; } @@ -288,7 +284,7 @@ Status ReportSender_Send(ReportSender *inst, ReportSendingTask task) // || (inst1->external_param == inst2->external_param); // } -ReportSendingTaskID _throw(Report report, Location loc) +ReportSendingTaskID THROW(Report report, Location loc) { // // /* Create new a instance of ReportSender. */ // // ReportSender sender; diff --git a/catlog.c b/catlog.c index 1fc47c1..30b6d9d 100644 --- a/catlog.c +++ b/catlog.c @@ -91,7 +91,7 @@ Status CatlogSender_Send(CatlogSender *inst) /* Skip unavailable instances and parameters. */ fails(inst, apply(UnavailableInstance)); - const int written = fprintf(inst->dst, "%s", inst->msg.content); + const int written = fprintf(inst->dst, "%s\n", inst->msg.content); /* Write msg. */ state(!written, error(ReadWriteError, "No bytes were written into buffer.")); diff --git a/common.h b/common.h index 8ff4405..5314232 100644 --- a/common.h +++ b/common.h @@ -23,6 +23,18 @@ /* Another way to handle if statements more cleanly. */ # define solve(s, b) { if (s) b } +/* Handling expression with its result. */ +# define when(expr, b) { int _ = expr; if (expr) b } + +/* Handling expression with its calculated result. */ +# define where(expr, b) { int _ = expr; b } + +# define ok(s, b) { Status _ = s; if (StatusUtils_IsOkay(_)) b } + +# define notok(s, b) { Status _ = s; if (!StatusUtils_IsOkay(_)) b } + +# define seek(s, b) { Status _ = s; b } + /* Create a new UnknownStatus on the fly. */ # define unknown(e, c, v) ((Status) {\ .identity = e.identity,\ @@ -60,7 +72,7 @@ .description = e.description,\ .characteristic = p.characteristic,\ .loc = __HERE__,\ - .prev = &p\ + .prev = (Status *)&p\ }) # define value(e, v) ((Status) {\ @@ -98,12 +110,6 @@ CatlogSender_Send(&sender);\ } -# define ok(s, b) { Status _ = s; if (StatusUtils_IsOkay(_)) b } - -# define notok(s, b) { Status _ = s; if (!StatusUtils_IsOkay(_)) b } - -# define seek(s, b) { Status _ = s; b } - // /** // * @brief Forcibly return desired value $v once $s is not $k. // * @return $v once state for $s is false. @@ -125,22 +131,23 @@ // (void)printf("%s\n", buff);\ // } -# define print_status(s) {\ - char buff[LITERALISATION_LENGTH_MAXIMUM];\ - (void)Status_Literalise(&s, buff);\ - (void)fprintf(stderr, "%s\n", buff);\ -} +// # define print_status(s) {\ +// char buff[LITERALISATION_LENGTH_MAXIMUM];\ +// (void)Status_Literalise(&s, buff);\ +// (void)fprintf(stderr, "%s\n", buff);\ +// } -# define print_statusdump(s) {\ - Status _ = s;\ - const int dump_len = StatusUtils_Depth(&_);\ - Status dump[dump_len];\ - StatusUtils_Dump(&_, dump, 0);\ - for (register int i = 0; i < dump_len; i++) {\ - (void)printf("%d/%d\n", i, dump_len - 1);\ - print_status(dump[i]);\ - }\ -} +// # define print_statusdump(s) {\ +// Status _ = s;\ +// const int dump_len = StatusUtils_Depth(&_);\ +// Status dump[dump_len];\ +// StatusUtils_Dump(&_, dump, 0);\ +// for (register int i = 0; i < dump_len; i++) {\ +// /* TODO(william): Replace following line with coloured-term-output function. */\ +// (void)printf("\e[1m[%d/%d]\e[0m\n", i, dump_len - 1);\ +// print_status(dump[i]);\ +// }\ +// } # define strnil(s) (!s ? ("(null)") : s)