From dfc50ce142ca4ee34425d0e75c29360c684986cb Mon Sep 17 00:00:00 2001 From: Simon Marchi Date: Tue, 21 May 2024 12:43:47 -0400 Subject: [PATCH] cpp-common/bt2c: Logger: factor out code to append cause Factor out the code that appends an error cause to a helper method, so that it can be re-used. Change-Id: I055314bef66968ef2ef30b5dd1728680d2f79c7c Signed-off-by: Simon Marchi Reviewed-on: https://review.lttng.org/c/babeltrace/+/12228 Reviewed-by: Philippe Proulx --- src/cpp-common/bt2c/logging.hpp | 34 +++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/src/cpp-common/bt2c/logging.hpp b/src/cpp-common/bt2c/logging.hpp index 2619152e..bda68740 100644 --- a/src/cpp-common/bt2c/logging.hpp +++ b/src/cpp-common/bt2c/logging.hpp @@ -294,6 +294,25 @@ public: return _mTextLocStrFmt; } + void appendCauseStr(const char * const fileName, const int lineNo, const char * const initMsg, + const char * const msg) const noexcept + { + if (_mSelfMsgIter) { + bt_current_thread_error_append_cause_from_message_iterator( + _mSelfMsgIter->libObjPtr(), fileName, lineNo, "%s%s", initMsg, msg); + } else if (_mSelfComp) { + bt_current_thread_error_append_cause_from_component(_mSelfComp->libObjPtr(), fileName, + lineNo, "%s%s", initMsg, msg); + } else if (_mSelfCompCls) { + bt_current_thread_error_append_cause_from_component_class( + _mSelfCompCls->libObjPtr(), fileName, lineNo, "%s%s", initMsg, msg); + } else { + BT_ASSERT(_mModuleName); + bt_current_thread_error_append_cause_from_unknown(_mModuleName->data(), fileName, + lineNo, "%s%s", initMsg, msg); + } + } + private: struct _StdLogWriter final { @@ -534,20 +553,7 @@ private: /* Append an error cause if needed */ if (AppendCauseV) { - if (_mSelfMsgIter) { - bt_current_thread_error_append_cause_from_message_iterator( - _mSelfMsgIter->libObjPtr(), fileName, lineNo, "%s%s", initMsg, _mBuf.data()); - } else if (_mSelfComp) { - bt_current_thread_error_append_cause_from_component( - _mSelfComp->libObjPtr(), fileName, lineNo, "%s%s", initMsg, _mBuf.data()); - } else if (_mSelfCompCls) { - bt_current_thread_error_append_cause_from_component_class( - _mSelfCompCls->libObjPtr(), fileName, lineNo, "%s%s", initMsg, _mBuf.data()); - } else { - BT_ASSERT(_mModuleName); - bt_current_thread_error_append_cause_from_unknown( - _mModuleName->data(), fileName, lineNo, "%s%s", initMsg, _mBuf.data()); - } + this->appendCauseStr(fileName, lineNo, initMsg, _mBuf.data()); } } -- 2.34.1