-
Notifications
You must be signed in to change notification settings - Fork 0
/
gajeta.h
227 lines (181 loc) · 6.82 KB
/
gajeta.h
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
//
// Copyright (c) 2013-2015, John Mettraux, [email protected]
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// Made in Japan.
//
// https://github.com/flon-io/gajeta
// gajeta.h
#ifndef FLON_GAJETA_H
#define FLON_GAJETA_H
#include <stdio.h>
#include <stdarg.h>
#include "flutil.h"
#define FGAJ_VERSION "1.0.0"
// 10 't' trace
// 20 'd' debug
// 30 'i' info
// 40 'w' warn
// 50 'e' error
/* "subjecter" function type.
* Returns the length of the "subject" written to buffer or
* -1 in case of error.
*/
typedef ssize_t fgaj_subjecter(
char *buffer, size_t off,
const char *file, int line, const char *func, const void *subject);
/* Logger function type.
*/
typedef void fgaj_logger(
char level, const char *subject, const char *msg);
//
// configuration
typedef struct fgaj_conf {
char color; // 't', 'T', 'f' for true, True and false respectively
char level; // defaults to 30 (info)
short utc; // 1 = true, defaults to 0
char *host; // defaults to result of gethostname()
fgaj_subjecter *subjecter; // "subjecter" function
fgaj_logger *logger; // logger function
void *out; // logging destination
short flush; // defaults to 0, when 1 will flush after each log
void *params; // whatever suits the logger func
size_t subject_maxlen; // defaults to 256 (-1)
size_t message_maxlen; // defaults to 1024 (-1)
} fgaj_conf;
/* Returns the configuration global var.
*/
fgaj_conf *fgaj_conf_get();
/* Resets the logger. Used for testing.
*/
void fgaj_conf_reset();
/* Reads the env and sets the configuration accordingly.
* Is called behind the scene, but is available here for cases when
* one wants to do custom configuration and then give the env the
* last word (by calling this method).
*/
void fgaj_read_env();
//
// "subjecters"
/* Default subjecter function.
*/
ssize_t fgaj_default_subjecter(
char *buffer, size_t off,
const char *file, int line, const char *func, const void *subject);
//
// loggers
/* Default logger function.
*/
void fgaj_color_file_logger(
char level, const char *subject, const char *msg);
/* Simple logger function. Used for testing. Logs to a char[] placed in
* fgaj_conf_get()->out
*/
void fgaj_string_logger(
char level, const char *subject, const char *msg);
/* Logger for specs, logging in grey, towards the right side of the screen.
*/
void fgaj_grey_logger(
char level, const char *subject, const char *msg);
//
// logging functions (and macros)
/* A raw logging method, accepts the level as a char ('e', 'w', 'd', ...) or
* as a number (10, 20, ...) and the pref / msg.
*
* It accepts 'r' as a [virtual] level as well (like 'e' but appends the
* result of strerror(errno) to the message)
*
* When `short` is 1, it will log `errno`'s text value as well (akin
* to `perror()`.
*/
void fgaj_log(
char level, short err,
const char *file, int line, const char *func, const void *subject,
const char *format, ...);
// the ellipsis in there cover "format and ellipsis"...
#define fgaj_t(...) \
fgaj_log('t', 0, __FILE__, __LINE__, __func__, NULL, __VA_ARGS__)
#define fgaj_d(...) \
fgaj_log('d', 0, __FILE__, __LINE__, __func__, NULL, __VA_ARGS__)
#define fgaj_i(...) \
fgaj_log('i', 0, __FILE__, __LINE__, __func__, NULL, __VA_ARGS__)
#define fgaj_w(...) \
fgaj_log('w', 0, __FILE__, __LINE__, __func__, NULL, __VA_ARGS__)
#define fgaj_e(...) \
fgaj_log('e', 0, __FILE__, __LINE__, __func__, NULL, __VA_ARGS__)
#define fgaj_r(...) \
fgaj_log('r', 1, __FILE__, __LINE__, __func__, NULL, __VA_ARGS__)
#define fgaj_l(level, ...) \
fgaj_log(level, level == 'r', __FILE__, __LINE__, __func__, NULL, __VA_ARGS__)
#define fgaj_ll(level, subject, ...) \
fgaj_log(level, level == 'r', subject, -1, NULL, NULL, __VA_ARGS__)
#define fgaj_tr(...) \
fgaj_log('t', 1, __FILE__, __LINE__, __func__, NULL, __VA_ARGS__)
#define fgaj_dr(...) \
fgaj_log('d', 1, __FILE__, __LINE__, __func__, NULL, __VA_ARGS__)
#define fgaj_ir(...) \
fgaj_log('i', 1, __FILE__, __LINE__, __func__, NULL, __VA_ARGS__)
#define fgaj_wr(...) \
fgaj_log('w', 1, __FILE__, __LINE__, __func__, NULL, __VA_ARGS__)
#define fgaj_st(...) \
fgaj_log('t', 0, __FILE__, __LINE__, __func__, __VA_ARGS__)
#define fgaj_sd(...) \
fgaj_log('d', 0, __FILE__, __LINE__, __func__, __VA_ARGS__)
#define fgaj_si(...) \
fgaj_log('i', 0, __FILE__, __LINE__, __func__, __VA_ARGS__)
#define fgaj_sw(...) \
fgaj_log('w', 0, __FILE__, __LINE__, __func__, __VA_ARGS__)
#define fgaj_se(...) \
fgaj_log('e', 0, __FILE__, __LINE__, __func__, __VA_ARGS__)
#define fgaj_sr(...) \
fgaj_log('r', 1, __FILE__, __LINE__, __func__, __VA_ARGS__)
#define fgaj_str(...) \
fgaj_log('t', 1, __FILE__, __LINE__, __func__, __VA_ARGS__)
#define fgaj_sdr(...) \
fgaj_log('d', 1, __FILE__, __LINE__, __func__, __VA_ARGS__)
#define fgaj_sir(...) \
fgaj_log('i', 1, __FILE__, __LINE__, __func__, __VA_ARGS__)
#define fgaj_swr(...) \
fgaj_log('w', 1, __FILE__, __LINE__, __func__, __VA_ARGS__)
#define fgaj_sl(level, ...) \
fgaj_log(level, level == 'r', __FILE__, __LINE__, __func__, __VA_ARGS__)
//
// helper functions
//
// they are only relevant for people implementing logger functions.
/* Turns a level char like 'i', 'W', 20 to a string like "INFO", "WARN",
* "DEBUG", respectively.
*/
char *fgaj_level_to_string(char level);
/* Use this function to free the string obtained from fgaj_level_to_string().
* This function only frees if the result is from the heap.
*/
void fgaj_level_string_free(char *s);
/* Turns a level char like 'i', 'W', ... into a numeric level like 30, 40, ...
*/
char fgaj_normalize_level(char level);
/* Turns a string like 'i', 'info', 'INFO', '30', ... into a numeric level
* like 30, 40, ...
*/
char fgaj_parse_level(char *s);
/* Returns a (malloc'ed) string detailing the current time.
*/
char *fgaj_now();
#endif // FLON_GAJETA_H