From fc1c829d69a2731429d3b884294f918019d8fb95 Mon Sep 17 00:00:00 2001 From: asterix Date: Tue, 3 Jun 2008 15:31:02 +0000 Subject: [PATCH] Add logging multilevel system. git-svn-id: https://src.develer.com/svnoss/bertos/trunk@1413 38d2e660-2303-0410-9eaa-f027e97ec537 --- bertos/cfg/debug.h | 67 ++++++++++++++++++++++ bertos/cfg/log.h | 140 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 207 insertions(+) create mode 100644 bertos/cfg/log.h diff --git a/bertos/cfg/debug.h b/bertos/cfg/debug.h index 60a9e7fb..2fbedc51 100644 --- a/bertos/cfg/debug.h +++ b/bertos/cfg/debug.h @@ -46,6 +46,7 @@ #include #include + /* * Defaults for rarely used config stuff. */ @@ -185,6 +186,72 @@ #define TRACEMSG(...) do {} while(0) #endif + /** + * Multi level logging system. + * + * You can use these macro directy or using the cfg/log.h module + * that provide a simple interface for using the logging multilevel system. + * The priority level is order form error messages (hight priority) to info messages + * (low priority), so if you choose a low level log message you can see also all message + * that have a hight priority. + * + * \{ + */ + /// Logging level definition + #define DBG_LOG_ERROR 0 + #define DBG_LOG_WARNING 1 + #define DBG_LOG_INFO 2 + + /// Logging verbose mode + #define DBG_LOG_VERBOSE 1 + #define DBG_LOG_SILENT 0 + + #define DBG_ERROR(debug_level, mode, str,...) \ + do { \ + if(debug_level >= DBG_LOG_ERROR) \ + { \ + if (mode == DBG_LOG_VERBOSE)\ + kprintf("Error(%s():%d): "str, __func__, __LINE__, ## __VA_ARGS__); \ + else \ + kprintf("Error: "str, ## __VA_ARGS__); \ + } \ + else \ + { \ + /* NONE */ \ + } \ + } while (0) + + #define DBG_WARNING(debug_level, mode, str,...) \ + do { \ + if(debug_level >= DBG_LOG_WARNING) \ + { \ + if (mode == DBG_LOG_VERBOSE) \ + kprintf("Warning(%s():%d): "str, __func__, __LINE__, ## __VA_ARGS__); \ + else \ + kprintf("Warning: "str, ## __VA_ARGS__); \ + } \ + else \ + { \ + /* NONE */ \ + } \ + } while (0) + + #define DBG_INFO(debug_level, mode, str,...) \ + do { \ + if(debug_level >= DBG_LOG_INFO) \ + { \ + if (mode == DBG_LOG_VERBOSE) \ + kprintf("Info(%s():%d): "str, __func__, __LINE__, ## __VA_ARGS__); \ + else \ + kprintf("Info: "str, ## __VA_ARGS__); \ + } \ + else \ + { \ + /* NONE */ \ + } \ + } while (0) + /* \} */ + /** * \name Walls to detect data corruption * \{ diff --git a/bertos/cfg/log.h b/bertos/cfg/log.h new file mode 100644 index 00000000..b3fa27f9 --- /dev/null +++ b/bertos/cfg/log.h @@ -0,0 +1,140 @@ +/** + * \file + * + * + * \brief Logging system module. + * + * This module implement a simple interface to use the multi level + * logging system. + * The log message have the priority order, like this: + * + * - error message (most hight) + * - warning message + * - info message (most low) + * + * With this priority system we can log only the message that have egual or major + * priority than log level that you has been configurate. Further you can have a + * differ log level for each module that you want. To do this you just need to + * define LOG_LEVEL in cfg of select module. + * When you set a log level, the system logs only the message that have priority + * egual or major that you have define, but the other logs function are not include + * at compile time, so all used logs function are linked, but the other no. + * + * To use logging system you should include this module in your drive and use + * a LOG_ERROR, LOG_WARNING and LOG_INFO macros to set the level log of the message. + * Then you should define a LOG_LEVEL and LOG_VERBOSE costant in your + * cfg/cfg_.h using the follow police: + * + * \code + * + * /** + * * Logging level definition. + * * + * * Use 0 to log only the error messages + * * Use 1 to log the error and warning messages + * * Use 2 to log all messages + * * + * #define _LOG_LEVEL 2 + * + * /** + * * Set logging verbosity. + * * + * * If verbose is zero print short log messages. + * * + * #define _LOG_VERBOSE 1 + * + * // Define logging setting (for cfg/log.h module). + * #define LOG_LEVEL _LOG_LEVEL + * #define LOG_VERBOSE _LOG_VERBOSE + * + * \endcode + * + * if you include a log.h module without define the LOG_LEVEL and LOG_VERBOSE + * macros, the module use the default setting (see below). + * + * WARNING: when use the log.h module, and you want to set a your log level + * make sure to include this module after a cfg_.h, because the + * LOG_LEVEL and LOG_VERBOSE macros must be define before to include log module, + * otherwise the log module use a default settings. + * + * \version $Id$ + * + * \author Daniele Basile + */ + +#ifndef CFG_LOG_H +#define CFG_LOG_H + +#include + +// Use a default setting if nobody have define a log level +#ifndef LOG_LEVEL +#define LOG_LEVEL DBG_LOG_ERROR +#endif + +// Use a default setting if nobody have define a log verbosity +#ifndef LOG_VERBOSE +#define LOG_VERBOSE DBG_LOG_SILENT +#endif + +/** + * Error log message. + * Use this macro to print error messages. This log level have hight priority, + * that means the error messages are always printed, if debug is enable. + */ +#define LOG_ERROR(str,...) DBG_ERROR(LOG_LEVEL, LOG_VERBOSE, str, ## __VA_ARGS__) + +/** + * Log message level select. + * When you choose a log level messages you choose + * also which print function are linked. + * If you choose a low level of log you link all log function (error, warning and info), + * but if choose a hight level you link only that have the priority egual or hight. + * The priority level go from error (most hight) to info (most low) (see cfg/debug.h + * for more detail). + * + */ +#if (LOG_LEVEL <= DBG_LOG_INFO) + #define LOG_WARNING(str,...) DBG_WARNING(LOG_LEVEL, LOG_VERBOSE, str, ## __VA_ARGS__) + #define LOG_INFO(str,...) DBG_INFO(LOG_LEVEL, LOG_VERBOSE, str, ## __VA_ARGS__) + +#elif (LOG_LEVEL <= DBG_LOG_WARNING) + #define LOG_WARNING(str,...) DBG_WARNING(LOG_LEVEL, LOG_VERBOSE, str, ## __VA_ARGS__) + #define LOG_INFO(str,...) /* Nothing */ + +#else /* LOG_LEVEL <= DBG_LOG_ERROR */ + #define LOG_WARNING(str,...) /* Nothing */ + #define LOG_INFO(str,...) /* Nothing */ + +#endif + +#endif /* CFG_LOG_H */ + -- 2.25.1