|
24 | 24 | * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
25 | 25 | */ |
26 | 26 |
|
| 27 | +/** |
| 28 | + * @file log_base.h |
| 29 | + * @brief Base class for logging in robotkernel modules. |
| 30 | + * |
| 31 | + * Provides a common logging interface and log level configuration |
| 32 | + * support for modules, services, and other components within the |
| 33 | + * robotkernel framework. |
| 34 | + * |
| 35 | + * This class also implements the `svc_configure_loglevel` service |
| 36 | + * used to change log levels at runtime via service calls. |
| 37 | + * |
| 38 | + * Logging output is routed through the robotkernel logging facility. |
| 39 | + * |
| 40 | + * Derived classes should call `log()` with the appropriate level |
| 41 | + * to emit log messages. |
| 42 | + */ |
| 43 | + |
27 | 44 | #ifndef ROBOTKERNEL_LOG_BASE_H |
28 | 45 | #define ROBOTKERNEL_LOG_BASE_H |
29 | 46 |
|
|
35 | 52 |
|
36 | 53 | namespace robotkernel { |
37 | 54 |
|
| 55 | +/** |
| 56 | + * @class log_base |
| 57 | + * @brief Base class implementing logging behavior for components. |
| 58 | + * |
| 59 | + * This class encapsulates: |
| 60 | + * - A configurable log level. |
| 61 | + * - A name and implementation identifier used in log output. |
| 62 | + * - Optional service to configure log levels at runtime. |
| 63 | + * |
| 64 | + * Components deriving from `log_base` gain a consistent logging |
| 65 | + * interface and integration with the robotkernel logging facility. |
| 66 | + */ |
38 | 67 | class log_base : |
39 | 68 | public services::robotkernel::log_base::svc_base_configure_loglevel |
40 | 69 | { |
41 | 70 | private: |
42 | | - log_base(); //!< prevent default construction |
43 | | - |
44 | | - public: |
45 | | - loglevel ll; //!< loglevel loglevel |
46 | | - std::string name; //!< name of module |
47 | | - std::string impl; //!< name of implementation |
48 | | - std::string service_prefix; //!< name of service |
49 | | - |
50 | | - //! construction |
51 | | - /*! |
52 | | - * \param[in] ll Loglevel to set. |
53 | | - */ |
54 | | - log_base(loglevel ll); |
55 | | - |
56 | | - //! construction |
57 | | - /*! |
58 | | - * \param node config node |
59 | | - */ |
60 | | - log_base(const std::string& name, const std::string& impl, |
61 | | - const std::string& service_prefix, const YAML::Node& node = YAML::Node()); |
62 | | - |
63 | | - //! virtual destruction |
64 | | - virtual ~log_base(); |
65 | | - |
66 | | - //! Return current loglevel |
67 | | - const loglevel get_loglevel() const; |
68 | | - |
69 | | - //! Set new loglevel |
70 | | - /*! |
71 | | - * \param[in] ll New loglevel to be set. |
72 | | - */ |
73 | | - void set_loglevel(loglevel ll); |
74 | | - |
75 | | - //! svc_configure_loglevel |
76 | | - /*! |
77 | | - * \param[in] req Service request data. |
78 | | - * \param[out] resp Service response data. |
79 | | - */ |
80 | | - void svc_configure_loglevel( |
81 | | - const struct services::robotkernel::log_base::svc_req_configure_loglevel& req, |
82 | | - struct services::robotkernel::log_base::svc_resp_configure_loglevel& resp) override; |
83 | | - |
84 | | - //! log to kernel logging facility |
85 | | - void log(robotkernel::loglevel lvl, const char *format, ...); |
| 71 | + /** |
| 72 | + * @brief Disabled default constructor. |
| 73 | + * |
| 74 | + * This class must be constructed with at least a log level |
| 75 | + * or with all identifying information present. |
| 76 | + */ |
| 77 | + log_base(); |
| 78 | + |
| 79 | +public: |
| 80 | + /// Current loglevel for this component. |
| 81 | + loglevel ll; |
| 82 | + |
| 83 | + /// Component name used in log output. |
| 84 | + std::string name; |
| 85 | + |
| 86 | + /// Implementation identifier (e.g. module/plugin type). |
| 87 | + std::string impl; |
| 88 | + |
| 89 | + /// Optional prefix for service-related log entries. |
| 90 | + std::string service_prefix; |
| 91 | + |
| 92 | +public: |
| 93 | + /** |
| 94 | + * @brief Construct a log_base with an explicit loglevel. |
| 95 | + * |
| 96 | + * @param[in] ll |
| 97 | + * Initial log level for this component. |
| 98 | + */ |
| 99 | + log_base(loglevel ll); |
| 100 | + |
| 101 | + /** |
| 102 | + * @brief Construct a log_base with identifying information. |
| 103 | + * |
| 104 | + * @param[in] name |
| 105 | + * Human identifier for this instance (e.g., module name). |
| 106 | + * |
| 107 | + * @param[in] impl |
| 108 | + * Implementation name or type. |
| 109 | + * |
| 110 | + * @param[in] service_prefix |
| 111 | + * Optional prefix for log messages from services. |
| 112 | + * |
| 113 | + * @param[in] node |
| 114 | + * YAML configuration node (unused in base, available for derived use). |
| 115 | + */ |
| 116 | + log_base(const std::string& name, |
| 117 | + const std::string& impl, |
| 118 | + const std::string& service_prefix, |
| 119 | + const YAML::Node& node = YAML::Node()); |
| 120 | + |
| 121 | + /** |
| 122 | + * @brief Virtual destructor. |
| 123 | + */ |
| 124 | + virtual ~log_base(); |
| 125 | + |
| 126 | + /** |
| 127 | + * @brief Return the current log level. |
| 128 | + * |
| 129 | + * @return Current loglevel. |
| 130 | + */ |
| 131 | + const loglevel get_loglevel() const; |
| 132 | + |
| 133 | + /** |
| 134 | + * @brief Set a new log level. |
| 135 | + * |
| 136 | + * Used internally and by the `svc_configure_loglevel` service |
| 137 | + * to adjust logging detail at runtime. |
| 138 | + * |
| 139 | + * @param[in] ll New log level value. |
| 140 | + */ |
| 141 | + void set_loglevel(loglevel ll); |
| 142 | + |
| 143 | + /** |
| 144 | + * @brief Service callback for configuring loglevel. |
| 145 | + * |
| 146 | + * Implements the service logic for runtime log level configuration. |
| 147 | + * This override comes from the `svc_base_configure_loglevel` interface. |
| 148 | + * |
| 149 | + * @param[in] req Service request containing a log level. |
| 150 | + * @param[out] resp Service response. |
| 151 | + */ |
| 152 | + void svc_configure_loglevel( |
| 153 | + const struct services::robotkernel::log_base::svc_req_configure_loglevel& req, |
| 154 | + struct services::robotkernel::log_base::svc_resp_configure_loglevel& resp) override; |
| 155 | + |
| 156 | + /** |
| 157 | + * @brief Emit a log message to the robotkernel logging facility. |
| 158 | + * |
| 159 | + * @param[in] lvl |
| 160 | + * Log level to use for this message. |
| 161 | + * |
| 162 | + * @param[in] format |
| 163 | + * printf-style format string. |
| 164 | + * |
| 165 | + * @param[in] … |
| 166 | + * Arguments passed to the format string. |
| 167 | + */ |
| 168 | + void log(robotkernel::loglevel lvl, const char *format, ...); |
86 | 169 | }; |
87 | 170 |
|
88 | | -//! Return current loglevel |
89 | | -inline const loglevel log_base::get_loglevel() const { |
90 | | - return ll; |
91 | | -} |
| 171 | +// ----------------------------------------------------------------------------- |
| 172 | +// Inline member implementations |
| 173 | +// ----------------------------------------------------------------------------- |
92 | 174 |
|
93 | | -//! Set new loglevel |
94 | | -/*! |
95 | | - * \param[in] ll New loglevel to be set. |
| 175 | +/** |
| 176 | + * @brief Return the stored log level. |
| 177 | + * |
| 178 | + * @return The current loglevel. |
96 | 179 | */ |
97 | | -inline void log_base::set_loglevel(loglevel ll) { |
98 | | - this->ll = ll; |
| 180 | +inline const loglevel log_base::get_loglevel() const |
| 181 | +{ |
| 182 | + return ll; |
99 | 183 | } |
100 | 184 |
|
| 185 | +/** |
| 186 | + * @brief Set the stored loglevel. |
| 187 | + * |
| 188 | + * @param[in] ll The new loglevel. |
| 189 | + */ |
| 190 | +inline void log_base::set_loglevel(loglevel ll) |
| 191 | +{ |
| 192 | + this->ll = ll; |
| 193 | +} |
101 | 194 |
|
102 | 195 | }; // namespace robotkernel |
103 | 196 |
|
|
0 commit comments