aboutsummaryrefslogtreecommitdiffstats
path: root/src/ckerrlog.h
blob: 66923cb07c9f4f99c24d82eff14be6a3d7ff3646 (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
/* ckerrlog.h - 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)
 *
 * -----------------------------------------------------------------------------
 *
 * The error reporting and logging infrastructure used across ck.
 * This file defines the basic macros other functions should call to
 * report problems or help the user.
 *
 * All macros use printf like syntax to format the resutling strings.
 * There is no need for newline (`\n`) it is automatically appended to
 * each string.
 *
 * ERRLOG(component-name)
 *   Should be called once in every .c file defining the component name
 *   to be used with error reporting and logging.
 *
 * LOG("format-string", ...)
 *   Add format-string to the log. The component name is prepended.
 *   The log can be saved to a logfile and/or printed at the end of
 *   the ck session provided the --verbose flag is active.
 *
 * ERR("format-string", ...)
 *   Reports the format-string at the moment of calling, prepended with
 *   Error and the component-name. Also adds the error to the log.
 *
 * HELP("format-string", ...)
 *   Print the format-string at the moment of calling, without adding
 *   any component name nor pushing the string to the log.
 *
 * -------------------------------------------------------------------------- */
#ifndef CKERRLOG_H
#define CKERRLOG_H

#include <stdarg.h>

#include "ckutil.h"

#define CK_STREAMS                              \
  X(err)                                        \
  X(log)                                        \
  X(logv)                                       \
  X(help)

extern void initialize_errlog(int argc, char* argv[]);
extern void report_errlog();
extern void errlog_set_verbose(int level);

extern void ckerr(char *err, ...);
extern void cklog(char *log, ...);
extern void ckhelp(char *log, ...);

#define X(stream)                                               \
  extern void ck## stream(char *log, ...);                      \
  void ck## stream ##_with_delim(char *d, char *txt, ...);      \
  void report_## stream();
CK_STREAMS
#undef X

/**********/
/* Macros */
/**********/

/* define the component's name */
#define ERRLOG(_COMPONENT)                              \
  static const char COMPONENT[STR_S] = #_COMPONENT

#define ERR(...)                                          \
  ckerr_with_delim("\n", "Error in [%s]:", COMPONENT);    \
  ckerr(__VA_ARGS__);                                     \
  cklog_with_delim(" ", "ERROR: [%s]", COMPONENT);      \
  cklog(__VA_ARGS__);                                     \
  report_err();                                 

/* Print help message */
#define HELP(...)                               \
  ckhelp(__VA_ARGS__);                          \
  report_help();

/* Log event */
#define LOG(...)                                   \
  cklog_with_delim(" ", "[%s]", COMPONENT);        \
  cklog(__VA_ARGS__);

#define LOG_V(...)
#endif /* CKERRLOG_H */