LOG_PRINT: Fix out of order parameters
[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  * \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 (highest)
39  *  - warning message
40  *  - info message (lowest)
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  *      /// Module logging level.
60  *      #define <cfg_module_name>_LOG_LEVEL    LOG_LVL_INFO
61  *
62  *      /// Module logging format.
63  *      #define <cfg_module_name>_LOG_FORMAT   LOG_FMT_VERBOSE
64  * \endcode
65  *
66  * - then, in the module that you use a logging macros you should define
67  *   a LOG_LEVEL and LOG_FORMAT using the previous value that you have define
68  *   in cfg_<cfg_module_name>.h header. After this you should include the cfg/log.h
69  *   module:
70  *
71  * \code
72  *      // Define log settings for cfg/log.h.
73  *      #define LOG_LEVEL   <cfg_module_name>_LOG_LEVEL
74  *      #define LOG_FORMAT  <cfg_module_name>_LOG_FORMAT
75  *      #include <cfg/log.h>
76  * \endcode
77  *
78  * if you include a log.h module without define the LOG_LEVEL and LOG_VERBOSE
79  * macros, the module use the default setting (see below).
80  *
81  * WARNING: when use the log.h module, and you want to set a your log level
82  * make sure to include this module after a \c cfg_<cfg_module_name>.h, because the
83  * LOG_LEVEL and LOG_VERBOSE macros must be defined before to include log module,
84  * otherwise the log module use a default settings.
85  *
86  * \version $Id$
87  * \author Daniele Basile <asterix@develer.com>
88  *
89  */
90
91 #ifndef CFG_LOG_H
92 #define CFG_LOG_H
93
94 #include <cfg/debug.h>
95
96
97 // Use a default setting if nobody defined a log level
98 #ifndef LOG_LEVEL
99 #define LOG_LEVEL       LOG_LVL_ERR
100 #endif
101
102 // Use a default setting if nobody defined a log format
103 #ifndef LOG_FORMAT
104 #define LOG_FORMAT      LOG_FMT_TERSE
105 #endif
106
107 /**
108  * \name Logging level definition
109  *
110  * When you choose a log level messages you choose
111  * also which print function are linked.
112  * If you choose a low level of log you link all log function (error, warning and info),
113  * but if choose a hight level you link only that have the priority egual or hight.
114  * The priority level go from error (most hight) to info (most low) (see cfg/debug.h
115  * for more detail).
116  * \{
117  */
118 #define LOG_LVL_NONE      0
119 #define LOG_LVL_ERR       1
120 #define LOG_LVL_WARN      2
121 #define LOG_LVL_INFO      3
122 /* \} */
123
124 /**
125  * \name Logging format
126  *
127  * There are two logging format: terse and verbose.  The latter prepends
128  * function names and line number information to each log entry.
129  * \{
130  */
131 #define LOG_FMT_VERBOSE   1
132 #define LOG_FMT_TERSE     0
133 /* \} */
134
135 #define LOG_SILENT    LOG_FMT_TERSE   /* OBSOLETE */
136 #define LOG_VERBOSE   LOG_FMT_VERBOSE /* OBSOLETE */
137
138 #if LOG_FORMAT == LOG_FMT_VERBOSE
139         #define LOG_PRINT(str_level, str,...)    kprintf("%s():%d:%s: " str, __func__, __LINE__, str_level, ## __VA_ARGS__)
140 #elif LOG_FORMAT == LOG_FMT_TERSE
141         #define LOG_PRINT(str_level, str,...)    kprintf("%s: " str, str_level, ## __VA_ARGS__)
142 #else
143         #error No LOG_FORMAT defined
144 #endif
145
146 #if LOG_LEVEL >= LOG_LVL_ERR
147         #define LOG_ERR(str,...)       LOG_PRINT("ERR", str, ## __VA_ARGS__)
148 #else
149         #define LOG_ERR(str,...)       /* Nothing */
150 #endif
151
152 #if LOG_LEVEL >= LOG_LVL_WARN
153         #define LOG_WARN(str,...)       LOG_PRINT("WARN", str, ## __VA_ARGS__)
154 #else
155         #define LOG_WARN(str,...)       /* Nothing */
156 #endif
157
158 #if LOG_LEVEL >= LOG_LVL_INFO
159         #define LOG_INFO(str,...)       LOG_PRINT("INFO", str, ## __VA_ARGS__)
160 #else
161         #define LOG_INFO(str,...)       /* Nothing */
162 #endif
163
164 #endif /* CFG_LOG_H */