summaryrefslogtreecommitdiff
path: root/src/log.h
blob: cc3bf89e722587ab785e99a7dd711f47622c41ce (plain)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#ifndef _SK_LOG_H
#define _SK_LOG_H

#include "util/font.h"


/* types */

enum sk_log_level {
	SK_LOG_ERROR = 1,
	SK_LOG_WARN,
	SK_LOG_INFO,
	SK_LOG_DEBUG,
	SK_LOG_TRACE,

	SK_LOG_MIN = SK_LOG_ERROR,
	SK_LOG_MAX = SK_LOG_TRACE
};

typedef enum sk_log_level sk_log_level;
typedef const char * sk_font_map[SK_LOG_MAX+1];

/* font maps */

extern sk_font_map _SK_LOG_COLOURED;
extern sk_font_map _SK_LOG_MONOCHROME;
#define SK_LOG_COLOURED &_SK_LOG_COLOURED
#define SK_LOG_MONOCHROME &_SK_LOG_MONOCHROME


/* type conversion functions */

/**
 * sk_log_level_to_string:
 * converts an sk_log_level to its string representation.
 *
 * @param level: the log level
 * @return the string representation of level.
 */
const char *sk_log_level_to_string(sk_log_level level) SK_RETURNS_NONNULL;

/**
 * sk_log_level_of_string:
 * converts a string representation to its sk_log_level.
 *
 * s is compared case-insensitively.
 * In case an invalid string representation is given,
 * 0 is returned.
 *
 * @param s: the string representation of the log level
 * @return sk_log_level
 */
sk_log_level sk_log_level_of_string(const char *s) SK_NONNULL(1);


/* getters/setters for global state variables */

/**
 * sk_log_level_get:
 * returns the current minimum log level to be logged.
 *
 * @return the log level
 */
sk_log_level sk_log_level_get();

/**
 * sk_log_level_set:
 * sets a new minimum log level to be logged.
 *
 * @param new: the new minimum log level.
 */
void sk_log_level_set(sk_log_level new);

/**
 * sk_log_font_get:
 * get the current logging font map.
 *
 * @return the current logging font map
 */
sk_font_map *sk_log_font_get() SK_RETURNS_NONNULL;

/**
 * sk_log_font_set:
 * sets a new logging font map.
 *
 * @param font: the new logging font map.
 */
void sk_log_font_set(sk_font_map *restrict font) SK_NONNULL(1);


/* logging functions */

/**
 * sk_log:
 * a generic logging function accepting the log level as a function argument.
 *
 * The message given using a format and format arguments will be formatted using
 * a standard format and a new line will be appended.
 *
 * @param level: the log level of this message.
 * @param format: the message as a format string, cf. printf(3).
 * @param varg: format arguments if needed.
 * @return the number of bytes logged (0 if no message was logged due to
 *         "insufficient" log level, and a negative number if an error occurred)
 */
int sk_log(sk_log_level level, const char *restrict format, ...)
	SK_PRINTF(2, 3);

/**
 * sk_error:
 * log an error message.
 *
 * cf. sk_log().
 */
#define sk_error(...) sk_log(SK_LOG_ERROR, __VA_ARGS__)

/**
 * sk_warn:
 * log a warning message.
 *
 * cf. sk_log().
 */
#define sk_warn(...) sk_log(SK_LOG_WARN, __VA_ARGS__)

/**
 * sk_info:
 * log an informational message.
 *
 * cf. sk_log().
 */
#define sk_info(...) sk_log(SK_LOG_INFO, __VA_ARGS__)

/**
 * sk_debug:
 * log a debug message.
 *
 * cf. sk_log().
 */
#define sk_debug(...) sk_log(SK_LOG_DEBUG, __VA_ARGS__)

/**
 * sk_trace:
 * log a trace message.
 *
 * cf. sk_log().
 */
#define sk_trace(...) sk_log(SK_LOG_TRACE, __VA_ARGS__)

#endif