aboutsummaryrefslogtreecommitdiffstats
path: root/src/ckerrlog.c
diff options
context:
space:
mode:
authorgramanas <anastasis.gramm2@gmail.com>2018-10-04 01:20:13 +0300
committergramanas <anastasis.gramm2@gmail.com>2018-10-04 01:20:13 +0300
commitfa41863b03bbf67a7ad4edad9328b0cdc48e497b (patch)
treee7e4dd5986364c29a3d51ac661ce9b133effc868 /src/ckerrlog.c
parentbc2899dd69fce0f1868dbfad3394ef8b3dc48069 (diff)
downloadck-fa41863b03bbf67a7ad4edad9328b0cdc48e497b.tar.gz
ck-fa41863b03bbf67a7ad4edad9328b0cdc48e497b.tar.bz2
ck-fa41863b03bbf67a7ad4edad9328b0cdc48e497b.zip
store configs in subfolders and error/logging infrastructure
Diffstat (limited to 'src/ckerrlog.c')
-rw-r--r--src/ckerrlog.c86
1 files changed, 86 insertions, 0 deletions
diff --git a/src/ckerrlog.c b/src/ckerrlog.c
new file mode 100644
index 0000000..1be1c08
--- /dev/null
+++ b/src/ckerrlog.c
@@ -0,0 +1,86 @@
+/* ckerrlog.c - Error report and logging for ck ------------------------*- C -*-
+ *
+ * This file is part of ck, the config keeper
+ *
+ * -----------------------------------------------------------------------------
+ *
+ * Copyright (C) 2018 Anastasis Grammenos
+ * GPLv3 (see LICENCE for the full notice)
+ *
+ * -------------------------------------------------------------------------- */
+#include <time.h>
+
+#include "ckerrlog.h"
+#include "ckutil.h"
+
+#define COMPONENT "ckerrlog"
+
+static char *err;
+static char *log;
+static int loglvl;
+static char buf[STR_M];
+
+char * get_time() {
+ time_t rawtime;
+ struct tm * timeinfo;
+ time (&rawtime);
+ timeinfo = localtime (&rawtime);
+ strftime (buf,80,"[%c]",timeinfo);
+ return buf;
+}
+
+void initialize_errlog() {
+ err = NULL;
+ log = NULL;
+ loglvl = 0;
+ cklog("%s Log session started", get_time());
+}
+
+void report_errlog() {
+ if (err)
+ printf("Errors:\n%s", err);
+ free(err);
+ if (log)
+ printf("Logs:\n%s", log);
+ free(log);
+}
+
+#define X(stream) \
+ void add_ ## stream ## _with_delim(char *delim, char *txt, \
+ va_list args) { \
+ char tmp[STR_L]; \
+ vsprintf(tmp, txt, args); \
+ if (stream) { \
+ stream = (char *)realloc(stream, strlen(stream) + \
+ strlen(tmp) + strlen(delim) + 1); \
+ strcat(stream, tmp); \
+ strcat(stream, delim); \
+ return; \
+ } \
+ stream = (char *)malloc(strlen(tmp) + strlen(delim) + 1); \
+ strcpy(stream, tmp); \
+ strcat(stream, delim); \
+ }
+CK_STREAMS
+#undef X
+
+void ckerr(char *txt, ...) {
+ va_list args;
+ va_start(args, txt);
+ add_err_with_delim("\n", txt, args);
+ va_end(args);
+}
+
+void cklog(char *txt, ...) {
+ va_list args;
+ va_start(args, txt);
+ add_log_with_delim("\n", txt, args);
+ va_end(args);
+}
+
+void ckerr_add_component(char *txt, ...) {
+ va_list args;
+ va_start(args, txt);
+ add_err_with_delim(" ", txt, args);
+ va_end(args);
+}