Skip to content

Commit c4d28f9

Browse files
BMarchisuab321321
authored andcommitted
Provide logging severity for string (ros2#458)
* Provide logging severity for string Signed-off-by: Brian Ezequiel Marchi <[email protected]> Signed-off-by: AbhinavSingh <[email protected]>
1 parent 813dfa3 commit c4d28f9

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

rclpy/rclpy/logging.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,3 +65,8 @@ def set_logger_level(name, level):
6565
def get_logger_effective_level(name):
6666
logger_level = _rclpy_logging.rclpy_logging_get_logger_effective_level(name)
6767
return LoggingSeverity(logger_level)
68+
69+
70+
def get_logging_severity_from_string(log_severity):
71+
return LoggingSeverity(
72+
_rclpy_logging.rclpy_logging_severity_level_from_string(log_severity))

rclpy/src/rclpy/_rclpy_logging.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include <Python.h>
1616

17+
#include <rcutils/allocator.h>
1718
#include <rcutils/error_handling.h>
1819
#include <rcutils/logging.h>
1920
#include <rcutils/time.h>
@@ -169,6 +170,35 @@ rclpy_logging_rcutils_log(PyObject * Py_UNUSED(self), PyObject * args)
169170
Py_RETURN_NONE;
170171
}
171172

173+
/// Get the log severity based on the log level string representation
174+
/**
175+
* Raises RuntimeError if an invalid log level name is given.
176+
* Raises ValueError if log level is not a string.
177+
*
178+
* \param[in] log_level Name of the log level.
179+
* \return NULL on failure
180+
Log level associated with the string representation
181+
*/
182+
static PyObject *
183+
rclpy_logging_severity_level_from_string(PyObject * Py_UNUSED(self), PyObject * args)
184+
{
185+
int severity;
186+
const char * log_level = NULL;
187+
if (!PyArg_ParseTuple(args, "s", &log_level)) {
188+
return NULL;
189+
}
190+
rcutils_allocator_t allocator = rcutils_get_default_allocator();
191+
rcutils_ret_t ret = rcutils_logging_severity_level_from_string(log_level, allocator, &severity);
192+
if (ret != RCUTILS_RET_OK) {
193+
PyErr_Format(PyExc_RuntimeError,
194+
"Failed to get log severity from name \"%s\", return code: %d\n",
195+
log_level, ret);
196+
rcutils_reset_error();
197+
return NULL;
198+
}
199+
return PyLong_FromLongLong(severity);
200+
}
201+
172202
/// Define the public methods of this module
173203
static PyMethodDef rclpy_logging_methods[] = {
174204
{
@@ -196,6 +226,10 @@ static PyMethodDef rclpy_logging_methods[] = {
196226
"rclpy_logging_rcutils_log", rclpy_logging_rcutils_log, METH_VARARGS,
197227
"Log a message with the specified severity"
198228
},
229+
{
230+
"rclpy_logging_severity_level_from_string", rclpy_logging_severity_level_from_string,
231+
METH_VARARGS, "Determine log level from string"
232+
},
199233

200234
{NULL, NULL, 0, NULL} /* sentinel */
201235
};

rclpy/test/test_logging.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,15 @@ def test_clear_config(self):
359359
rclpy.logging._root_logger.get_effective_level(),
360360
my_logger.get_effective_level())
361361

362+
def test_logging_severity_from_string(self):
363+
for severity in rclpy.logging.LoggingSeverity:
364+
self.assertEqual(
365+
rclpy.logging.get_logging_severity_from_string(severity.name), severity)
366+
367+
def test_nonexistent_logging_severity_from_string(self):
368+
with self.assertRaises(RuntimeError):
369+
rclpy.logging.get_logging_severity_from_string('non_existent_severity')
370+
362371

363372
if __name__ == '__main__':
364373
unittest.main()

0 commit comments

Comments
 (0)