src/cpp-common: add `LogCfg` class
authorFrancis Deslauriers <francis.deslauriers@efficios.com>
Fri, 22 Apr 2022 20:47:55 +0000 (16:47 -0400)
committerPhilippe Proulx <eeppeliteloop@gmail.com>
Mon, 11 Sep 2023 15:24:02 +0000 (11:24 -0400)
`LogCfg` is a class to carry around the logging information used by
`BT_LOG*()` macros in the in-tree plugins.

Philippe added:

* Class comment.

* Setting other members when possible (for example, `_mSelfComp` when
  using the self message iterator constructor).

* Use references instead of pointers in constructors because they can't
  be `nullptr`, making it possible to remove some assertions.

* Component class member (because the libbabeltrace2 API has nothing
  to get a self component class from a self component) and
  compClsName() method.

  compClsName() is always available when using the self message
  iterator, component, or component class constructor.

Signed-off-by: Francis Deslauriers <francis.deslauriers@efficios.com>
Signed-off-by: Philippe Proulx <eeppeliteloop@gmail.com>
Change-Id: Ic6980a2bcf74f8b270b0a108efc79f25f0e65d8d
Reviewed-on: https://review.lttng.org/c/babeltrace/+/7936
Reviewed-by: Philippe Proulx <eeppeliteloop@gmail.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/10808
Tested-by: jenkins <jenkins@lttng.org>
src/cpp-common/Makefile.am
src/cpp-common/log-cfg.hpp [new file with mode: 0644]

index 2849847a2dd21dcd5363587169a4dfbbff8f063b..761a23e0c4b8931873536f3d4ddde40a3fc60504 100644 (file)
@@ -1,3 +1,3 @@
 # SPDX-License-Identifier: MIT
 
