cd3e1f3df676a99643f0ab09c20d92a7b243883f
[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  *
31  * -->
32  *
33  * \addtogroup logging
34  * \brief Logging system module.
35  *
36  * This module implement a simple interface to use the multi level logging system.
37  * The log message have the priority order, like this:
38  *
39  *  - error message (highest)
40  *  - warning message
41  *  - info message (lowest)
42  *
43  * With this priority system we log only the messages that have priority higher
44  * or equal to the log level that has been configurated; messages below the
45  * selected log level are not included at compile time, so no time and space
46  * is wasted on unused functions.
47  *
48  * Furthermore you can define different log levels for each module. To do this
49  * you just need to define LOG_LEVEL in the configuration file for the
50  * selected module.
51  *
52  * This module provides two types of macros:
53  *
54  *  - LOG_* macros: these macros allow formatted output, using the same format
55  *                  as kprintf
56  *  - LOG_*B macros: these macros allow to optionally compile a block of code
57  *                   depending on the logging level chosen
58  *
59  * To use the logging system you should include this module in your driver
60  * and use one of the LOG_ERR, LOG_WARN and LOG_INFO macros to output error
61  * messages.
62  * Then you should define a LOG_LEVEL and LOG_VERBOSE costant in your
63  * \c cfg/cfg_\<your_cfg_module_name\>.h using the follow policy:
64  *
65  * - in your file \c cfg/cfg_\<cfg_module_name\>.h, define the logging
66  *   level and verbosity mode for your specific module:
67  *
68  * \code
69  *      /// Module logging level.
70  *      #define <cfg_module_name>_LOG_LEVEL    LOG_LVL_INFO
71  *
72  *      /// Module logging format.
73  *      #define <cfg_module_name>_LOG_FORMAT   LOG_FMT_VERBOSE
74  * \endcode
75  *
76  * - then, in the module where you use the logging macros you should define
77  *   the macros LOG_LEVEL and LOG_FORMAT and you should include cfg/log.h
78  *   module, as demonstrated in the following example:
79  *
80  * \code
81  *      // Define log settings for cfg/log.h.
82  *      #define LOG_LEVEL   <cfg_module_name>_LOG_LEVEL
83  *      #define LOG_FORMAT  <cfg_module_name>_LOG_FORMAT
84  *      #include <cfg/log.h>
85  * \endcode
86  *
87  * if you include a log.h module without defining the LOG_LEVEL and LOG_VERBOSE
88  * macros, the module uses the default settings.
89  *
90  * WARNING: when using the log.h module make sure to include this module after
91  * a \c cfg_<cfg_module_name>.h, because the LOG_LEVEL and LOG_VERBOSE macros
92  * must be defined before including the log module. Otherwise the log module
93  * will use the default settings.
94  *
95  * \author Daniele Basile <asterix@develer.com>
96  *
97  * $WIZ$
98  */
99
100 #ifndef CFG_LOG_H
101 #define CFG_LOG_H
102
103 #include <cfg/debug.h>
104
105 /**
106  * \defgroup logging Logging facilities
107  * \{
108  */
109
110 // Use a default setting if nobody defined a log level
111 #ifndef LOG_LEVEL
112 #define LOG_LEVEL       LOG_LVL_ERR
113 #endif
114
115 // Use a default setting if nobody defined a log format
116 #ifndef LOG_FORMAT
117 #define LOG_FORMAT      LOG_FMT_TERSE
118 #endif
119
120 /**
121  * \name Logging level definition
122  *
123  * When you choose a log level messages you choose
124  * also which print function are linked.
125  * When using a log level, you link all log functions that have a priority
126  * higher or equal than the level you chose.
127  * The priority level go from error (highest) to info (lowest).
128  *
129  * $WIZ$ log_level = "LOG_LVL_NONE", "LOG_LVL_ERR", "LOG_LVL_WARN", "LOG_LVL_INFO"
130  * \{
131  */
132 #define LOG_LVL_NONE      0
133 #define LOG_LVL_ERR       1
134 #define LOG_LVL_WARN      2
135 #define LOG_LVL_INFO      3
136 /** \} */
137
138 /**
139  * \name Logging format
140  *
141  * There are two logging format: terse and verbose.  The latter prepends
142  * function names and line number information to each log entry.
143  *
144  * $WIZ$ log_format = "LOG_FMT_VERBOSE", "LOG_FMT_TERSE"
145  * \{
146  */
147 #define LOG_FMT_VERBOSE   1
148 #define LOG_FMT_TERSE     0
149 /** \} */
150
151 #if LOG_FORMAT == LOG_FMT_VERBOSE
152         #define LOG_PRINT(str_level, str,...)    kprintf("%s():%d:%s: " str, __func__, __LINE__, str_level, ## __VA_ARGS__)
153 #elif LOG_FORMAT == LOG_FMT_TERSE
154         #define LOG_PRINT(str_level, str,...)    kprintf("%s: " str, str_level, ## __VA_ARGS__)
155 #else
156         #error No LOG_FORMAT defined
157 #endif
158
159 #if LOG_LEVEL >= LOG_LVL_ERR
160         /**
161          * Output an error message
162          */
163         #define LOG_ERR(str,...)       LOG_PRINT("ERR", str, ## __VA_ARGS__)
164         /**
165          * Define a code block that will be compiled only when LOG_LEVEL >= LOG_LVL_ERR
166          */
167         #define LOG_ERRB(x)            x
168 #else
169         INLINE void LOG_ERR(UNUSED_ARG(const char *, fmt), ...) { /* nop */ }
170         #define LOG_ERRB(x)            /* Nothing */
171 #endif
172
173 #if LOG_LEVEL >= LOG_LVL_WARN
174         /**
175          * Output a warning message
176          */
177         #define LOG_WARN(str,...)       LOG_PRINT("WARN", str, ## __VA_ARGS__)
178         /**
179          * Define a code block that will be compiled only when LOG_LEVEL >= LOG_LVL_WARN
180          */
181         #define LOG_WARNB(x)            x
182 #else
183         INLINE void LOG_WARN(UNUSED_ARG(const char *, fmt), ...) { /* nop */ }
184         #define LOG_WARNB(x)            /* Nothing */
185 #endif
186
187 #if LOG_LEVEL >= LOG_LVL_INFO
188         /**
189          * Output an informative message
190          */
191         #define LOG_INFO(str,...)       LOG_PRINT("INFO", str, ## __VA_ARGS__)
192         /**
193          * Define a code block that will be compiled only when LOG_LEVEL >= LOG_LVL_INFO
194          */
195         #define LOG_INFOB(x)            x
196 #else
197         INLINE void LOG_INFO(UNUSED_ARG(const char *, fmt), ...) { /* nop */ }
198         #define LOG_INFOB(x)            /* Nothing */
199 #endif
200 /** \} */
201
202
203 #endif /* CFG_LOG_H */