From dd6aa1190869aa7c87e6f90f7ed9aa785371373a Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Sat, 15 Jun 2019 11:04:54 -0400 Subject: [PATCH] Logging: add `src/plugins/comp-logging.h` (BT_COMP_LOG*()) This new header is intended to be included by the different component class files so that their logging statements can automatically include the component's name. The BT_COMP_LOG*() macros prepend a prefix similar to this to the logging message: [mein-sink] where `mein-sink` is a component's name. Having the component name within each logging message of a given component class adds valuable context and can help resolve issues faster. I did not include the plugin name and the component class's type and name within the prefix because we already have them thanks to the logging tag, for example `PLUGIN/SRC.CTF.FS`. This would be redundant, make the logging message longer, and make `comp-logging.h` more complex. The BT_COMP_LOG*() macros rely on the `BT_COMP_LOG_SELF_COMP` definition. This is an expression which is, in the logging statement context, the `bt_self_component *` address. Because we have the self component there, we are free to prepend more contextual information to the logging message in the future, should we need it. Typically, when a component class source file defines `BT_LOG_OUTPUT_LEVEL` to `(my_comp->log_level)`, where `my_comp` points to the component's private structure, it will also define `BT_COMP_LOG_SELF_COMP` to `(my_comp->self_comp)`. If `BT_COMP_LOG_SELF_COMP` evaluates to `NULL`, the macros use the prefix [N/A] This could be the case when instrumenting code that is shared for both graph and non-graph contexts, for example the metadata and stream decoders of the `ctf` plugin which can also be used within a query operation. Signed-off-by: Philippe Proulx Change-Id: I62c63ed78d234922f556631726fb7bb852924f0a Reviewed-on: https://review.lttng.org/c/babeltrace/+/1468 Tested-by: jenkins Reviewed-by: Francis Deslauriers --- src/plugins/Makefile.am | 2 + src/plugins/comp-logging.h | 142 +++++++++++++++++++++++++++++++++++++ 2 files changed, 144 insertions(+) create mode 100644 src/plugins/comp-logging.h diff --git a/src/plugins/Makefile.am b/src/plugins/Makefile.am index 0c53f764..aa0c6dba 100644 --- a/src/plugins/Makefile.am +++ b/src/plugins/Makefile.am @@ -3,3 +3,5 @@ SUBDIRS = utils text ctf if ENABLE_DEBUG_INFO SUBDIRS += lttng-utils endif + +EXTRA_DIST = comp-logging.h diff --git a/src/plugins/comp-logging.h b/src/plugins/comp-logging.h new file mode 100644 index 00000000..d1399f72 --- /dev/null +++ b/src/plugins/comp-logging.h @@ -0,0 +1,142 @@ +#ifndef BABELTRACE_PLUGINS_COMP_LOGGING_H +#define BABELTRACE_PLUGINS_COMP_LOGGING_H + +/* + * Copyright 2019 Philippe Proulx + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +#ifndef BT_LOG_TAG +# error Please define a tag with BT_LOG_TAG before including this file. +#endif + +#ifndef BT_LOG_OUTPUT_LEVEL +# error Please define a log level expression with BT_LOG_OUTPUT_LEVEL before including this file. +#endif + +#ifndef BT_COMP_LOG_SELF_COMP +# error Please define a self component address expression with BT_COMP_LOG_SELF_COMP before including this file. +#endif + +#include +#include +#include +#include +#include "logging/log.h" + +#define _BT_COMP_LOG_COMP_PREFIX "[%s] " +#define _BT_COMP_LOG_COMP_NA_STR "N/A" + +/* Logs with level `_lvl` for self component `_self_comp` */ +#define BT_COMP_LOG(_lvl, _self_comp, _fmt, ...) \ + BT_LOG_WRITE((_lvl), BT_LOG_TAG, _BT_COMP_LOG_COMP_PREFIX _fmt, \ + (_self_comp) ? \ + bt_component_get_name( \ + bt_self_component_as_component(_self_comp)) : \ + _BT_COMP_LOG_COMP_NA_STR, \ + ##__VA_ARGS__) + +#define BT_COMP_LOG_CUR_LVL(_lvl, _cur_lvl, _self_comp, _fmt, ...) \ + BT_LOG_WRITE_CUR_LVL((_lvl), (_cur_lvl), BT_LOG_TAG, \ + _BT_COMP_LOG_COMP_PREFIX _fmt, \ + (_self_comp) ? \ + bt_component_get_name( \ + bt_self_component_as_component(_self_comp)) : \ + _BT_COMP_LOG_COMP_NA_STR, \ + ##__VA_ARGS__) + +#define BT_COMP_LOG_ERRNO(_lvl, _self_comp, _msg, _fmt, ...) \ + BT_LOG_WRITE_ERRNO((_lvl), BT_LOG_TAG, _msg, \ + _BT_COMP_LOG_COMP_PREFIX _fmt, \ + (_self_comp) ? \ + bt_component_get_name( \ + bt_self_component_as_component(_self_comp)) : \ + _BT_COMP_LOG_COMP_NA_STR, \ + ##__VA_ARGS__) + +#define BT_COMP_LOG_ERRNO_CUR_LVL(_lvl, _cur_lvl, _self_comp, _msg, _fmt, ...) \ + BT_LOG_WRITE_ERRNO_CUR_LVL((_lvl), (_cur_lvl), BT_LOG_TAG, _msg, \ + _BT_COMP_LOG_COMP_PREFIX _fmt, \ + (_self_comp) ? \ + bt_component_get_name( \ + bt_self_component_as_component(_self_comp)) : \ + _BT_COMP_LOG_COMP_NA_STR, \ + ##__VA_ARGS__) + +#define BT_COMP_LOG_MEM(_lvl, _self_comp, _data_ptr, _data_sz, _fmt, ...) \ + BT_LOG_WRITE_MEM((_lvl), BT_LOG_TAG, (_data_ptr), (_data_sz), \ + _BT_COMP_LOG_COMP_PREFIX _fmt, \ + (_self_comp) ? \ + bt_component_get_name( \ + bt_self_component_as_component(_self_comp)) : \ + _BT_COMP_LOG_COMP_NA_STR, \ + ##__VA_ARGS__) + +/* Specific per-level logging macros; they use `BT_COMP_LOG_SELF_COMP` */ +#define BT_COMP_LOGF(_fmt, ...) \ + BT_COMP_LOG(BT_LOG_FATAL, (BT_COMP_LOG_SELF_COMP), _fmt, ##__VA_ARGS__) +#define BT_COMP_LOGE(_fmt, ...) \ + BT_COMP_LOG(BT_LOG_ERROR, (BT_COMP_LOG_SELF_COMP), _fmt, ##__VA_ARGS__) +#define BT_COMP_LOGW(_fmt, ...) \ + BT_COMP_LOG(BT_LOG_WARN, (BT_COMP_LOG_SELF_COMP), _fmt, ##__VA_ARGS__) +#define BT_COMP_LOGI(_fmt, ...) \ + BT_COMP_LOG(BT_LOG_INFO, (BT_COMP_LOG_SELF_COMP), _fmt, ##__VA_ARGS__) +#define BT_COMP_LOGD(_fmt, ...) \ + BT_COMP_LOG(BT_LOG_DEBUG, (BT_COMP_LOG_SELF_COMP), _fmt, ##__VA_ARGS__) +#define BT_COMP_LOGV(_fmt, ...) \ + BT_COMP_LOG(BT_LOG_VERBOSE, (BT_COMP_LOG_SELF_COMP), _fmt, ##__VA_ARGS__) +#define BT_COMP_LOGF_STR(_str) \ + BT_COMP_LOG(BT_LOG_FATAL, (BT_COMP_LOG_SELF_COMP), "%s", (_str)) +#define BT_COMP_LOGE_STR(_str) \ + BT_COMP_LOG(BT_LOG_ERROR, (BT_COMP_LOG_SELF_COMP), "%s", (_str)) +#define BT_COMP_LOGW_STR(_str) \ + BT_COMP_LOG(BT_LOG_WARN, (BT_COMP_LOG_SELF_COMP), "%s", (_str)) +#define BT_COMP_LOGI_STR(_str) \ + BT_COMP_LOG(BT_LOG_INFO, (BT_COMP_LOG_SELF_COMP), "%s", (_str)) +#define BT_COMP_LOGD_STR(_str) \ + BT_COMP_LOG(BT_LOG_DEBUG, (BT_COMP_LOG_SELF_COMP), "%s", (_str)) +#define BT_COMP_LOGV_STR(_str) \ + BT_COMP_LOG(BT_LOG_VERBOSE, (BT_COMP_LOG_SELF_COMP), "%s", (_str)) +#define BT_COMP_LOGF_ERRNO(_msg, _fmt, ...) \ + BT_COMP_LOG_ERRNO(BT_LOG_FATAL, (BT_COMP_LOG_SELF_COMP), _msg, _fmt, ##__VA_ARGS__) +#define BT_COMP_LOGE_ERRNO(_msg, _fmt, ...) \ + BT_COMP_LOG_ERRNO(BT_LOG_ERROR, (BT_COMP_LOG_SELF_COMP), _msg, _fmt, ##__VA_ARGS__) +#define BT_COMP_LOGW_ERRNO(_msg, _fmt, ...) \ + BT_COMP_LOG_ERRNO(BT_LOG_WARN, (BT_COMP_LOG_SELF_COMP), _msg, _fmt, ##__VA_ARGS__) +#define BT_COMP_LOGI_ERRNO(_msg, _fmt, ...) \ + BT_COMP_LOG_ERRNO(BT_LOG_INFO, (BT_COMP_LOG_SELF_COMP), _msg, _fmt, ##__VA_ARGS__) +#define BT_COMP_LOGD_ERRNO(_msg, _fmt, ...) \ + BT_COMP_LOG_ERRNO(BT_LOG_DEBUG, (BT_COMP_LOG_SELF_COMP), _msg, _fmt, ##__VA_ARGS__) +#define BT_COMP_LOGV_ERRNO(_msg, _fmt, ...) \ + BT_COMP_LOG_ERRNO(BT_LOG_VERBOSE, (BT_COMP_LOG_SELF_COMP), _msg, _fmt, ##__VA_ARGS__) +#define BT_COMP_LOGF_MEM(_data_ptr, _data_sz, _fmt, ...) \ + BT_COMP_LOG_MEM(BT_LOG_FATAL, (BT_COMP_LOG_SELF_COMP), (_data_ptr), (_data_sz), _fmt, ##__VA_ARGS__) +#define BT_COMP_LOGE_MEM(_data_ptr, _data_sz, _fmt, ...) \ + BT_COMP_LOG_MEM(BT_LOG_ERROR, (BT_COMP_LOG_SELF_COMP), (_data_ptr), (_data_sz), _fmt, ##__VA_ARGS__) +#define BT_COMP_LOGW_MEM(_data_ptr, _data_sz, _fmt, ...) \ + BT_COMP_LOG_MEM(BT_LOG_WARN, (BT_COMP_LOG_SELF_COMP), (_data_ptr), (_data_sz), _fmt, ##__VA_ARGS__) +#define BT_COMP_LOGI_MEM(_data_ptr, _data_sz, _fmt, ...) \ + BT_COMP_LOG_MEM(BT_LOG_INFO, (BT_COMP_LOG_SELF_COMP), (_data_ptr), (_data_sz), _fmt, ##__VA_ARGS__) +#define BT_COMP_LOGD_MEM(_data_ptr, _data_sz, _fmt, ...) \ + BT_COMP_LOG_MEM(BT_LOG_DEBUG, (BT_COMP_LOG_SELF_COMP), (_data_ptr), (_data_sz), _fmt, ##__VA_ARGS__) +#define BT_COMP_LOGV_MEM(_data_ptr, _data_sz, _fmt, ...) \ + BT_COMP_LOG_MEM(BT_LOG_VERBOSE, (BT_COMP_LOG_SELF_COMP), (_data_ptr), (_data_sz), _fmt, ##__VA_ARGS__) + +#endif /* BABELTRACE_PLUGINS_COMP_LOGGING_H */ -- 2.34.1