diff --git a/src/App/Application.cpp b/src/App/Application.cpp index e432b9d739..195f37337c 100644 --- a/src/App/Application.cpp +++ b/src/App/Application.cpp @@ -190,7 +190,11 @@ PyDoc_STRVAR(FreeCAD_doc, ); PyDoc_STRVAR(Console_doc, - "FreeCAD Console\n" + "FreeCAD Console module.\n\n" + "The Console module contains functions to manage log entries, messages,\n" + "warnings and errors.\n" + "There are also functions to get/set the status of the observers used as\n" + "logging interfaces." ); PyDoc_STRVAR(Base_doc, diff --git a/src/Base/Console.cpp b/src/Base/Console.cpp index ba34901f3e..018255a6d7 100644 --- a/src/Base/Console.cpp +++ b/src/Base/Console.cpp @@ -454,131 +454,80 @@ ConsoleSingleton & ConsoleSingleton::Instance() // ConsoleSingleton Methods // Methods structure PyMethodDef ConsoleSingleton::Methods[] = { {"PrintMessage", ConsoleSingleton::sPyMessage, METH_VARARGS, - "PrintMessage(string) -- Print a message to the output"}, + "PrintMessage(obj) -> None\n\n" + "Print a message to the output.\n\n" + "obj : object\n The string representation is printed."}, {"PrintLog", ConsoleSingleton::sPyLog, METH_VARARGS, - "PrintLog(string) -- Print a log message to the output"}, - {"PrintError" , ConsoleSingleton::sPyError, METH_VARARGS, - "PrintError(string) -- Print an error message to the output"}, + "PrintLog(obj) -> None\n\n" + "Print a log message to the output.\n\n" + "obj : object\n The string representation is printed."}, + {"PrintError", ConsoleSingleton::sPyError, METH_VARARGS, + "PrintError(obj) -> None\n\n" + "Print an error message to the output.\n\n" + "obj : object\n The string representation is printed."}, {"PrintWarning", ConsoleSingleton::sPyWarning, METH_VARARGS, - "PrintWarning -- Print a warning to the output"}, + "PrintWarning(obj) -> None\n\n" + "Print a warning message to the output.\n\n" + "obj : object\n The string representation is printed."}, {"SetStatus", ConsoleSingleton::sPySetStatus, METH_VARARGS, - "Set the status for either Log, Msg, Wrn or Error for an observer"}, + "SetStatus(observer, type, status) -> None\n\n" + "Set the status for either 'Log', 'Msg', 'Wrn' or 'Error' for an observer.\n\n" + "observer : str\n Logging interface name.\n" + "type : str\n Message type.\n" + "status : bool"}, {"GetStatus", ConsoleSingleton::sPyGetStatus, METH_VARARGS, - "Get the status for either Log, Msg, Wrn or Error for an observer"}, + "GetStatus(observer, type) -> bool or None\n\n" + "Get the status for either 'Log', 'Msg', 'Wrn' or 'Error' for an observer.\n" + "Returns None if the specified observer doesn't exist.\n\n" + "observer : str\n Logging interface name.\n" + "type : str\n Message type."}, + {"GetObservers", ConsoleSingleton::sPyGetObservers, METH_VARARGS, + "GetObservers() -> list of str\n\n" + "Get the names of the current logging interfaces."}, {nullptr, nullptr, 0, nullptr} /* Sentinel */ }; +#define FC_PYCONSOLE_MSG(func, args) \ + PyObject *output;\ + if (!PyArg_ParseTuple(args, "O", &output))\ + return nullptr;\ + PY_TRY {\ + const char* string = nullptr;\ + PyObject* unicode = nullptr;\ + if (PyUnicode_Check(output)) {\ + string = PyUnicode_AsUTF8(output);\ + }\ + else {\ + unicode = PyObject_Str(output);\ + if (unicode)\ + string = PyUnicode_AsUTF8(unicode);\ + }\ + if (string)\ + func("%s",string); /*process message*/\ + Py_XDECREF(unicode);\ + }\ + PY_CATCH\ + Py_Return; + PyObject *ConsoleSingleton::sPyMessage(PyObject * /*self*/, PyObject *args) { - PyObject *output; - if (!PyArg_ParseTuple(args, "O", &output)) - return nullptr; - - const char* string=nullptr; - PyObject* unicode=nullptr; - if (PyUnicode_Check(output)) { - string = PyUnicode_AsUTF8(output); - } - else { - unicode = PyObject_Str(output); - if (unicode) - string = PyUnicode_AsUTF8(unicode); - } - - PY_TRY { - if (string) - Instance().Message("%s",string); // process message - } PY_CATCH; - - Py_XDECREF(unicode); - - Py_INCREF(Py_None); - return Py_None; + FC_PYCONSOLE_MSG(Instance().Message, args); } PyObject *ConsoleSingleton::sPyWarning(PyObject * /*self*/, PyObject *args) { - PyObject *output; - if (!PyArg_ParseTuple(args, "O", &output)) - return nullptr; - - const char* string=nullptr; - PyObject* unicode=nullptr; - if (PyUnicode_Check(output)) { - string = PyUnicode_AsUTF8(output); - } - else { - unicode = PyObject_Str(output); - if (unicode) - string = PyUnicode_AsUTF8(unicode); - } - - PY_TRY { - if (string) - Instance().Warning("%s",string); // process message - } PY_CATCH; - - Py_XDECREF(unicode); - - Py_INCREF(Py_None); - return Py_None; + FC_PYCONSOLE_MSG(Instance().Warning, args); } PyObject *ConsoleSingleton::sPyError(PyObject * /*self*/, PyObject *args) { - PyObject *output; - if (!PyArg_ParseTuple(args, "O", &output)) - return nullptr; - - const char* string=nullptr; - PyObject* unicode=nullptr; - if (PyUnicode_Check(output)) { - string = PyUnicode_AsUTF8(output); - } - else { - unicode = PyObject_Str(output); - if (unicode) - string = PyUnicode_AsUTF8(unicode); - } - - PY_TRY { - if (string) - Instance().Error("%s",string); // process message - } PY_CATCH; - - Py_XDECREF(unicode); - - Py_INCREF(Py_None); - return Py_None; + FC_PYCONSOLE_MSG(Instance().Error, args); } PyObject *ConsoleSingleton::sPyLog(PyObject * /*self*/, PyObject *args) { - PyObject *output; - if (!PyArg_ParseTuple(args, "O", &output)) - return nullptr; - - const char* string=nullptr; - PyObject* unicode=nullptr; - if (PyUnicode_Check(output)) { - string = PyUnicode_AsUTF8(output); - } - else { - unicode = PyObject_Str(output); - if (unicode) - string = PyUnicode_AsUTF8(unicode); - } - - PY_TRY { - if (string) - Instance().Log("%s",string); // process message - } PY_CATCH; - - Py_XDECREF(unicode); - - Py_INCREF(Py_None); - return Py_None; + FC_PYCONSOLE_MSG(Instance().Log, args); } PyObject *ConsoleSingleton::sPyGetStatus(PyObject * /*self*/, PyObject *args) @@ -592,10 +541,7 @@ PyObject *ConsoleSingleton::sPyGetStatus(PyObject * /*self*/, PyObject *args) bool b=false; ILogger *pObs = Instance().Get(pstr1); if (!pObs) - { - Py_INCREF(Py_None); - return Py_None; - } + Py_Return; if (strcmp(pstr2,"Log") == 0) b = pObs->bLog; @@ -605,42 +551,60 @@ PyObject *ConsoleSingleton::sPyGetStatus(PyObject * /*self*/, PyObject *args) b = pObs->bMsg; else if (strcmp(pstr2,"Err") == 0) b = pObs->bErr; + else + Py_Error(Base::PyExc_FC_GeneralError,"Unknown message type (use 'Log', 'Err', 'Msg' or 'Wrn')"); - return Py_BuildValue("i",b?1:0); - }PY_CATCH; + return PyBool_FromLong(b ? 1 : 0); + } + PY_CATCH; } PyObject *ConsoleSingleton::sPySetStatus(PyObject * /*self*/, PyObject *args) { char *pstr1; char *pstr2; - int Bool; - if (!PyArg_ParseTuple(args, "ssi", &pstr1, &pstr2,&Bool)) + PyObject* pyStatus; + if (!PyArg_ParseTuple(args, "ssO!", &pstr1, &pstr2, &PyBool_Type, &pyStatus)) return nullptr; PY_TRY{ + bool status = asBoolean(pyStatus); ILogger *pObs = Instance().Get(pstr1); - if (pObs) - { + if (pObs) { if (strcmp(pstr2,"Log") == 0) - pObs->bLog = (Bool==0)?false:true; + pObs->bLog = status; else if (strcmp(pstr2,"Wrn") == 0) - pObs->bWrn = (Bool==0)?false:true; + pObs->bWrn = status; else if (strcmp(pstr2,"Msg") == 0) - pObs->bMsg = (Bool==0)?false:true; + pObs->bMsg = status; else if (strcmp(pstr2,"Err") == 0) - pObs->bErr = (Bool==0)?false:true; + pObs->bErr = status; else - Py_Error(Base::PyExc_FC_GeneralError,"Unknown Message Type (use Log, Err, Msg or Wrn)"); + Py_Error(Base::PyExc_FC_GeneralError,"Unknown message type (use 'Log', 'Err', 'Msg' or 'Wrn')"); - Py_INCREF(Py_None); - return Py_None; + Py_Return; } else { - Py_Error(Base::PyExc_FC_GeneralError,"Unknown Console Type"); + Py_Error(Base::PyExc_FC_GeneralError,"Unknown logger type"); } - } PY_CATCH; + } + PY_CATCH; +} + +PyObject *ConsoleSingleton::sPyGetObservers(PyObject * /*self*/, PyObject *args) +{ + if (!PyArg_ParseTuple(args, "")) + return nullptr; + + PY_TRY { + Py::List list; + for (auto i : Instance()._aclObservers) + list.append(Py::String(i->Name() ? i->Name() : "")); + + return Py::new_reference_to(list); + } + PY_CATCH } Base::ILogger::~ILogger() diff --git a/src/Base/Console.h b/src/Base/Console.h index 07928e9694..38d1d0de87 100644 --- a/src/Base/Console.h +++ b/src/Base/Console.h @@ -597,6 +597,7 @@ protected: static PyObject *sPyError (PyObject *self,PyObject *args); static PyObject *sPySetStatus(PyObject *self,PyObject *args); static PyObject *sPyGetStatus(PyObject *self,PyObject *args); + static PyObject *sPyGetObservers(PyObject *self, PyObject *args); bool _bVerbose; bool _bCanRefresh;