aboutsummaryrefslogtreecommitdiffstats
path: root/include/embedlog.h.in
blob: 0ee95f06499341179a1fad5af88c6bdb7ab9d4e7 (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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/* ==========================================================================
    Licensed under BSD 2clause license. See LICENSE file for more information
    Author: Michał Łyszczek <michal.lyszczek@bofc.pl>
   ========================================================================== */


#ifndef EMBEDLOG_H
#define EMBEDLOG_H 1

#include <stddef.h>
#include <stdarg.h>
#include <stdio.h>

#ifdef __cplusplus
extern "C" {
#endif

#ifdef NOFINFO
#   define ELF NULL, 0, NULL, EL_FATAL
#   define ELA NULL, 0, NULL, EL_ALERT
#   define ELC NULL, 0, NULL, EL_CRIT
#   define ELE NULL, 0, NULL, EL_ERROR
#   define ELW NULL, 0, NULL, EL_WARN
#   define ELN NULL, 0, NULL, EL_NOTICE
#   define ELI NULL, 0, NULL, EL_INFO
#   define ELD NULL, 0, NULL, EL_DBG

#   define OELF NULL, 0, NULL, EL_FATAL, EL_OPTIONS_OBJECT
#   define OELA NULL, 0, NULL, EL_ALERT, EL_OPTIONS_OBJECT
#   define OELC NULL, 0, NULL, EL_CRIT, EL_OPTIONS_OBJECT
#   define OELE NULL, 0, NULL, EL_ERROR, EL_OPTIONS_OBJECT
#   define OELW NULL, 0, NULL, EL_WARN, EL_OPTIONS_OBJECT
#   define OELN NULL, 0, NULL, EL_NOTICE, EL_OPTIONS_OBJECT
#   define OELI NULL, 0, NULL, EL_INFO, EL_OPTIONS_OBJECT
#   define OELD NULL, 0, NULL, EL_DBG, EL_OPTIONS_OBJECT
#else
#   define ELF __FILE__, __LINE__, EL_FUNC_NAME, EL_FATAL
#   define ELA __FILE__, __LINE__, EL_FUNC_NAME, EL_ALERT
#   define ELC __FILE__, __LINE__, EL_FUNC_NAME, EL_CRIT
#   define ELE __FILE__, __LINE__, EL_FUNC_NAME, EL_ERROR
#   define ELW __FILE__, __LINE__, EL_FUNC_NAME, EL_WARN
#   define ELN __FILE__, __LINE__, EL_FUNC_NAME, EL_NOTICE
#   define ELI __FILE__, __LINE__, EL_FUNC_NAME, EL_INFO
#   define ELD __FILE__, __LINE__, EL_FUNC_NAME, EL_DBG

#   define OELF __FILE__, __LINE__, EL_FUNC_NAME, EL_FATAL, EL_OPTIONS_OBJECT
#   define OELA __FILE__, __LINE__, EL_FUNC_NAME, EL_ALERT, EL_OPTIONS_OBJECT
#   define OELC __FILE__, __LINE__, EL_FUNC_NAME, EL_CRIT, EL_OPTIONS_OBJECT
#   define OELE __FILE__, __LINE__, EL_FUNC_NAME, EL_ERROR, EL_OPTIONS_OBJECT
#   define OELW __FILE__, __LINE__, EL_FUNC_NAME, EL_WARN, EL_OPTIONS_OBJECT
#   define OELN __FILE__, __LINE__, EL_FUNC_NAME, EL_NOTICE, EL_OPTIONS_OBJECT
#   define OELI __FILE__, __LINE__, EL_FUNC_NAME, EL_INFO, EL_OPTIONS_OBJECT
#   define OELD __FILE__, __LINE__, EL_FUNC_NAME, EL_DBG, EL_OPTIONS_OBJECT
#endif

#if (__STDC_VERSION__ >= 199901L)
    /* options for c99 users, they can affort more */
#   define EL_FUNC_NAME __func__
#   ifdef NDEBUG
#       define EL_DEBUG(...) ((void)0)
#   else
#       define EL_DEBUG(...) el_print(ELD, __VA_ARGS__)
#   endif
#else
#   define EL_FUNC_NAME NULL
#endif

/* enum values, new enums can be added, but values of already
 * existing enums *must* stay unchanged, to keep ABI stable!
 */

enum el_output
{
    EL_OUT_NONE   = 0x0000,
    EL_OUT_STDERR = 0x0001,
    EL_OUT_STDOUT = 0x0002,
    EL_OUT_SYSLOG = 0x0004,
    EL_OUT_FILE   = 0x0008,
    EL_OUT_NET    = 0x0010,
    EL_OUT_TTY    = 0x0020,
    EL_OUT_CUSTOM = 0x0040,
    EL_OUT_ALL    = 0x7fff
};

enum el_level
{
    EL_FATAL  = 0,
    EL_ALERT  = 1,
    EL_CRIT   = 2,
    EL_ERROR  = 3,
    EL_WARN   = 4,
    EL_NOTICE = 5,
    EL_INFO   = 6,
    EL_DBG    = 7
};

enum el_option
{
    EL_LEVEL          = 0,
    EL_OUT            = 1,
    EL_COLORS         = 2,
    EL_TS             = 3,
    EL_TS_TM          = 4,
    EL_TS_FRACT       = 5,
    EL_PRINT_LEVEL    = 6,
    EL_PRINT_NL       = 7,
    EL_FINFO          = 8,
    EL_FUNCINFO       = 9,
    EL_CUSTOM_PUTS    = 10,
    EL_TTY_DEV        = 11,
    EL_PREFIX         = 12,

    EL_FPATH          = 13,
    EL_FROTATE_NUMBER = 14,
    EL_FROTATE_SIZE   = 15,
    EL_FSYNC_EVERY    = 16,
    EL_FSYNC_LEVEL    = 17,

    /* internal use only, should represent number of options, if
     * you add option, increment this by one (or by the number of
     * options you add).
     */

    EL_OPT_ERROR      = 18
};

enum el_option_timestamp
{
    EL_TS_OFF   = 0,
    EL_TS_SHORT = 1,
    EL_TS_LONG  = 2,

    /* internal use only, should represent number of options, if
     * you add option, increment this by one (or by the number of
     * options you add).
     */

    EL_TS_ERROR = 3
};

enum el_option_timestamp_timer
{
    EL_TS_TM_TIME      = 0,
    EL_TS_TM_CLOCK     = 1,
    EL_TS_TM_REALTIME  = 2,
    EL_TS_TM_MONOTONIC = 3,

    /* internal use only, should represent number of options, if
     * you add option, increment this by one (or by the number of
     * options you add).
     */

    EL_TS_TM_ERROR     = 4
};

enum el_option_timestamp_fractions
{
    EL_TS_FRACT_OFF   = 0,
    EL_TS_FRACT_MS    = 1,
    EL_TS_FRACT_US    = 2,
    EL_TS_FRACT_NS    = 3,

    /* internal use only, should represent number of options, if
     * you add option, increment this by one (or by the number of
     * options you add).
     */

    EL_TS_FRACT_ERROR = 4
};

typedef int (*el_custom_puts)(const char *s, void *user);

/* Main embedlog object that holds printing options and other data.
 * This definition is here so people that don't care about stable
 * ABI can avoid mallocs and allocate objects on stack. Check
 * el_overview(7) for more information about this.
 *
 * Any #if [0,1] in this struct is generated during compilation and
 * should no be changed without library recompilation, unless you
 * like when your program executes undefined behaviour.
 *
 * It's generated as #if [0,1] so this is applied system-wide, if
 * it were to be defined like #if ENABLE_OUT_FILE, then every
 * compilation unit that uses embedlog would have to define that to
 * either 1 or 0. Easy for mistakes and hard to find bugs.
 */
struct el
{
    unsigned int    outputs:7;
    unsigned int    colors:1;
    unsigned int    timestamp:2;
    unsigned int    timestamp_timer:3;
    unsigned int    timestamp_fractions:2;
    unsigned int    print_log_level:1;
    unsigned int    print_newline:1;
    unsigned int    finfo:1;
    unsigned int    funcinfo:1;
    unsigned int    level:3;
    unsigned int    level_current_msg:3;

#if @ENABLE_OUT_FILE@
    unsigned int    fsync_level:3;
    unsigned int    frotate_number;
    unsigned int    fcurrent_rotate;
    unsigned long   frotate_size;
    unsigned long   file_sync_every;
    unsigned long   written_after_sync;
    unsigned long   fpos;
    FILE           *file;
    char           *current_log;
    const char     *fname;
#endif

#if @ENABLE_OUT_TTY@
    int             serial_fd;
#endif

#if @ENABLE_PREFIX@
    const char     *prefix;
#endif

#if @ENABLE_OUT_CUSTOM@
    el_custom_puts  custom_puts;
    void           *custom_puts_user;
#endif
};

int el_init(void);
int el_cleanup(void);
int el_option(int option, ...);
int el_puts(const char *string);
int el_print(const char *file, size_t line, const char *func,
        enum el_level level, const char *fmt, ...);
int el_vprint(const char *file, size_t line, const char *func,
        enum el_level level, const char *fmt, va_list ap);
int el_pmemory(const char *file, size_t line, const char *func,
        enum el_level level, const void *memory, size_t mlen);
int el_pmemory_table(const char *file, size_t line, const char *func,
        enum el_level level, const void *memory, size_t mlen);
int el_perror(const char *file, size_t line, const char *func,
        enum el_level level, const char *fmt, ...);
int el_putb(const void *memory, size_t mlen);
int el_pbinary(enum el_level level, const void *memory, size_t mlen);
const struct el *el_get_el(void);
int el_flush(void);

struct el * el_new(void);
int el_destroy(struct el *el);

int el_oinit(struct el *el);
int el_ocleanup(struct el *el);
int el_ooption(struct el *el, int option, ...);
int el_oputs(struct el *el, const char *string);
int el_oprint(const char *file, size_t line, const char *func,
        enum el_level level, struct el *el, const char *fnt, ...);
int el_ovprint(const char *file, size_t line, const char *func,
        enum el_level level, struct el *el, const char *fmt,
        va_list ap);
int el_opmemory(const char *file, size_t line, const char *func,
        enum el_level level, struct el *el, const void *memory,
        size_t mlen);
int el_opmemory_table(const char *file, size_t line, const char *func,
        enum el_level level, struct el *el, const void *memory,
        size_t mlen);
int el_operror(const char *file, size_t line, const char *func,
        enum el_level level, struct el *el, const char *fmt, ...);
int el_oputb(struct el *el, const void *memory, size_t mlen);
int el_opbinary(enum el_level level, struct el *el,
        const void *memory, size_t mlen);
int el_oflush(struct el *el);

#ifdef __cplusplus
}
#endif

#endif