Fix documentation
[bertos.git] / bertos / cfg / log.h
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2008 Develer S.r.l. (http://www.develer.com/)
30  * All Rights Reserved.
31  * -->
32  *
33  * \brief Logging system module.
34  *
35  * This module implement a simple interface to use the multi level logging system.
36  * The log message have the priority order, like this:
37  *
38  *  - error message (most hight)
39  *  - warning message
40  *  - info message (most low)
41  *
42  * With this priority system we can log only the message that have egual or major
43  * priority than log level that you has been configurate. Further you can have a
44  * differ log level for each module that you want. To do this you just need to
45  * define LOG_LEVEL in cfg of select module.
46  * When you set a log level, the system logs only the message that have priority
47  * egual or major that you have define, but the other logs function are not include
48  * at compile time, so all used logs function are linked, but the other no.
49  *
50  * To use logging system you should include this module in your drive and use
51  * a LOG_ERROR, LOG_WARNING and LOG_INFO macros to set the level log of the message.
52  * Then you should define a LOG_LEVEL and LOG_VERBOSE costant in your
53  * \c cfg/cfg_\<your_cfg_module_name\>.h using the follow policy:
54  *
55  * - in your file \c cfg/cfg_\<cfg_module_name\>.h, you define the logging
56  *   level and verbosity mode for your specific module:
57  *
58  * \code
59  *
60  *       **
61  *       * Logging level definition.
62  *       *
63  *       * Use 0 to log only the error messages
64  *       * Use 1 to log the error and warning messages
65  *       * Use 2 to log all messages
66  *       *
67  *      #define <cfg_module_name>_LOG_LEVEL      2
68  *
69  *       **
70  *       * Set logging verbosity.
71  *       *
72  *       * If verbose is zero print short log messages.
73  *       *
74  *      #define <cfg_module_name>_LOG_VERBOSE    1
75  *
76  * \endcode
77  *
78  * - then, in the module that you use a logging macros you should define
79  *   a LOG_LEVEL and LOG_VERBOSE using the previous value that you have define
80  *   in cfg_<cfg_module_name>.h header. After this you should include the cfg/log.h
81  *   module:
82  *
83  * \code
84  *
85  *      // Define logging setting (for cfg/log.h module).
86  *      #define LOG_LEVEL       <cfg_module_name>_LOG_LEVEL
87  *      #define LOG_VERBOSE     <cfg_module_name>_LOG_VERBOSE
88  *      #include <cfg/log.h>
89  *
90  * \endcode
91  *
92  * if you include a log.h module without define the LOG_LEVEL and LOG_VERBOSE
93  * macros, the module use the default setting (see below).
94  *
95  * WARNING: when use the log.h module, and you want to set a your log level
96  * make sure to include this module after a \c cfg_<cfg_module_name>.h, because the
97  * LOG_LEVEL and LOG_VERBOSE macros must be defined before to include log module,
98  * otherwise the log module use a default settings.
99  *
100  * \version $Id$
101  *
102  * \author Daniele Basile <asterix@develer.com>
103  */
104
105 #ifndef CFG_LOG_H
106 #define CFG_LOG_H
107
108 #include <cfg/debug.h>
109
110
111 // Use a default setting if nobody have define a log level
112 #ifndef LOG_LEVEL
113 #define LOG_LEVEL       LOG_LVL_ERR
114 #endif
115
116 // Use a default setting if nobody have define a log verbosity
117 #ifndef LOG_VERBOSITY
118 #define LOG_VERBOSITY   LOG_SILENT
119 #endif
120
121 /**
122 * Multi level logging system.
123 *
124 * You can use these macro directy or using the cfg/log.h module
125 * that provide a simple interface for using the logging multilevel system.
126 * The priority level is order form error messages (hight priority) to info messages
127 * (low priority), so if you choose a low level log message you can see also all message
128 * that have a hight priority.
129 *
130 * \{
131 */
132 /// Logging level definition
133 #define LOG_LVL_ERR       0
134 #define LOG_LVL_WARN      1
135 #define LOG_LVL_INFO      2
136
137 /// Logging verbose mode
138 #define LOG_VERBOSE   1
139 #define LOG_SILENT    0
140
141
142 #if LOG_VERBOSITY == LOG_VERBOSE
143         #define LOG_PRINT(str_level, str,...)    kprintf("%s(%s():%d): "str, str_level, __func__, __LINE__, ## __VA_ARGS__)
144
145 #elif LOG_VERBOSITY == LOG_SILENT
146         #define LOG_PRINT(str_level, str,...)    kprintf("%s: "str, str_level, ## __VA_ARGS__)
147
148 #else
149         #error No log verbosity defined
150
151 #endif
152
153 /**
154  * Log message level select.
155  * When you choose a log level messages you choose
156  * also which print function are linked.
157  * If you choose a low level of log you link all log function (error, warning and info),
158  * but if choose a hight level you link only that have the priority egual or hight.
159  * The priority level go from error (most hight) to info (most low) (see cfg/debug.h
160  * for more detail).
161  *
162  */
163 #define LOG_ERR(str,...)            LOG_PRINT("ERR", str, ## __VA_ARGS__)
164
165 #if (LOG_LEVEL <= LOG_LVL_INFO)
166         #define LOG_WARN(str,...)       LOG_PRINT("WARN", str, ## __VA_ARGS__)
167         #define LOG_INFO(str,...)       LOG_PRINT("INFO", str, ## __VA_ARGS__)
168
169 #elif (LOG_LEVEL <= LOG_LVL_WARN)
170         #define LOG_WARN(str,...)       LOG_PRINT("WARN", str, ## __VA_ARGS__)
171         #define LOG_INFO(str,...)       /* Nothing */
172
173 #else /* LOG_LEVEL <= LOG_LVL_ERR */
174         #define LOG_WARN(str,...)       /* Nothing */
175         #define LOG_INFO(str,...)       /* Nothing */
176
177 #endif
178
179 #endif /* CFG_LOG_H */
180