Add logging multilevel system.
[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
36  * logging system.
37  * The log message have the priority order, like this:
38  *
39  * - error message (most hight)
40  * - warning message 
41  * - info message (most low)
42  *
43  * With this priority system we can log only the message that have egual or major
44  * priority than log level that you has been configurate. Further you can have a 
45  * differ log level for each module that you want. To do this you just need to 
46  * define LOG_LEVEL in cfg of select module.
47  * When you set a log level, the system logs only the message that have priority
48  * egual or major that you have define, but the other logs function are not include
49  * at compile time, so all used logs function are linked, but the other no.
50  *
51  * To use logging system you should include this module in your drive and use 
52  * a LOG_ERROR, LOG_WARNING and LOG_INFO macros to set the level log of the message.
53  * Then you should define a LOG_LEVEL and LOG_VERBOSE costant in your
54  * cfg/cfg_<your_module_name>.h using the follow police:
55  * 
56  * \code
57  *
58  *      /**
59  *       * Logging level definition.
60  *       *
61  *       * Use 0 to log only the error messages
62  *       * Use 1 to log the error and warning messages
63  *       * Use 2 to log all messages
64  *       *
65  *      #define <module_name>_LOG_LEVEL      2
66  *
67  *      /**
68  *       * Set logging verbosity.
69  *       *
70  *       * If verbose is zero print short log messages.
71  *       *
72  *      #define <module_name>_LOG_VERBOSE    1
73  *
74  *      // Define logging setting (for cfg/log.h module).
75  *      #define LOG_LEVEL       <module_name>_LOG_LEVEL
76  *      #define LOG_VERBOSE     <module_name>_LOG_VERBOSE
77  *
78  * \endcode
79  *
80  * if you include a log.h module without define the LOG_LEVEL and LOG_VERBOSE
81  * macros, the module use the default setting (see below).
82  *
83  * WARNING: when use the log.h module, and you want to set a your log level
84  * make sure to include this module after a cfg_<module_name>.h, because the
85  * LOG_LEVEL and LOG_VERBOSE macros must be define before to include log module,
86  * otherwise the log module use a default settings.
87  *
88  * \version $Id$
89  *
90  * \author Daniele Basile <asterix@develer.com>
91  */
92
93 #ifndef CFG_LOG_H
94 #define CFG_LOG_H
95
96 #include <cfg/debug.h>
97
98 // Use a default setting if nobody have define a log level
99 #ifndef LOG_LEVEL
100 #define LOG_LEVEL   DBG_LOG_ERROR
101 #endif
102
103 // Use a default setting if nobody have define a log verbosity
104 #ifndef LOG_VERBOSE
105 #define LOG_VERBOSE   DBG_LOG_SILENT
106 #endif
107
108 /**
109  *  Error log message.
110  *  Use this macro to print error messages. This log level have hight priority,
111  *  that means the error messages are always printed, if debug is enable.
112  */
113 #define LOG_ERROR(str,...)      DBG_ERROR(LOG_LEVEL, LOG_VERBOSE, str, ## __VA_ARGS__)
114
115 /**
116  * Log message level select.
117  * When you choose a log level messages you choose
118  * also which print function are linked.
119  * If you choose a low level of log you link all log function (error, warning and info),
120  * but if choose a hight level you link only that have the priority egual or hight.
121  * The priority level go from error (most hight) to info (most low) (see cfg/debug.h 
122  * for more detail).
123  *
124  */
125 #if (LOG_LEVEL <= DBG_LOG_INFO)
126         #define LOG_WARNING(str,...)    DBG_WARNING(LOG_LEVEL, LOG_VERBOSE,  str, ## __VA_ARGS__)
127         #define LOG_INFO(str,...)       DBG_INFO(LOG_LEVEL, LOG_VERBOSE, str, ## __VA_ARGS__)
128
129 #elif (LOG_LEVEL <= DBG_LOG_WARNING)
130         #define LOG_WARNING(str,...)    DBG_WARNING(LOG_LEVEL, LOG_VERBOSE,  str, ## __VA_ARGS__)
131         #define LOG_INFO(str,...)       /* Nothing */
132
133 #else /* LOG_LEVEL <= DBG_LOG_ERROR */
134         #define LOG_WARNING(str,...)    /* Nothing */
135         #define LOG_INFO(str,...)       /* Nothing */
136
137 #endif
138
139 #endif /* CFG_LOG_H */
140