(FEA) More macros for Status
This commit is contained in:
83
catlog.c
Executable file → Normal file
83
catlog.c
Executable file → Normal file
@@ -1,30 +1,32 @@
|
||||
#include <Compound/catlog.h>
|
||||
#include "Status/include/status.h"
|
||||
#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. */
|
||||
fails(inst, UnavailableInstance);
|
||||
state((initiator == NULL || msg == NULL), InvalidParameter);
|
||||
fails(inst, apply(UnavailableInstance));
|
||||
state((initiator == NULL || msg == NULL), apply(InvalidParameter));
|
||||
|
||||
inst->time = time(NULL);
|
||||
inst->level = level;
|
||||
inst->initiator = initiator;
|
||||
inst->content = msg;
|
||||
|
||||
return NormalStatus;
|
||||
return apply(NormalStatus);
|
||||
}
|
||||
|
||||
Status CatlogMsg_CopyOf(CatlogMsg *inst, CatlogMsg *other)
|
||||
{
|
||||
/* Skip unavailable instances and parameters. */
|
||||
fails(inst, UnavailableInstance);
|
||||
fails(other, InvalidParameter);
|
||||
fails(inst, apply(UnavailableInstance));
|
||||
fails(other, apply(InvalidParameter));
|
||||
|
||||
*inst = *other;
|
||||
|
||||
return NormalStatus;
|
||||
return apply(NormalStatus);
|
||||
}
|
||||
|
||||
bool CatlogMsg_Equals(CatlogMsg *inst, CatlogMsg *other)
|
||||
@@ -43,23 +45,23 @@ bool CatlogMsg_Equals(CatlogMsg *inst, CatlogMsg *other)
|
||||
Status CatlogSender_Create(CatlogSender *inst, CatlogMsg *msg, FILE *dst)
|
||||
{
|
||||
/* Skip unavailable instances and parameters. */
|
||||
fails(inst, UnavailableInstance);
|
||||
fails(msg, InvalidParameter);
|
||||
fails(inst, apply(UnavailableInstance));
|
||||
fails(msg, apply(InvalidParameter));
|
||||
|
||||
/* Copy and assign, with detections. */
|
||||
/* Copy and assign. */
|
||||
inst->msg = *msg;
|
||||
inst->dst = (dst == NULL ? (stdout) : dst);
|
||||
inst->dst = (!dst ? stdout : dst);
|
||||
inst->successful = false;
|
||||
inst->elapsed = (struct timespec){.tv_sec = 0, .tv_nsec = 0};
|
||||
inst->elapsed = (struct timespec){ .tv_sec = 0, .tv_nsec = 0 };
|
||||
|
||||
return NormalStatus;
|
||||
return apply(NormalStatus);
|
||||
}
|
||||
|
||||
Status CatlogSender_CopyOf(CatlogSender *inst, CatlogSender *other)
|
||||
{
|
||||
/* Skip unavailable instances and parameters. */
|
||||
fails(inst, UnavailableInstance);
|
||||
fails(other, InvalidParameter);
|
||||
fails(inst, apply(UnavailableInstance));
|
||||
fails(other, apply(InvalidParameter));
|
||||
|
||||
/* Copy and assign */
|
||||
inst->msg = other->msg;
|
||||
@@ -67,7 +69,7 @@ Status CatlogSender_CopyOf(CatlogSender *inst, CatlogSender *other)
|
||||
inst->successful = other->successful;
|
||||
inst->elapsed = other->elapsed;
|
||||
|
||||
return NormalStatus;
|
||||
return apply(NormalStatus);
|
||||
}
|
||||
|
||||
bool CatlogSender_Equals(CatlogSender *inst, CatlogSender *other)
|
||||
@@ -84,35 +86,38 @@ bool CatlogSender_Equals(CatlogSender *inst, CatlogSender *other)
|
||||
);
|
||||
}
|
||||
|
||||
Status CatlogSender_Send(CatlogSender *inst, char *filepath, bool append)
|
||||
throws(ReadWriteError)
|
||||
Status CatlogSender_Send(CatlogSender *inst)
|
||||
{
|
||||
/* Skip unavailable instances and parameters. */
|
||||
fails(inst, UnavailableInstance);
|
||||
fails(filepath, UnavailableFileName);
|
||||
fails(inst, apply(UnavailableInstance));
|
||||
|
||||
/* Open file. */
|
||||
// ensure(CatlogUtils_OpenFile(inst->dst, (append ? "a" : "w")),
|
||||
// "Unable to open file.");
|
||||
(void)CatlogUtils_OpenFile(inst->dst, filepath, (append ? "a" : "w"));
|
||||
const int written = fprintf(inst->dst, "%s", inst->msg.content);
|
||||
|
||||
/* Write msg. */
|
||||
return unknown(NormalStatus, "", !fprintf(inst->dst, "%s", inst->msg.content));
|
||||
state(!written, error(ReadWriteError, "No bytes were written into buffer."));
|
||||
|
||||
return apply(NormalStatus);
|
||||
}
|
||||
|
||||
Status CatlogUtils_CalcElapsed(struct timespec t1, struct timespec t2);
|
||||
|
||||
Status CatlogUtils_OpenFile(FILE *store, char *filepath,
|
||||
const char const *__restrict mode)
|
||||
Status CatlogUtils_OpenFile(FILE **fileptr, const char *filepath,
|
||||
const char const *restrict mode)
|
||||
{
|
||||
/* No need to open a system output stream. */
|
||||
if (!strcmp(filepath, "stdin") ||
|
||||
!strcmp(filepath, "stdout") ||
|
||||
!strcmp(filepath, "stderr")) {
|
||||
return NormalStatus;
|
||||
}
|
||||
|
||||
store = fopen(filepath, mode);
|
||||
|
||||
return NormalStatus;
|
||||
/* Skip unavailable instances and parameters. */
|
||||
fails(fileptr, apply(UnavailableBuffer));
|
||||
fails(filepath, apply(UnavailableFileName));
|
||||
fails(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)));
|
||||
}
|
||||
|
Reference in New Issue
Block a user