4 * This file is part of BeRTOS.
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.
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.
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
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.
29 * Copyright 2008 Develer S.r.l. (http://www.develer.com/)
34 * \brief Logging system module.
36 * This module implement a simple interface to use the multi level logging system.
37 * The log message have the priority order, like this:
39 * - error message (highest)
41 * - info message (lowest)
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.
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
52 * This module provides two types of macros:
54 * - LOG_* macros: these macros allow formatted output, using the same format
56 * - LOG_*B macros: these macros allow to optionally compile a block of code
57 * depending on the logging level chosen
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
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:
65 * - in your file \c cfg/cfg_\<cfg_module_name\>.h, define the logging
66 * level and verbosity mode for your specific module:
69 * /// Module logging level.
70 * #define <cfg_module_name>_LOG_LEVEL LOG_LVL_INFO
72 * /// Module logging format.
73 * #define <cfg_module_name>_LOG_FORMAT LOG_FMT_VERBOSE
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:
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>
87 * if you include a log.h module without defining the LOG_LEVEL and LOG_VERBOSE
88 * macros, the module uses the default settings.
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.
95 * \author Daniele Basile <asterix@develer.com>
103 #include <cfg/debug.h>
106 * \defgroup logging Logging facilities
110 // Use a default setting if nobody defined a log level
112 #define LOG_LEVEL LOG_LVL_ERR
115 // Use a default setting if nobody defined a log format
117 #define LOG_FORMAT LOG_FMT_TERSE
121 * \name Logging level definition
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).
129 * $WIZ$ log_level = "LOG_LVL_NONE", "LOG_LVL_ERR", "LOG_LVL_WARN", "LOG_LVL_INFO"
132 #define LOG_LVL_NONE 0
133 #define LOG_LVL_ERR 1
134 #define LOG_LVL_WARN 2
135 #define LOG_LVL_INFO 3
139 * \name Logging format
141 * There are two logging format: terse and verbose. The latter prepends
142 * function names and line number information to each log entry.
144 * $WIZ$ log_format = "LOG_FMT_VERBOSE", "LOG_FMT_TERSE"
147 #define LOG_FMT_VERBOSE 1
148 #define LOG_FMT_TERSE 0
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__)
156 #error No LOG_FORMAT defined
159 #if LOG_LEVEL >= LOG_LVL_ERR
161 * Output an error message
163 #define LOG_ERR(str,...) LOG_PRINT("ERR", str, ## __VA_ARGS__)
165 * Define a code block that will be compiled only when LOG_LEVEL >= LOG_LVL_ERR
167 #define LOG_ERRB(x) x
169 INLINE void LOG_ERR(UNUSED_ARG(const char *, fmt), ...) { /* nop */ }
170 #define LOG_ERRB(x) /* Nothing */
173 #if LOG_LEVEL >= LOG_LVL_WARN
175 * Output a warning message
177 #define LOG_WARN(str,...) LOG_PRINT("WARN", str, ## __VA_ARGS__)
179 * Define a code block that will be compiled only when LOG_LEVEL >= LOG_LVL_WARN
181 #define LOG_WARNB(x) x
183 INLINE void LOG_WARN(UNUSED_ARG(const char *, fmt), ...) { /* nop */ }
184 #define LOG_WARNB(x) /* Nothing */
187 #if LOG_LEVEL >= LOG_LVL_INFO
189 * Output an informative message
191 #define LOG_INFO(str,...) LOG_PRINT("INFO", str, ## __VA_ARGS__)
193 * Define a code block that will be compiled only when LOG_LEVEL >= LOG_LVL_INFO
195 #define LOG_INFOB(x) x
197 INLINE void LOG_INFO(UNUSED_ARG(const char *, fmt), ...) { /* nop */ }
198 #define LOG_INFOB(x) /* Nothing */
203 #endif /* CFG_LOG_H */