/
log.h
209 lines (186 loc) · 5.84 KB
/
log.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
/**
* @section License
*
* The MIT License (MIT)
*
* Copyright (c) 2014-2016, Erik Moqvist
*
* 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.
*
* This file is part of the Simba project.
*/
#ifndef __DEBUG_LOG_H__
#define __DEBUG_LOG_H__
#include "simba.h"
/* Severity levels' log masks. */
/** An unhandleable error that results in a program crash. */
#define LOG_FATAL 0
/** A handable error conditions. */
#define LOG_ERROR 1
/** A warning. */
#define LOG_WARNING 2
/** Generic (useful) information about system operation. */
#define LOG_INFO 3
/** Developer debugging messages. */
#define LOG_DEBUG 4
/** Craete a log mask with given level set. */
#define LOG_MASK(level) (1 << (LOG_ ## level))
/** Set all levels up to and including given level. */
#define LOG_UPTO(level) ((1 << (LOG_ ## level + 1)) - 1)
/** Set all levels. */
#define LOG_ALL LOG_UPTO(DEBUG)
/** Clear all levels. */
#define LOG_NONE 0x00
struct log_handler_t {
void *chout_p;
struct log_handler_t *next_p;
};
struct log_object_t {
const char *name_p;
char mask;
struct log_object_t *next_p;
};
/**
* Initialize the logging module. This function must be called before
* calling any other function in this module.
*
* The module will only be initialized once even if this function is
* called multiple times.
*
* @return zero(0) or negative error code.
*/
int log_module_init(void);
/**
* Initialize given log object with given name and mask.
*
* @param[in] self_p Log object to initialize.
* @param[in] name_p Log object name.
* @param[in] mask Log object mask.
*
* @return zero(0) or negative error code.
*/
int log_object_init(struct log_object_t *self_p,
const char *name_p,
char mask);
/**
* Set given log mask for given log object.
*
* @param[in] self_p Log object.
* @param[in] mask Log object mask.
*
* @return zero(0) or negative error code.
*/
int log_object_set_log_mask(struct log_object_t *self_p,
char mask);
/**
* Get the log mask of given log object.
*
* @param[in] self_p Log object.
*
* @return Log mask.
*/
char log_object_get_log_mask(struct log_object_t *self_p);
/**
* Check if given log level is enabled in given log object.
*
* @param[in] self_p Log object, or NULL to check the level in the
* thread log mask.
* @param[in] level Log level to check.
*
* @return true(1) if given log level is enabled, false(0) if given
* log level is disabled, otherwise negative error code.
*/
int log_object_is_enabled_for(struct log_object_t *self_p,
int level);
/**
* Check if given log level is set in the log object mask. If so,
* format a log entry and write it to all log handlers.
*
* ``self_p`` may be NULL, and in that case the current thread's log
* mask is used instead of the log object mask.
*
* @param[in] self_p Log object, or NULL to use the thread's log mask.
* @param[in] level Log level.
* @param[in] fmt_p Log format string.
* @param[in] ... Variable argument list.
*
* @return zero(0) or negative error code.
*/
int log_object_print(struct log_object_t *self_p,
int level,
const char *fmt_p,
...);
/**
* Initialize given log handler with given output channel.
*
* @param[in] self_p Log handler to initialize.
* @param[in] chout_p Output handler.
*
* @return zero(0) or negative error code.
*/
int log_handler_init(struct log_handler_t *self_p,
void *chout_p);
/**
* Add given log handler to the list of log handlers. Log entries will
* be written to all log handlers in the list.
*
* @param[in] handler_p Log handler to add.
*
* @return zero(0) or negative error code.
*/
int log_add_handler(struct log_handler_t *handler_p);
/**
* Remove given log handler from the list of log handlers.
*
* @param[in] handler_p Log handler to remove.
*
* @return zero(0) or negative error code.
*/
int log_remove_handler(struct log_handler_t *handler_p);
/**
* Add given log object to the list of log objects. There are file
* system commands to list all log objects in the list and also modify
* their log mask.
*
* @param[in] object_p Log object to add.
*
* @return zero(0) or negative error code.
*/
int log_add_object(struct log_object_t *object_p);
/**
* Remove given log object from the list of log objects.
*
* @param[in] object_p Object to remove.
*
* @return zero(0) or negative error code.
*/
int log_remove_object(struct log_object_t *object_p);
/**
* Set the output channel of the default log handler.
*
* @param[in] chout_p Channel to set as the default output
* channel. May be NULL if no output should be
* written.
*
* @return zero(0) or negative error code.
*/
int log_set_default_handler_output_channel(void *chout_p);
#endif