aboutsummaryrefslogtreecommitdiffstats
path: root/src/ckerrlog.c
blob: 9e1fc5111b4ef767de6fa874546c125a355f02d9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/* 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 int loglvl;
static char buf[STR_M];

#define X(stream) static char *stream;
CK_STREAMS
#undef X

char * get_time() {
  time_t rawtime;
  struct tm * timeinfo;
  time (&rawtime);
  timeinfo = localtime (&rawtime);
  strftime (buf,80,"[%c]",timeinfo);
  return buf;
}

void initialize_errlog() {
#define X(stream) stream = NULL;
  CK_STREAMS
#undef X
  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 ckhelp(char *txt, ...) {
  va_list args;
  va_start(args, txt);
  add_help_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);
}

void ckhelp_add_component(char *txt, ...) {
  va_list args;
  va_start(args, txt);
  add_help_with_delim(" ", txt, args);
  va_end(args);
}

void report_err() {
  printf("%s", err);
  free(err);
  err = NULL;
}

void report_help() {
  printf("%s", help);
  free(help);
  help = NULL;
}