Files
Compound/catlog.c
William Lee bc4be4e295 (MOD) Implemented Array_Create, Array_CopyOf and Array_Delete
(MOD) Replaced project compiler from "gcc" with "clang" due to out-of-order execution keep happening after running with Array.

(MOD) Removed certain functions from Utils due to insufficiencies of usage.

(MOD) Defined 1 more macro "fail".

(MOD) Fixed 1 bug from "var.c":  Struct member "alive" was not under supervision from the entire programme cycle.

(MOD) Removed 1 useless usage for "Utils_LiteraliseInteger".

(MOD) Removed 1 useless block of commented code.
2024-06-26 15:20:47 +08:00

123 lines
3.5 KiB
C

#include <Compound/common.h>
#include <Compound/status.h>
#include <Compound/catlog.h>
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 *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)));
}