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/)
33 * \defgroup logging Logging facilities
36 * \brief Logging system module.
38 * This module implement a simple interface to use the multi level logging system.
39 * The log message have the priority order, like this:
41 * - error message (highest)
43 * - info message (lowest)
45 * With this priority system we log only the messages that have priority higher
46 * or equal to the log level that has been configurated; messages below the
47 * selected log level are not included at compile time, so no time and space
48 * is wasted on unused functions.
50 * Furthermore you can define different log levels for each module. To do this
51 * you just need to define LOG_LEVEL in the configuration file for the
54 * This module provides two types of macros:
56 * - LOG_* macros: these macros allow formatted output, using the same format
58 * - LOG_*B macros: these macros allow to optionally compile a block of code
59 * depending on the logging level chosen
61 * To use the logging system you should include this module in your driver
62 * and use one of the LOG_ERR, LOG_WARN and LOG_INFO macros to output error
64 * Then you should define a LOG_LEVEL and LOG_VERBOSE costant in your
65 * \c cfg/cfg_\<your_cfg_module_name\>.h using the follow policy:
67 * - in your file \c cfg/cfg_\<cfg_module_name\>.h, define the logging
68 * level and verbosity mode for your specific module:
71 * /// Module logging level.
72 * #define <cfg_module_name>_LOG_LEVEL LOG_LVL_INFO
74 * /// Module logging format.
75 * #define <cfg_module_name>_LOG_FORMAT LOG_FMT_VERBOSE
78 * - then, in the module where you use the logging macros you should define
79 * the macros LOG_LEVEL and LOG_FORMAT and you should include cfg/log.h
80 * module, as demonstrated in the following example:
83 * // Define log settings for cfg/log.h.
84 * #define LOG_LEVEL <cfg_module_name>_LOG_LEVEL
85 * #define LOG_FORMAT <cfg_module_name>_LOG_FORMAT
86 * #include <cfg/log.h>
89 * if you include a log.h module without defining the LOG_LEVEL and LOG_VERBOSE
90 * macros, the module uses the default settings.
92 * WARNING: when using the log.h module make sure to include this module after
93 * a \c cfg_<cfg_module_name>.h, because the LOG_LEVEL and LOG_VERBOSE macros
94 * must be defined before including the log module. Otherwise the log module
95 * will use the default settings.
97 * \author Daniele Basile <asterix@develer.com>
105 #include <cfg/debug.h>
107 // Use a default setting if nobody defined a log level
109 #define LOG_LEVEL LOG_LVL_ERR
112 // Use a default setting if nobody defined a log format
114 #define LOG_FORMAT LOG_FMT_TERSE
118 * \name Logging level definition
120 * When you choose a log level messages you choose
121 * also which print function are linked.
122 * When using a log level, you link all log functions that have a priority
123 * higher or equal than the level you chose.
124 * The priority level go from error (highest) to info (lowest).
126 * $WIZ$ log_level = "LOG_LVL_NONE", "LOG_LVL_ERR", "LOG_LVL_WARN", "LOG_LVL_INFO"
129 #define LOG_LVL_NONE 0
130 #define LOG_LVL_ERR 1
131 #define LOG_LVL_WARN 2
132 #define LOG_LVL_INFO 3
136 * \name Logging format
138 * There are two logging format: terse and verbose. The latter prepends
139 * function names and line number information to each log entry.
141 * $WIZ$ log_format = "LOG_FMT_VERBOSE", "LOG_FMT_TERSE"
144 #define LOG_FMT_VERBOSE 1
145 #define LOG_FMT_TERSE 0
148 #if LOG_FORMAT == LOG_FMT_VERBOSE
149 #define LOG_PRINT(str_level, str,...) kprintf("%s():%d:%s: " str, __func__, __LINE__, str_level, ## __VA_ARGS__)
150 #elif LOG_FORMAT == LOG_FMT_TERSE
151 #define LOG_PRINT(str_level, str,...) kprintf("%s: " str, str_level, ## __VA_ARGS__)
153 #error No LOG_FORMAT defined
156 #if LOG_LEVEL >= LOG_LVL_ERR
158 * Output an error message
160 #define LOG_ERR(str,...) LOG_PRINT("ERR", str, ## __VA_ARGS__)
162 * Define a code block that will be compiled only when LOG_LEVEL >= LOG_LVL_ERR
164 #define LOG_ERRB(x) x
166 INLINE void LOG_ERR(UNUSED_ARG(const char *, fmt), ...) { /* nop */ }
167 #define LOG_ERRB(x) /* Nothing */
170 #if LOG_LEVEL >= LOG_LVL_WARN
172 * Output a warning message
174 #define LOG_WARN(str,...) LOG_PRINT("WARN", str, ## __VA_ARGS__)
176 * Define a code block that will be compiled only when LOG_LEVEL >= LOG_LVL_WARN
178 #define LOG_WARNB(x) x
180 INLINE void LOG_WARN(UNUSED_ARG(const char *, fmt), ...) { /* nop */ }
181 #define LOG_WARNB(x) /* Nothing */
184 #if LOG_LEVEL >= LOG_LVL_INFO
186 * Output an informative message
188 #define LOG_INFO(str,...) LOG_PRINT("INFO", str, ## __VA_ARGS__)
190 * Define a code block that will be compiled only when LOG_LEVEL >= LOG_LVL_INFO
192 #define LOG_INFOB(x) x
194 INLINE void LOG_INFO(UNUSED_ARG(const char *, fmt), ...) { /* nop */ }
195 #define LOG_INFOB(x) /* Nothing */
200 #endif /* CFG_LOG_H */