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
|
/* Copyright (C) 2010-2013 Mamadou Diop.
* Copyright (C) 2013 Doubango Telecom <http://doubango.org>
* This file is part of Open Source Doubango Framework.
*
* DOUBANGO is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* DOUBANGO is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with DOUBANGO.
*
*/
/**@file tsk_debug.h
* @brief Useful functions for debugging purpose.
*/
#ifndef _TINYSAK_DEBUG_H_
#define _TINYSAK_DEBUG_H_
#include "tinysak_config.h"
#include <stdio.h>
TSK_BEGIN_DECLS
#if !defined(DEBUG_LEVEL)
# define DEBUG_LEVEL DEBUG_LEVEL_ERROR
#endif
/**@ingroup tsk_debug_group
* @def DEBUG_LEVEL
* Defines the defaul debug level. Sould be: @ref DEBUG_LEVEL_INFO, @ref DEBUG_LEVEL_WARN, @ref DEBUG_LEVEL_ERROR or @ref DEBUG_LEVEL_FATAL. <br />
* You can set this value at build time using your CFLAGS (e.g. <i>CFLAGS=$CFLAGS -DDEBUG_LEVEL=DEBUG_LEVEL_ERROR</i>). <br />
* At runtime, this value could be changed using @ref tsk_debug_set_level().
*/
/**@ingroup tsk_debug_group
* @def DEBUG_LEVEL_INFO
* @a INFO level (4). This is the lowest possible level and will turn on all logging.
*/
/**@ingroup tsk_debug_group
* @def DEBUG_LEVEL_WARN
* @a WARN level (3). Warning are error which could change the normal process without blocking the application.
*/
/**@ingroup tsk_debug_group
* @def DEBUG_LEVEL_ERROR
* @a ERROR level (2). This level logs error which might change the application behavior.
*/
/**@ingroup tsk_debug_group
* @def DEBUG_LEVEL_FATAL
* @a FATAL level (1). This level logs fatal errors which might abort the application.
*/
/**@ingroup tsk_debug_group
* @def TSK_DEBUG_INFO
* Writes the C string pointed by @a FMT to <b>stderr</b> output if debug level is >= 4 (@ref DEBUG_LEVEL_INFO) and no callack function is defined using @ref tsk_debug_set_info_cb. <br />
* <i>Example</i>:
* @code
* TSK_DEBUG_INFO("Project name = %s, version = %f", "Doubango", 3.0);
* @endcode
* @param FMT C string that contains the text to be written to <b>stderr</b>. The string accept same specifiers as <a target=_blank href="http://www.cplusplus.com/reference/cstdio/printf/">printf</a>.
* @sa @ref TSK_DEBUG_WARN @ref TSK_DEBUG_ERROR @ref TSK_DEBUG_FATAL <br />
*/
/**@ingroup tsk_debug_group
* @def TSK_DEBUG_WARN
* Writes the C string pointed by @a FMT to <b>stderr</b> output if debug level is >= 4 (@ref DEBUG_LEVEL_WARN) and no callack function is defined using @ref tsk_debug_set_warn_cb. <br />
* <i>Example</i>:
* @code
* TSK_DEBUG_WARN("Warning code = %d, descripting = %s", 404, "Not found");
* @endcode
* @param FMT C string that contains the text to be written to <b>stderr</b>. The string accept same specifiers as <a target=_blank href="http://www.cplusplus.com/reference/cstdio/printf/">printf</a>.
* @sa @ref TSK_DEBUG_INFO @ref TSK_DEBUG_ERROR @ref TSK_DEBUG_FATAL <br />
*/
/**@ingroup tsk_debug_group
* @def TSK_DEBUG_ERROR
* Writes the C string pointed by @a FMT to <b>stderr</b> output if debug level is >= 4 (@ref DEBUG_LEVEL_ERROR) and no callack function is defined using @ref tsk_debug_set_error_cb. <br />
* <i>Example</i>:
* @code
* TSK_DEBUG_ERROR("Warning code = %d, descripting = %s", 400, "Bad request");
* @endcode
* @param FMT C string that contains the text to be written to <b>stderr</b>. The string accept same specifiers as <a target=_blank href="http://www.cplusplus.com/reference/cstdio/printf/">printf</a>.
* @sa @ref TSK_DEBUG_INFO @ref TSK_DEBUG_WARN @ref TSK_DEBUG_FATAL <br />
*/
/**@ingroup tsk_debug_group
* @def TSK_DEBUG_FATAL
* Writes the C string pointed by @a FMT to <b>stderr</b> output if debug level is >= 4 (@ref DEBUG_LEVEL_FATAL) and no callack function is defined using @ref tsk_debug_set_fatal_cb. <br />
* <i>Example</i>:
* @code
* TSK_DEBUG_ERROR("Warning code = %d, descripting = %s", 403, "Forbidden");
* @endcode
* @param FMT C string that contains the text to be written to <b>stderr</b>. The string accept same specifiers as <a target=_blank href="http://www.cplusplus.com/reference/cstdio/printf/">printf</a>.
* @sa @ref TSK_DEBUG_INFO @ref TSK_DEBUG_WARN @ref TSK_DEBUG_ERROR <br />
*/
#define DEBUG_LEVEL_INFO 4
#define DEBUG_LEVEL_WARN 3
#define DEBUG_LEVEL_ERROR 2
#define DEBUG_LEVEL_FATAL 1
#if TSK_HAVE_DEBUG_H
# include <my_debug.h>
#else
typedef int (*tsk_debug_f)(const void* arg, const char* fmt, ...);
/* INFO */
#define TSK_DEBUG_INFO(FMT, ...) \
if(tsk_debug_get_level() >= DEBUG_LEVEL_INFO){ \
if(tsk_debug_get_info_cb()) \
tsk_debug_get_info_cb()(tsk_debug_get_arg_data(), "*[DOUBANGO INFO]: " FMT "\n", ##__VA_ARGS__); \
else \
fprintf(stderr, "*[DOUBANGO INFO]: " FMT "\n", ##__VA_ARGS__); \
}
/* WARN */
#define TSK_DEBUG_WARN(FMT, ...) \
if(tsk_debug_get_level() >= DEBUG_LEVEL_WARN){ \
if(tsk_debug_get_warn_cb()) \
tsk_debug_get_warn_cb()(tsk_debug_get_arg_data(), "**[DOUBANGO WARN]: function: \"%s()\" \nfile: \"%s\" \nline: \"%u\" \nMSG: " FMT "\n", __FUNCTION__, __FILE__, __LINE__, ##__VA_ARGS__); \
else \
fprintf(stderr, "**[DOUBANGO WARN]: function: \"%s()\" \nfile: \"%s\" \nline: \"%u\" \nMSG: " FMT "\n", __FUNCTION__, __FILE__, __LINE__, ##__VA_ARGS__); \
}
/* ERROR */
#define TSK_DEBUG_ERROR(FMT, ...) \
if(tsk_debug_get_level() >= DEBUG_LEVEL_ERROR){ \
if(tsk_debug_get_error_cb()) \
tsk_debug_get_error_cb()(tsk_debug_get_arg_data(), "***[DOUBANGO ERROR]: function: \"%s()\" \nfile: \"%s\" \nline: \"%u\" \nMSG: " FMT "\n", __FUNCTION__, __FILE__, __LINE__, ##__VA_ARGS__); \
else \
fprintf(stderr, "***[DOUBANGO ERROR]: function: \"%s()\" \nfile: \"%s\" \nline: \"%u\" \nMSG: " FMT "\n", __FUNCTION__, __FILE__, __LINE__, ##__VA_ARGS__); \
}
/* FATAL */
#define TSK_DEBUG_FATAL(FMT, ...) \
if(tsk_debug_get_level() >= DEBUG_LEVEL_FATAL){ \
if(tsk_debug_get_fatal_cb()) \
tsk_debug_get_fatal_cb()(tsk_debug_get_arg_data(), "****[DOUBANGO FATAL]: function: \"%s()\" \nfile: \"%s\" \nline: \"%u\" \nMSG: " FMT "\n", __FUNCTION__, __FILE__, __LINE__, ##__VA_ARGS__); \
else \
fprintf(stderr, "****[DOUBANGO FATAL]: function: \"%s()\" \nfile: \"%s\" \nline: \"%u\" \nMSG: " FMT "\n", __FUNCTION__, __FILE__, __LINE__, ##__VA_ARGS__); \
}
TINYSAK_API void tsk_debug_set_arg_data(const void*);
TINYSAK_API const void* tsk_debug_get_arg_data();
TINYSAK_API void tsk_debug_set_info_cb(tsk_debug_f );
TINYSAK_API tsk_debug_f tsk_debug_get_info_cb();
TINYSAK_API void tsk_debug_set_warn_cb(tsk_debug_f );
TINYSAK_API tsk_debug_f tsk_debug_get_warn_cb();
TINYSAK_API void tsk_debug_set_error_cb(tsk_debug_f );
TINYSAK_API tsk_debug_f tsk_debug_get_error_cb( );
TINYSAK_API void tsk_debug_set_fatal_cb(tsk_debug_f );
TINYSAK_API tsk_debug_f tsk_debug_get_fatal_cb( );
TINYSAK_API int tsk_debug_get_level( );
TINYSAK_API void tsk_debug_set_level(int );
#endif /* TSK_HAVE_DEBUG_H */
TSK_END_DECLS
#endif /* _TINYSAK_DEBUG_H_ */
|