#include "Status/include/status.h" #include #include #include Status CatlogMsg_Create(CatlogMsg *inst, CatlogLevel level, char *initiator, char *msg) { /* Skip unavailable instances and parameters. */ nonull(inst, apply(UnavailableInstance)); state((initiator == NULL || msg == NULL), apply(InvalidParameter)); inst->time = time(NULL); inst->level = level; inst->initiator = initiator; inst->content = msg; return apply(NormalStatus); } Status CatlogMsg_CopyOf(CatlogMsg *inst, CatlogMsg *other) { /* Skip unavailable instances and parameters. */ nonull(inst, apply(UnavailableInstance)); nonull(other, apply(InvalidParameter)); *inst = *other; return apply(NormalStatus); } bool CatlogMsg_Equals(CatlogMsg *inst, CatlogMsg *other) { /* Skip unavailable instances and parameters. */ state((!inst || other == NULL), false); return ( inst->time == other->time && inst->level == other->level && (!strcmp(inst->initiator, other->initiator)) && (!strcmp(inst->content, other->content)) ); } Status CatlogSender_Create(CatlogSender *inst, CatlogMsg *msg, FILE *dst) { /* Skip unavailable instances and parameters. */ nonull(inst, apply(UnavailableInstance)); nonull(msg, apply(InvalidParameter)); /* Copy and assign. */ inst->msg = *msg; inst->dst = (!dst ? stdout : dst); inst->successful = false; inst->elapsed = (struct timespec){ .tv_sec = 0, .tv_nsec = 0 }; return apply(NormalStatus); } Status CatlogSender_CopyOf(CatlogSender *inst, CatlogSender *other) { /* Skip unavailable instances and parameters. */ nonull(inst, apply(UnavailableInstance)); nonull(other, apply(InvalidParameter)); /* Copy and assign */ inst->msg = other->msg; inst->dst = other->dst; inst->successful = other->successful; inst->elapsed = other->elapsed; return apply(NormalStatus); } bool CatlogSender_Equals(CatlogSender *inst, CatlogSender *other) { /* Skip unavailable instances and parameters. */ state((!inst || other == NULL), false); return ( CatlogMsg_Equals(&inst->msg, &other->msg) && inst->dst == other->dst && inst->successful == other->successful && ((inst->elapsed.tv_sec == other->elapsed.tv_sec) && (inst->elapsed.tv_nsec == other->elapsed.tv_nsec)) ); } Status CatlogSender_Send(CatlogSender *inst) { /* Skip unavailable instances and parameters. */ nonull(inst, apply(UnavailableInstance)); const int written = fprintf(inst->dst, "%s\n", inst->msg.content); /* Write msg. */ state(!written, error(ReadWriteError, "No bytes were written into buffer.")); return apply(NormalStatus); } Status CatlogUtils_OpenFile(FILE **fileptr, const char *filepath, const char const *restrict mode) { /* Skip unavailable instances and parameters. */ nonull(fileptr, apply(UnavailableBuffer)); nonull(filepath, apply(UnavailableFileName)); nonull(mode, apply(UnavailableFileAccessMode)); /* Open the file. Return CatCannotOpenFile once failed. */ state(!(*fileptr = fopen(filepath, mode)), apply(CatCannotOpenFile)); return apply(NormalStatus); } Status CatlogUtils_CloseFile(FILE **fileptr) { /* Skip if either the fileptr or the *fileptr is unavailable. */ state(!fileptr || !*fileptr, apply(UnavailableParameter)); /* Return returning code of fclose, sealed with "value". */ return apply(value(UnknownStatus, fclose(*fileptr))); }