From 50cefc5104d62fc2d1ba87c7df79a28ff6a6ef5c Mon Sep 17 00:00:00 2001 From: "Zheng, Lei" Date: Fri, 21 Jun 2019 10:13:46 +0800 Subject: [PATCH] Console: improve console logging facility --- src/Base/Console.cpp | 100 +++++++++++++++++++------------------------ src/Base/Console.h | 58 +++++++++++++++---------- 2 files changed, 80 insertions(+), 78 deletions(-) diff --git a/src/Base/Console.cpp b/src/Base/Console.cpp index 3a970657a857..26ce894abecf 100644 --- a/src/Base/Console.cpp +++ b/src/Base/Console.cpp @@ -40,6 +40,7 @@ #include "Exception.h" #include "PyObjectBase.h" #include +#include using namespace Base; @@ -251,18 +252,24 @@ void ConsoleSingleton::SetConnectionMode(ConnectionMode mode) */ void ConsoleSingleton::Message( const char *pMsg, ... ) { - char format[BufferSize]; - const unsigned int format_len = BufferSize; - - va_list namelessVars; - va_start(namelessVars, pMsg); // Get the "..." vars - vsnprintf(format, format_len, pMsg, namelessVars); - va_end(namelessVars); - - if (connectionMode == Direct) - NotifyMessage(format); - else - QCoreApplication::postEvent(ConsoleOutput::getInstance(), new ConsoleEvent(MsgType_Txt, format)); +#define FC_CONSOLE_FMT(_type,_type2) \ + char format[BufferSize];\ + format[sizeof(format)-4] = '.';\ + format[sizeof(format)-3] = '.';\ + format[sizeof(format)-2] = '\n';\ + format[sizeof(format)-1] = 0;\ + const unsigned int format_len = sizeof(format)-4;\ + va_list namelessVars;\ + va_start(namelessVars, pMsg);\ + vsnprintf(format, format_len, pMsg, namelessVars);\ + format[sizeof(format)-5] = '.';\ + va_end(namelessVars);\ + if (connectionMode == Direct)\ + Notify##_type(format);\ + else\ + QCoreApplication::postEvent(ConsoleOutput::getInstance(), new ConsoleEvent(MsgType_##_type2, format)); + + FC_CONSOLE_FMT(Message,Txt); } /** Prints a Message @@ -282,18 +289,7 @@ void ConsoleSingleton::Message( const char *pMsg, ... ) */ void ConsoleSingleton::Warning( const char *pMsg, ... ) { - char format[BufferSize]; - const unsigned int format_len = BufferSize; - - va_list namelessVars; - va_start(namelessVars, pMsg); // Get the "..." vars - vsnprintf(format, format_len, pMsg, namelessVars); - va_end(namelessVars); - - if (connectionMode == Direct) - NotifyWarning(format); - else - QCoreApplication::postEvent(ConsoleOutput::getInstance(), new ConsoleEvent(MsgType_Wrn, format)); + FC_CONSOLE_FMT(Warning,Wrn); } /** Prints a Message @@ -313,18 +309,7 @@ void ConsoleSingleton::Warning( const char *pMsg, ... ) */ void ConsoleSingleton::Error( const char *pMsg, ... ) { - char format[BufferSize]; - const unsigned int format_len = BufferSize; - - va_list namelessVars; - va_start(namelessVars, pMsg); // Get the "..." vars - vsnprintf(format, format_len, pMsg, namelessVars); - va_end(namelessVars); - - if (connectionMode == Direct) - NotifyError(format); - else - QCoreApplication::postEvent(ConsoleOutput::getInstance(), new ConsoleEvent(MsgType_Err, format)); + FC_CONSOLE_FMT(Error,Err); } @@ -346,24 +331,12 @@ void ConsoleSingleton::Error( const char *pMsg, ... ) void ConsoleSingleton::Log( const char *pMsg, ... ) { - char format[BufferSize]; - const unsigned int format_len = BufferSize; - - if (_bVerbose) + if (!_bVerbose) { - va_list namelessVars; - va_start(namelessVars, pMsg); // Get the "..." vars - vsnprintf(format, format_len, pMsg, namelessVars); - va_end(namelessVars); - - if (connectionMode == Direct) - NotifyLog(format); - else - QCoreApplication::postEvent(ConsoleOutput::getInstance(), new ConsoleEvent(MsgType_Log, format)); + FC_CONSOLE_FMT(Log,Log); } } - /** Delivers the time/date * This method gives you a string with the actual time/date. You can * use that for Log() calls to make timestamps. @@ -464,7 +437,7 @@ int *ConsoleSingleton::GetLogLevel(const char *tag, bool create) { void ConsoleSingleton::Refresh() { if(_bCanRefresh) - QCoreApplication::sendPostedEvents(); + qApp->processEvents(QEventLoop::ExcludeUserInputEvents); } void ConsoleSingleton::EnableRefresh(bool enable) { @@ -552,7 +525,7 @@ PyObject *ConsoleSingleton::sPyMessage(PyObject * /*self*/, PyObject *args) PY_TRY { if (string) - Instance().Message("%s",string); // process message + Instance().NotifyMessage(string); // process message } PY_CATCH; Py_XDECREF(unicode); @@ -598,7 +571,7 @@ PyObject *ConsoleSingleton::sPyWarning(PyObject * /*self*/, PyObject *args) PY_TRY { if (string) - Instance().Warning("%s",string); // process message + Instance().NotifyWarning(string); // process message } PY_CATCH; Py_XDECREF(unicode); @@ -644,7 +617,7 @@ PyObject *ConsoleSingleton::sPyError(PyObject * /*self*/, PyObject *args) PY_TRY { if (string) - Instance().Error("%s",string); // process message + Instance().NotifyError(string); // process message } PY_CATCH; Py_XDECREF(unicode); @@ -690,7 +663,7 @@ PyObject *ConsoleSingleton::sPyLog(PyObject * /*self*/, PyObject *args) PY_TRY { if (string) - Instance().Log("%s",string); // process message + Instance().NotifyLog(string); // process message } PY_CATCH; Py_XDECREF(unicode); @@ -968,8 +941,23 @@ std::stringstream &LogLevel::prefix(std::stringstream &str, const char *src, int str << d.count() << ' '; } if(print_tag) str << '<' << tag << "> "; - if(print_src) { + if(print_src==2) { + PyFrameObject* frame = PyEval_GetFrame(); + if(frame) { + line = PyFrame_GetLineNumber(frame); +#if PY_MAJOR_VERSION >= 3 + src = PyUnicode_AsUTF8(frame->f_code->co_filename); +#else + src = PyString_AsString(frame->f_code->co_filename); +#endif + } + } + if(print_src && src && src[0]) { +#ifdef FC_OS_WIN32 + const char *_f = std::strrchr(src, '\\'); +#else const char *_f = std::strrchr(src, '/'); +#endif str << (_f?_f+1:src)<<"("< _aclObservers; @@ -623,12 +637,12 @@ class BaseExport LogLevel { std::string tag; int &lvl; bool print_tag; - bool print_src; + int print_src; bool print_time; bool add_eol; bool refresh; - LogLevel(const char *tag, bool print_tag=true, bool print_src=false, + LogLevel(const char *tag, bool print_tag=true, int print_src=0, bool print_time=false, bool add_eol=true, bool refresh=false) :tag(tag),lvl(*Console().GetLogLevel(tag)) ,print_tag(print_tag),print_src(print_src),print_time(print_time)