diff --git a/python/PyLogger.cxx b/python/PyLogger.cxx index 5b97beb07..eb3689c18 100644 --- a/python/PyLogger.cxx +++ b/python/PyLogger.cxx @@ -42,6 +42,24 @@ PyLogger::PyLogger(PyObject* logger) if (PyObject_IsInstance(logger, FGLogger_PyClass)) { logger_pyclass = logger; Py_INCREF(logger); + + convert_level_enums[LogLevel::BULK] = PyObject_GetAttrString(LogLevel_PyClass, "BULK"); + convert_level_enums[LogLevel::DEBUG] = PyObject_GetAttrString(LogLevel_PyClass, "DEBUG"); + convert_level_enums[LogLevel::INFO] = PyObject_GetAttrString(LogLevel_PyClass, "INFO"); + convert_level_enums[LogLevel::WARN] = PyObject_GetAttrString(LogLevel_PyClass, "WARN"); + convert_level_enums[LogLevel::ERROR] = PyObject_GetAttrString(LogLevel_PyClass, "ERROR"); + convert_level_enums[LogLevel::FATAL] = PyObject_GetAttrString(LogLevel_PyClass, "FATAL"); + + convert_format_enums[LogFormat::RESET] = PyObject_GetAttrString(LogFormat_PyClass, "RESET"); + convert_format_enums[LogFormat::RED] = PyObject_GetAttrString(LogFormat_PyClass, "RED"); + convert_format_enums[LogFormat::BLUE] = PyObject_GetAttrString(LogFormat_PyClass, "BLUE"); + convert_format_enums[LogFormat::CYAN] = PyObject_GetAttrString(LogFormat_PyClass, "CYAN"); + convert_format_enums[LogFormat::GREEN] = PyObject_GetAttrString(LogFormat_PyClass, "GREEN"); + convert_format_enums[LogFormat::DEFAULT] = PyObject_GetAttrString(LogFormat_PyClass, "DEFAULT"); + convert_format_enums[LogFormat::BOLD] = PyObject_GetAttrString(LogFormat_PyClass, "BOLD"); + convert_format_enums[LogFormat::NORMAL] = PyObject_GetAttrString(LogFormat_PyClass, "NORMAL"); + convert_format_enums[LogFormat::UNDERLINE_ON] = PyObject_GetAttrString(LogFormat_PyClass, "UNDERLINE_ON"); + convert_format_enums[LogFormat::UNDERLINE_OFF] = PyObject_GetAttrString(LogFormat_PyClass, "UNDERLINE_OFF"); } else { PyErr_SetString(PyExc_TypeError, "The logger must be an instance of FGLogger"); } @@ -50,30 +68,8 @@ PyLogger::PyLogger(PyObject* logger) //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% void PyLogger::SetLevel(LogLevel level) { - PyObjectPtr py_level; - - switch (level) - { - case LogLevel::BULK: - py_level = PyObject_GetAttrString(LogLevel_PyClass, "BULK"); - break; - case LogLevel::DEBUG: - py_level = PyObject_GetAttrString(LogLevel_PyClass, "DEBUG"); - break; - case LogLevel::INFO: - py_level = PyObject_GetAttrString(LogLevel_PyClass, "INFO"); - break; - case LogLevel::WARN: - py_level = PyObject_GetAttrString(LogLevel_PyClass, "WARN"); - break; - case LogLevel::ERROR: - py_level = PyObject_GetAttrString(LogLevel_PyClass, "ERROR"); - break; - case LogLevel::FATAL: - py_level = PyObject_GetAttrString(LogLevel_PyClass, "FATAL"); - break; - } - + assert(convert_level_enums.find(level) != convert_level_enums.end()); + PyObjectPtr py_level = convert_level_enums[level]; PyObjectPtr result = CallPythonMethodWithArguments("set_level", py_level); if (result) FGLogger::SetLevel(level); } @@ -100,42 +96,8 @@ void PyLogger::Message(const std::string& message) void PyLogger::Format(LogFormat format) { - PyObjectPtr py_format; - - switch (format) - { - case LogFormat::RESET: - py_format = PyObject_GetAttrString(LogFormat_PyClass, "RESET"); - break; - case LogFormat::RED: - py_format = PyObject_GetAttrString(LogFormat_PyClass, "RED"); - break; - case LogFormat::BLUE: - py_format = PyObject_GetAttrString(LogFormat_PyClass, "BLUE"); - break; - case LogFormat::CYAN: - py_format = PyObject_GetAttrString(LogFormat_PyClass, "CYAN"); - break; - case LogFormat::GREEN: - py_format = PyObject_GetAttrString(LogFormat_PyClass, "GREEN"); - break; - case LogFormat::DEFAULT: - py_format = PyObject_GetAttrString(LogFormat_PyClass, "DEFAULT"); - break; - case LogFormat::BOLD: - py_format = PyObject_GetAttrString(LogFormat_PyClass, "BOLD"); - break; - case LogFormat::NORMAL: - py_format = PyObject_GetAttrString(LogFormat_PyClass, "NORMAL"); - break; - case LogFormat::UNDERLINE_ON: - py_format = PyObject_GetAttrString(LogFormat_PyClass, "UNDERLINE_ON"); - break; - case LogFormat::UNDERLINE_OFF: - py_format = PyObject_GetAttrString(LogFormat_PyClass, "UNDERLINE_OFF"); - break; - } - + assert(convert_format_enums.find(format) != convert_format_enums.end()); + PyObjectPtr py_format = convert_format_enums[format]; CallPythonMethodWithArguments("format", py_format); } diff --git a/python/PyLogger.h b/python/PyLogger.h index 54ab6cedf..eff3b9dd8 100644 --- a/python/PyLogger.h +++ b/python/PyLogger.h @@ -24,6 +24,7 @@ found on the world wide web at http://www.gnu.org. */ +#include #include #include "input_output/FGLog.h" @@ -106,6 +107,8 @@ class PyLogger : public FGLogger PyObjectPtr CallPythonMethodWithArguments(const char* method_name, const PyObjectPtr& arg); PyObjectPtr logger_pyclass; + std::map convert_level_enums; + std::map convert_format_enums; }; } #endif diff --git a/python/jsbsim.pyx.in b/python/jsbsim.pyx.in index 8135498f7..59efdbf33 100644 --- a/python/jsbsim.pyx.in +++ b/python/jsbsim.pyx.in @@ -152,7 +152,7 @@ def inherit_docstring(cls): class DefaultLogger(FGLogger): """Default logger: print messages to stdout without formatting.""" def file_location(self, filename: str, line: int) -> None: - print(f"\nIn file {filename}: line {line}", end='') + print(f"\nIn file {filename}: line {line}") def message(self, message: str) -> None: print(f"{message}", end='')