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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
|
/*
* Linux WiMax
* log helpers
*
*
* Copyright (C) 2009 Intel Corporation. All rights reserved.
* Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* * Neither the name of Intel Corporation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdlib.h>
#define W_VERBOSITY W_ERROR
#include <wimaxll/log.h>
#include "internal.h"
/**
* Deliver \e libwimaxll diagnostics messages to \e stderr or \e stdout.
*
* \param wmx WiMAX handle this message is related to
* \param level Message level
* \param header Header for the message; the implementation must
* decide if it has to be printed or not.
* \param fmt printf-like format
* \param vargs variable-argument list as created by
* stdargs.h:va_list() that will be formatted according to \e
* fmt.
*
* Default diagnostics printing function. If the log level is
* "W_PRINT", we put it on stdout without a header, otherwise in
* stderr with header.
*
* \ingroup helper_log
*/
void wimaxll_vlmsg_default(struct wimaxll_handle *wmx, unsigned level,
const char *header,
const char *fmt, va_list vargs)
{
FILE *f = level != W_PRINT? stderr : stdout;
if (level == W_PRINT)
f = stdout;
else {
f = stderr;
fprintf(f, header);
}
vfprintf(f, fmt, vargs);
}
/**
* Print library diagnostics messages [backend]
*
* @param wmx WiMAX handle this message is related to
* @param level Message level
* @param header Header to print for the message (the implementation
* must decide if to print it or not).
* @param fmt printf-like format
* @param vargs variable-argument list as created by
* stdargs.h:va_list() that will be formatted according to \e
* fmt.
*
* Prints/writes the \e libwimaxll's diagnostics messages to a
* destination as selected by the user of the library.
*
* \note This function pointer must be set \b before calling any other
* \e libwimaxll function.
*
* By default, diagnostics are printed with wimaxll_vlmsg_default() to
* \a stderr or stdout based on the level.
*
* For example, to deliver diagnostics to syslog:
*
* @code
* #include <syslog.h>
* ...
* static
* void wimaxll_vlmsg_syslog(....const char *fmt, va_list vargs)
* {
* syslog(LOG_MAKEPRI(LOG_USER, LOG_INFO), header);
* vsyslog(LOG_MAKEPRI(LOG_USER, LOG_INFO), fmt, vargs);
* }
* ...
* wimaxll_vlmsg = wimaxll_vlmsg_syslog();
* ...
* wimaxll_open(BLAH);
* @endcode
*
* The internal function wimaxll_msg() and wimaxll_lmsg() are used as
* as a frontend to this function.
*
* \ingroup helper_log
*/
void (*wimaxll_vlmsg_cb)(struct wimaxll_handle *wmx, unsigned level,
const char *header,
const char *fmt, va_list vargs) =
wimaxll_vlmsg_default;
/**
* Default header for diagnostic messages
*
* If there is no handle, prints just "libwimaxll", otherwise
* "libwimaxll[device]"; if the message is debug, also adds a
* "(@ FUNCTION:LINE)" origin tag.
*
* \ingroup helper_log
*/
void wimaxll_msg_hdr_default(char *buf, size_t buf_len,
struct wimaxll_handle *wmx, enum w_levels level,
const char *origin_str, unsigned origin_line)
{
size_t bytes;
if (wmx == NULL)
bytes = snprintf(buf, buf_len, "libwimaxll: ");
else if ((unsigned long) wmx < 4096)
bytes = snprintf(buf, buf_len, "libwimaxll[bad handle %p]: ",
wmx);
else
bytes = snprintf(buf, buf_len, "libwimaxll[%s]: ", wmx->name);
if (level >= W_D0 && origin_str != NULL)
snprintf(buf + bytes, buf_len - bytes,
"(@ %s:%u) ", origin_str, origin_line);
}
/**
* Create a header for library diagnostic messages [backend]
*
* @param buf Buffer where to place the header
* @param buf_len Size of the buffer
* @param wmx WiMAX handle the message is being generated for (can be
* NULL or invalid).
* @param level Level of the log message this header is being
* generated for.
* @param origin_str Origin of the message (used for a source file
* name or function name). If %NULL, the origin information should
* not be considered.
* @param origin_line Origin of the message (used for a line in a
* source file or function).
*
* Creates a header to prefix to ever message printed with
* wimaxll_msg().
*
* By default, diagnostics are printed with wimaxll_msg_hdr_default()
* is used, which creates a "libwimaxll[DEVICENAME]" prefix.
*
* To change it:
*
* @code
* ...
* static
* void my_wimaxll_msg_hdr(char *buf, size_t len, struct
* wimaxll_handle *wmx)
* {
* snprintf(buf, len, "my prefix: ");
* }
* ...
* wimaxll_msg_hdr = my_wimaxll_msg_hdr;
* ...
* wimaxll_open(BLAH);
* @endcode
*
* \ingroup helper_log
*/
void (*wimaxll_msg_hdr_cb)(char *buf, size_t buf_len,
struct wimaxll_handle *wmx, enum w_levels level,
const char *origin_str, unsigned origin_line) =
wimaxll_msg_hdr_default;
static
void wimaxll_vlmsg(unsigned level, unsigned current_level,
const char *origin_str, unsigned origin_int,
struct wimaxll_handle *wmx, const char *fmt, va_list vargs)
{
char header[64] = "";
if (level > current_level && level != W_PRINT)
return;
if (wimaxll_msg_hdr_cb)
wimaxll_msg_hdr_cb(header, sizeof(header), wmx,
level, origin_str, origin_int);
wimaxll_vlmsg_cb(wmx, level, header, fmt, vargs);
}
/**
* Prints library diagnostic messages with a predefined format [frontend]
*
* @param wmx WiMAX handle; if NULL, no device header will be presented.
* @param fmt printf-like format followed by any arguments
*
* Called by the library functions to print status/error messages. By
* default these are sent over to stderr.
*
* However, library users can change this default behaviour by setting
* wimaxll_vmsg() as documented in that function pointer's
* documentation.
*
* \ingroup helper_log
*/
void wimaxll_msg(struct wimaxll_handle *wmx, const char *fmt, ...)
{
va_list vargs;
va_start(vargs, fmt);
wimaxll_vlmsg(W_PRINT, W_PRINT, NULL, 0, wmx, fmt, vargs);
va_end(vargs);
}
/**
* Prints library diagnostic messages with a predefined format
* [frontend] and log level control
*
* @param level level of the messagve
* @param current_level current logging level
* @param origin_str Origin of the message (used for a source file
* name or function name).
* @param origin_line Origin of the message (used for a line in a
* source file or function).
* @param wmx WiMAX handle; if NULL, no device header will be presented.
* @param fmt printf-like format followed by any arguments
*
* Called by the library functions to print status/error messages if
* the current log level allows it. By default these are sent over to
* stderr.
*
* However, library users can change this default behaviour by setting
* wimaxll_vmsg() as documented in that function pointer's
* documentation.
*
* \ingroup helper_log
*/
void wimaxll_lmsg(unsigned level, unsigned current_level,
const char *origin_str, unsigned origin_line,
struct wimaxll_handle *wmx, const char *fmt, ...)
{
va_list vargs;
va_start(vargs, fmt);
wimaxll_vlmsg(level, current_level, origin_str, origin_line,
wmx, fmt, vargs);
va_end(vargs);
}
/**
* Log an error message to stderr and abort
*
* \param result exit code to abort with
* \param fmt: printf-like format string and its arguments
*
* @ingroup: helper_log
*/
void w_abort(int result, const char *fmt, ...)
{
va_list vargs;
va_start(vargs, fmt);
wimaxll_vlmsg(W_ERROR, W_ERROR, __FILE__, __LINE__, NULL, fmt, vargs);
va_end(vargs);
exit(result);
}
/* These are defined in the header file */
/**
* Log a printf-like error message to stderr
*
* @fn w_error(fmt...)
* @ingroup helper_log
*/
/**
* Log a printf-like warning message to stderr
*
* @fn w_warn(fmt...)
* @ingroup helper_log
*/
/**
* Log a printf-like error message to stderr
*
* @fn w_info(fmt...)
* @ingroup helper_log
*/
/**
* Log a printf-like error message to stdout
*
* @fn w_print(fmt...)
* @ingroup helper_log
*/
/**
* Log a printf-like debug message (level 0)
*
* @fn w_d0(fmt...)
* @ingroup helper_log
*/
/**
* Log a printf-like debug message (level 1)
*
* @fn w_d1(fmt...)
* @ingroup helper_log
*/
/**
* Log a printf-like debug message (level 2)
*
* @fn w_d2(fmt...)
* @ingroup helper_log
*/
/**
* Log a printf-like debug message (level 3)
*
* @fn w_d3(fmt...)
* @ingroup helper_log
*/
/**
* Log a printf-like debug message (level 4)
*
* @fn w_d4(fmt...)
* @ingroup helper_log
*/
/**
* Log a printf-like debug message (level 5)
*
* @fn w_d5(fmt...)
* @ingroup helper_log
*/
/**
* Log a printf-like debug message (level 6)
*
* @fn w_d6(fmt...)
* @ingroup helper_log
*/
/**
* Log a printf-like debug message (level 7)
*
* @fn w_d7(fmt...)
* @ingroup helper_log
*/
|