forked from JSBSim-Team/jsbsim
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the
FGLogger
feature to the Python module.
- Loading branch information
Showing
9 changed files
with
358 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
Header: PyLog.cxx | ||
Author: Bertrand Coconnier | ||
Date started: 11/02/24 | ||
------------- Copyright (C) 2024 Bertrand Coconnier ------------- | ||
This program is free software; you can redistribute it and/or modify it under | ||
the terms of the GNU Lesser General Public License as published by the Free | ||
Software Foundation; either version 2 of the License, or (at your option) any | ||
later version. | ||
This program is distributed in the hope that it will be useful, but WITHOUT | ||
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more | ||
details. | ||
You should have received a copy of the GNU Lesser General Public License along | ||
with this program; if not, write to the Free Software Foundation, Inc., | ||
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | ||
Further information about the GNU Lesser General Public License can also be | ||
found on the world wide web at http://www.gnu.org. | ||
*/ | ||
|
||
#include <cassert> | ||
#include "PyLogger.h" | ||
|
||
namespace JSBSim { | ||
// These pointers are initialized in jsbsim.pyx | ||
PyObject* FGLogger_PyClass; | ||
PyObject* LogLevel_PyClass; | ||
PyObject* LogFormat_PyClass; | ||
|
||
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
CLASS IMPLEMENTATION | ||
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ | ||
|
||
PyLogger::PyLogger(PyObject* logger) | ||
{ | ||
if (PyObject_IsInstance(logger, FGLogger_PyClass)) { | ||
logger_pyclass = logger; | ||
Py_INCREF(logger_pyclass); | ||
} else { | ||
PyErr_SetString(PyExc_TypeError, "The logger must be an instance of FGLogger"); | ||
} | ||
} | ||
|
||
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
|
||
void PyLogger::SetLevel(LogLevel level) { | ||
PyObject* 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; | ||
} | ||
|
||
bool success = CallPythonMethodWithArguments("set_level", py_level); | ||
if (success) FGLogger::SetLevel(level); | ||
Py_DECREF(py_level); | ||
} | ||
|
||
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
|
||
void PyLogger::FileLocation(const std::string& filename, int line) | ||
{ | ||
PyObject* py_filename = PyUnicode_FromString(filename.c_str()); | ||
PyObject* py_line = PyLong_FromLong(line); | ||
PyObject* args = PyTuple_Pack(2, py_filename, py_line); | ||
CallPythonMethodWithTuple("file_location", args); | ||
Py_DECREF(args); | ||
Py_DECREF(py_filename); | ||
Py_DECREF(py_line); | ||
} | ||
|
||
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
|
||
void PyLogger::Message(const std::string& message) | ||
{ | ||
PyObject* msg = PyUnicode_FromString(message.c_str()); | ||
CallPythonMethodWithArguments("message", msg); | ||
Py_DECREF(msg); | ||
} | ||
|
||
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
|
||
void PyLogger::Format(LogFormat format) | ||
{ | ||
PyObject* 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; | ||
} | ||
|
||
CallPythonMethodWithArguments("format", py_format); | ||
Py_DECREF(py_format); | ||
} | ||
|
||
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
|
||
bool PyLogger::CallPythonMethodWithArguments(const char* method_name, PyObject* arg) | ||
{ | ||
PyObject* tuple = PyTuple_Pack(1, arg); | ||
bool success = CallPythonMethodWithTuple(method_name, tuple); | ||
Py_DECREF(tuple); | ||
return success; | ||
} | ||
|
||
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
|
||
bool PyLogger::CallPythonMethodWithTuple(const char* method_name, PyObject* tuple) | ||
{ | ||
PyObject* method = PyObject_GetAttrString(logger_pyclass, method_name); | ||
assert(method); // This should not fail as the constructor has checked the type of logger_pyclass. | ||
|
||
PyObject* result = PyObject_CallObject(method, tuple); | ||
Py_DECREF(method); | ||
|
||
if (result) { | ||
Py_DECREF(result); | ||
return true; | ||
} | ||
|
||
PyErr_Print(); | ||
return false; | ||
} | ||
} // namespace JSBSim |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% | ||
Header: PyLog.h | ||
Author: Bertrand Coconnier | ||
Date started: 11/02/24 | ||
------------- Copyright (C) 2024 Bertrand Coconnier ------------- | ||
This program is free software; you can redistribute it and/or modify it under | ||
the terms of the GNU Lesser General Public License as published by the Free | ||
Software Foundation; either version 2 of the License, or (at your option) any | ||
later version. | ||
This program is distributed in the hope that it will be useful, but WITHOUT | ||
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more | ||
details. | ||
You should have received a copy of the GNU Lesser General Public License along | ||
with this program; if not, write to the Free Software Foundation, Inc., | ||
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | ||
Further information about the GNU Lesser General Public License can also be | ||
found on the world wide web at http://www.gnu.org. | ||
*/ | ||
|
||
#include <Python.h> | ||
#include "input_output/FGLog.h" | ||
|
||
#ifndef PYLOG_H | ||
#define PYLOG_H | ||
|
||
namespace JSBSim { | ||
/// Pointers to the Python counterparts of C++ classes. | ||
extern PyObject* FGLogger_PyClass; | ||
extern PyObject* LogLevel_PyClass; | ||
extern PyObject* LogFormat_PyClass; | ||
|
||
class PyLogger : public FGLogger | ||
{ | ||
public: | ||
explicit PyLogger(PyObject* logger); | ||
~PyLogger() override { Py_XDECREF(logger_pyclass); } | ||
void SetLevel(LogLevel level) override; | ||
void FileLocation(const std::string& filename, int line) override; | ||
void Message(const std::string& message) override; | ||
void Format(LogFormat format) override; | ||
void Flush(void) override { CallPythonMethodWithTuple("flush", nullptr); } | ||
|
||
private: | ||
bool CallPythonMethodWithTuple(const char* method_name, PyObject* tuple); | ||
bool CallPythonMethodWithArguments(const char* method_name, PyObject* arg); | ||
|
||
PyObject* logger_pyclass = nullptr; | ||
}; | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.