-EXTRA_DIST = bt2 optional.hpp string_view.hpp nlohmann/json.hpp
+EXTRA_DIST = bt2 optional.hpp string_view.hpp nlohmann/json.hpp log-cfg.hpp
diff --git a/src/cpp-common/log-cfg.hpp b/src/cpp-common/log-cfg.hpp
new file mode 100644 (file)
index 0000000..2ddfab0
--- /dev/null
@@ -0,0 +1,148 @@
+/*
+ * SPDX-License-Identifier: MIT
+ *
+ * Copyright (c) 2022 Francis Deslauriers <francis.deslauriers@efficios.com>
+ * Copyright (c) 2022 Philippe Proulx <pproulx@efficios.com>
+ */
+
+#ifndef BABELTRACE_CPP_COMMON_LOG_CFG_HPP
+#define BABELTRACE_CPP_COMMON_LOG_CFG_HPP
+
+#include <babeltrace2/babeltrace.h>
+#include "common/assert.h"
+
+namespace bt2_common {
+
+/*
+ * Logging configuration.
+ *
+ * A logging configuration object contains all the execution context
+ * needed to:
+ *
+ * • Log, providing the name of some current component or component
+ *   class.
+ *
+ * • Append a cause to the error of the current thread from the right
+ *   actor.
+ *
+ * For a given logging configuration `L`, the guarantees are as such:
+ *
+ * If `L.selfMsgIter()` isn't `nullptr`:
+ *     • `L.selfComp()` isn't `nullptr`.
+ *     • `L.compCls()` isn't `nullptr`.
+ *     • `L.compName()` isn't `nullptr`.
+ *     • `L.compClsName()` isn't `nullptr`.
+ *     • `L.moduleName()` is `nullptr`.
+ *
+ * If `L.selfComp()` isn't `nullptr`:
+ *     • `L.compCls()` isn't `nullptr`.
+ *     • `L.compName()` isn't `nullptr`.
+ *     • `L.compClsName()` isn't `nullptr`.
+ *     • `L.moduleName()` is `nullptr`.
+ *
+ * If `L.selfCompCls()` isn't `nullptr`:
+ *     • `L.compCls()` isn't `nullptr`.
+ *     • `L.compClsName()` isn't `nullptr`.
+ *     • `L.moduleName()` is `nullptr`.
+ *
+ * If `L.compCls()` isn't `nullptr`:
+ *     • `L.compClsName()` isn't `nullptr`.
+ *     • `L.moduleName()` is `nullptr`.
+ *
+ * If `L.moduleName()` isn't `nullptr`:
+ *     • `L.selfMsgIter()` is `nullptr`.
+ *     • `L.selfComp()` is `nullptr`.
+ *     • `L.compCls()` is `nullptr`.
+ *     • `L.compName()` is `nullptr`.
+ *     • `L.compClsName()` is `nullptr`.
+ */
+class LogCfg final
+{
+public:
+    explicit LogCfg(const bt_logging_level logLevel, bt_self_message_iterator& selfMsgIter) :
+        _mLogLevel {logLevel}, _mSelfMsgIter {&selfMsgIter},
+        _mSelfComp {bt_self_message_iterator_borrow_component(&selfMsgIter)},
+        _mCompCls {&this->_compClsFromSelfComp(*_mSelfComp)}
+    {
+    }
+
+    explicit LogCfg(const bt_logging_level logLevel, bt_self_component& selfComp) :
+        _mLogLevel {logLevel}, _mSelfComp {&selfComp}, _mCompCls {
+                                                           &this->_compClsFromSelfComp(*_mSelfComp)}
+    {
+    }
+
+    explicit LogCfg(const bt_logging_level logLevel, bt_self_component_class& selfCompCls) :
+        _mLogLevel {logLevel}, _mSelfCompCls {&selfCompCls},
+        _mCompCls {bt_self_component_class_as_component_class(&selfCompCls)}
+    {
+    }
+
+    explicit LogCfg(const bt_logging_level logLevel, const char * const moduleName) :
+        _mLogLevel {logLevel}, _mModuleName {moduleName}
+    {
+        BT_ASSERT_DBG(_mModuleName);
+    }
+
+    LogCfg(const LogCfg&) = default;
+    LogCfg& operator=(const LogCfg&) = default;
+
+    bt_logging_level logLevel() const noexcept
+    {
+        return _mLogLevel;
+    }
+
+    bt_self_component *selfComp() const noexcept
+    {
+        return _mSelfComp;
+    }
+
+    const char *compName() const noexcept
+    {
+        BT_ASSERT_DBG(_mSelfComp);
+        return bt_component_get_name(bt_self_component_as_component(_mSelfComp));
+    }
+
+    bt_self_component_class *selfCompCls() const noexcept
+    {
+        return _mSelfCompCls;
+    }
+
+    const bt_component_class *compCls() const noexcept
+    {
+        return _mCompCls;
+    }
+
+    const char *compClsName() const noexcept
+    {
+        BT_ASSERT_DBG(_mCompCls);
+        return bt_component_class_get_name(_mCompCls);
+    }
+
+    bt_self_message_iterator *selfMsgIter() const noexcept
+    {
+        return _mSelfMsgIter;
+    }
+
+    const char *moduleName() const noexcept
+    {
+        return _mModuleName;
+    }
+
+private:
+    static const bt_component_class& _compClsFromSelfComp(bt_self_component& selfComp) noexcept
+    {
+        return *bt_component_borrow_class_const(bt_self_component_as_component(&selfComp));
+    }
+
+    bt_logging_level _mLogLevel;
+    bt_self_message_iterator *_mSelfMsgIter = nullptr;
+    bt_self_component *_mSelfComp = nullptr;
+    bt_self_component_class *_mSelfCompCls = nullptr;
+    const bt_component_class *_mCompCls = nullptr;
+    const char *_mModuleName = nullptr;
+};
+
+} /* namespace bt2_common */
+
+#endif /* BABELTRACE_CPP_COMMON_LOG_CFG_HPP */
This page took 0.02664 seconds and 4 git commands to generate.