Console/ILogger: Refactor and extension
=======================================
Refactor:
- Substitute the use of variadic templates with parameter packs.
- Use recently incorporated external library "fmt" to handle printf like formating.
- Extensive cleaning of pragmas and unnecessary forward declarations.
- Parameter packs and libfmt provide a much stronger type checking now, so
conversions that are by standard implicit as bool to int need an explicit static_cast
to avoid compilation warnings.
Extension:
- Include a notifier field, so that the originator of the message can be provided. E.g. Document#DocumentObject
- Include a new type of message called CriticalMessage, this message is intended to have
special behaviour in the future. Namely, it will be used to notify forward compatilibity issues.
It will be used to substitute the current signal/slot mechanism.
- Include two new types of messages for user notifications (Notification and TranslatedNotification). This messages
will be use to convey UI notifications intended for the user (such as non-intrusive message about the usage of a tool). There
are two versions to mark whether the string provided as a message is already translated or not. When using the console system for
notifications, these notifications may originate from the App or the Gui. In the former, it is generally the case that the strings
of messages are not (yet) translated (but they can be marked with QT_TRANSLATE_NOOP). In the latter, often the messages to be provided
are already translated.
Python support for CriticalMessage, Notification and TranslatedNofification, including shortcuts:
Crt = FreeCAD.Console.PrintCritical
Ntf = FreeCAD.Console.PrintNotification
Tnf = FreeCAD.Console.PrintTranslatedNotification
This commit is contained in:
committed by
abdullahtahiriyo
parent
2dac347526
commit
c604d1741d
@@ -1117,7 +1117,7 @@ void Application::setActiveDocument(Gui::Document* pcDocument)
|
||||
// May be useful for error detection
|
||||
if (d->activeDocument) {
|
||||
App::Document* doc = d->activeDocument->getDocument();
|
||||
Base::Console().Log("Active document is %s (at %p)\n",doc->getName(), doc);
|
||||
Base::Console().Log("Active document is %s (at %p)\n",doc->getName(), static_cast<void *>(doc));
|
||||
}
|
||||
else {
|
||||
Base::Console().Log("No active document\n");
|
||||
@@ -1192,7 +1192,7 @@ void Application::viewActivated(MDIView* pcView)
|
||||
#ifdef FC_DEBUG
|
||||
// May be useful for error detection
|
||||
Base::Console().Log("Active view is %s (at %p)\n",
|
||||
(const char*)pcView->windowTitle().toUtf8(),pcView);
|
||||
(const char*)pcView->windowTitle().toUtf8(),static_cast<void *>(pcView));
|
||||
#endif
|
||||
|
||||
signalActivateView(pcView);
|
||||
|
||||
@@ -724,11 +724,13 @@ class TestConsoleObserver : public Base::ILogger
|
||||
{
|
||||
QMutex mutex;
|
||||
public:
|
||||
int matchMsg, matchWrn, matchErr, matchLog;
|
||||
TestConsoleObserver() : matchMsg(0), matchWrn(0), matchErr(0), matchLog(0)
|
||||
int matchMsg, matchWrn, matchErr, matchLog, matchCritical;
|
||||
TestConsoleObserver() : matchMsg(0), matchWrn(0), matchErr(0), matchLog(0), matchCritical(0)
|
||||
{
|
||||
}
|
||||
void SendLog(const std::string& msg, Base::LogStyle level) override{
|
||||
void SendLog(const std::string& notifiername, const std::string& msg, Base::LogStyle level) override{
|
||||
|
||||
(void) notifiername;
|
||||
|
||||
QMutexLocker ml(&mutex);
|
||||
|
||||
@@ -745,6 +747,11 @@ public:
|
||||
case Base::LogStyle::Log:
|
||||
matchLog += strcmp(msg.c_str(), "Write a log to the console output.\n");
|
||||
break;
|
||||
case Base::LogStyle::Critical:
|
||||
matchMsg += strcmp(msg.c_str(), "Write a critical message to the console output.\n");
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -789,6 +796,16 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
class ConsoleCriticalTask : public QRunnable
|
||||
{
|
||||
public:
|
||||
void run() override
|
||||
{
|
||||
for (int i=0; i<10; i++)
|
||||
Base::Console().Critical("Write a critical message to the console output.\n");
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
void CmdTestConsoleOutput::activated(int iMsg)
|
||||
@@ -800,10 +817,11 @@ void CmdTestConsoleOutput::activated(int iMsg)
|
||||
QThreadPool::globalInstance()->start(new ConsoleWarningTask);
|
||||
QThreadPool::globalInstance()->start(new ConsoleErrorTask);
|
||||
QThreadPool::globalInstance()->start(new ConsoleLogTask);
|
||||
QThreadPool::globalInstance()->start(new ConsoleCriticalTask);
|
||||
QThreadPool::globalInstance()->waitForDone();
|
||||
Base::Console().DetachObserver(&obs);
|
||||
|
||||
if (obs.matchMsg > 0 || obs.matchWrn > 0 || obs.matchErr > 0 || obs.matchLog > 0) {
|
||||
if (obs.matchMsg > 0 || obs.matchWrn > 0 || obs.matchErr > 0 || obs.matchLog > 0 || obs.matchCritical > 0) {
|
||||
Base::Console().Error("Race condition in Console class\n");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -81,7 +81,7 @@ GUIConsole::~GUIConsole (void)
|
||||
FreeConsole();
|
||||
}
|
||||
|
||||
void GUIConsole::SendLog(const std::string& msg, Base::LogStyle level)
|
||||
void GUIConsole::SendLog(const std::string& notifiername, const std::string& msg, Base::LogStyle level)
|
||||
{
|
||||
int color = -1;
|
||||
switch(level){
|
||||
@@ -97,6 +97,9 @@ void GUIConsole::SendLog(const std::string& msg, Base::LogStyle level)
|
||||
case Base::LogStyle::Log:
|
||||
color = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
|
||||
break;
|
||||
case Base::LogStyle::Critical:
|
||||
color = FOREGROUND_RED | FOREGROUND_GREEN;
|
||||
break;
|
||||
}
|
||||
|
||||
::SetConsoleTextAttribute(::GetStdHandle(STD_OUTPUT_HANDLE), color);
|
||||
@@ -109,8 +112,10 @@ void GUIConsole::SendLog(const std::string& msg, Base::LogStyle level)
|
||||
// safely ignore GUIConsole::s_nMaxLines and GUIConsole::s_nRefCount
|
||||
GUIConsole::GUIConsole () {}
|
||||
GUIConsole::~GUIConsole () {}
|
||||
void GUIConsole::SendLog(const std::string& msg, Base::LogStyle level)
|
||||
void GUIConsole::SendLog(const std::string& notifiername, const std::string& msg, Base::LogStyle level)
|
||||
{
|
||||
(void) notifiername;
|
||||
|
||||
switch(level){
|
||||
case Base::LogStyle::Warning:
|
||||
std::cerr << "Warning: " << msg;
|
||||
@@ -124,6 +129,11 @@ void GUIConsole::SendLog(const std::string& msg, Base::LogStyle level)
|
||||
case Base::LogStyle::Log:
|
||||
std::clog << msg;
|
||||
break;
|
||||
case Base::LogStyle::Critical:
|
||||
std::cout << "Critical: " << msg;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -47,7 +47,7 @@ public:
|
||||
GUIConsole();
|
||||
/// Destructor
|
||||
~GUIConsole() override;
|
||||
void SendLog(const std::string& msg, Base::LogStyle level) override;
|
||||
void SendLog(const std::string& notifiername, const std::string& msg, Base::LogStyle level) override;
|
||||
const char* Name() override {return "GUIConsole";}
|
||||
|
||||
protected:
|
||||
|
||||
@@ -2146,10 +2146,16 @@ void StatusBarObserver::OnChange(Base::Subject<const char*> &rCaller, const char
|
||||
unsigned long col = rclGrp.GetUnsigned( sReason );
|
||||
this->err = format.arg(App::Color::fromPackedRGB<QColor>(col).name());
|
||||
}
|
||||
else if (strcmp(sReason, "colorCritical") == 0) {
|
||||
unsigned long col = rclGrp.GetUnsigned( sReason );
|
||||
this->critical = format.arg(QColor((col >> 24) & 0xff,(col >> 16) & 0xff,(col >> 8) & 0xff).name());
|
||||
}
|
||||
}
|
||||
|
||||
void StatusBarObserver::SendLog(const std::string& msg, Base::LogStyle level)
|
||||
void StatusBarObserver::SendLog(const std::string& notifiername, const std::string& msg, Base::LogStyle level)
|
||||
{
|
||||
(void) notifiername;
|
||||
|
||||
int messageType = -1;
|
||||
switch(level){
|
||||
case Base::LogStyle::Warning:
|
||||
@@ -2164,6 +2170,11 @@ void StatusBarObserver::SendLog(const std::string& msg, Base::LogStyle level)
|
||||
case Base::LogStyle::Log:
|
||||
messageType = MainWindow::Log;
|
||||
break;
|
||||
case Base::LogStyle::Critical:
|
||||
messageType = MainWindow::Critical;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
// Send the event to the main window to allow thread-safety. Qt will delete it when done.
|
||||
|
||||
@@ -201,7 +201,7 @@ public:
|
||||
|
||||
void updateActions(bool delay = false);
|
||||
|
||||
enum StatusType {None, Err, Wrn, Pane, Msg, Log, Tmp};
|
||||
enum StatusType {None, Err, Wrn, Pane, Msg, Log, Tmp, Critical};
|
||||
void showStatus(int type, const QString & message);
|
||||
|
||||
|
||||
@@ -371,14 +371,14 @@ public:
|
||||
/** Observes its parameter group. */
|
||||
void OnChange(Base::Subject<const char*> &rCaller, const char * sReason) override;
|
||||
|
||||
void SendLog(const std::string& msg, Base::LogStyle level) override;
|
||||
void SendLog(const std::string& notifiername, const std::string& msg, Base::LogStyle level) override;
|
||||
|
||||
/// name of the observer
|
||||
const char *Name() override {return "StatusBar";}
|
||||
|
||||
friend class MainWindow;
|
||||
private:
|
||||
QString msg, wrn, err;
|
||||
QString msg, wrn, err, critical;
|
||||
};
|
||||
|
||||
// -------------------------------------------------------------
|
||||
|
||||
@@ -170,6 +170,9 @@ void ReportHighlighter::highlightBlock (const QString & text)
|
||||
case LogText:
|
||||
setFormat(start, it.length-start, logCol);
|
||||
break;
|
||||
case Critical:
|
||||
setFormat(start, it.length-start, criticalCol);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
@@ -203,6 +206,11 @@ void ReportHighlighter::setErrorColor( const QColor& col )
|
||||
errCol = col;
|
||||
}
|
||||
|
||||
void ReportHighlighter::setCriticalColor( const QColor& col )
|
||||
{
|
||||
criticalCol = col;
|
||||
}
|
||||
|
||||
// ----------------------------------------------------------------------------
|
||||
|
||||
namespace Gui {
|
||||
@@ -245,6 +253,15 @@ public:
|
||||
bool show = showOnError();
|
||||
getGroup()->SetBool("checkShowReportViewOnError", !show);
|
||||
}
|
||||
static bool showOnCritical()
|
||||
{
|
||||
return getGroup()->GetBool("checkShowReportViewOnCritical", false);
|
||||
}
|
||||
static void toggleShowOnCritical()
|
||||
{
|
||||
bool show = showOnMessage();
|
||||
getGroup()->SetBool("checkShowReportViewOnCritical", !show);
|
||||
}
|
||||
|
||||
private:
|
||||
static ParameterGrp::handle getGroup()
|
||||
@@ -329,6 +346,11 @@ bool ReportOutputObserver::eventFilter(QObject *obj, QEvent *event)
|
||||
showReportView();
|
||||
}
|
||||
}
|
||||
else if (msgType == ReportHighlighter::Critical) {
|
||||
if (ReportOutputParameter::showOnCritical()) {
|
||||
showReportView();
|
||||
}
|
||||
}
|
||||
}
|
||||
return false; //true would prevent the messages reaching the report view
|
||||
}
|
||||
@@ -450,8 +472,10 @@ void ReportOutput::restoreFont()
|
||||
setFont(serifFont);
|
||||
}
|
||||
|
||||
void ReportOutput::SendLog(const std::string& msg, Base::LogStyle level)
|
||||
void ReportOutput::SendLog(const std::string& notifiername, const std::string& msg, Base::LogStyle level)
|
||||
{
|
||||
(void) notifiername;
|
||||
|
||||
ReportHighlighter::Paragraph style = ReportHighlighter::LogText;
|
||||
switch (level) {
|
||||
case Base::LogStyle::Warning:
|
||||
@@ -466,6 +490,11 @@ void ReportOutput::SendLog(const std::string& msg, Base::LogStyle level)
|
||||
case Base::LogStyle::Log:
|
||||
style = ReportHighlighter::LogText;
|
||||
break;
|
||||
case Base::LogStyle::Critical:
|
||||
style = ReportHighlighter::Critical;
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
QString qMsg = QString::fromUtf8(msg.c_str());
|
||||
@@ -544,6 +573,7 @@ void ReportOutput::contextMenuEvent ( QContextMenuEvent * e )
|
||||
bool bShowOnNormal = ReportOutputParameter::showOnMessage();
|
||||
bool bShowOnWarn = ReportOutputParameter::showOnWarning();
|
||||
bool bShowOnError = ReportOutputParameter::showOnError();
|
||||
bool bShowOnCritical = ReportOutputParameter::showOnCritical();
|
||||
|
||||
auto menu = new QMenu(this);
|
||||
auto optionMenu = new QMenu( menu );
|
||||
@@ -571,6 +601,10 @@ void ReportOutput::contextMenuEvent ( QContextMenuEvent * e )
|
||||
errAct->setCheckable(true);
|
||||
errAct->setChecked(bErr);
|
||||
|
||||
QAction* logCritical = displayMenu->addAction(tr("Critical messages"), this, &ReportOutput::onToggleCritical);
|
||||
logCritical->setCheckable(true);
|
||||
logCritical->setChecked(bCritical);
|
||||
|
||||
auto showOnMenu = new QMenu (optionMenu);
|
||||
showOnMenu->setTitle(tr("Show Report view on"));
|
||||
optionMenu->addMenu(showOnMenu);
|
||||
@@ -591,6 +625,10 @@ void ReportOutput::contextMenuEvent ( QContextMenuEvent * e )
|
||||
showErrAct->setCheckable(true);
|
||||
showErrAct->setChecked(bShowOnError);
|
||||
|
||||
QAction* showCriticalAct = showOnMenu->addAction(tr("Critical messages"), this, SLOT(onToggleShowReportViewOnCritical()));
|
||||
showCriticalAct->setCheckable(true);
|
||||
showCriticalAct->setChecked(bShowOnCritical);
|
||||
|
||||
optionMenu->addSeparator();
|
||||
|
||||
QAction* stdoutAct = optionMenu->addAction(tr("Redirect Python output"), this, &ReportOutput::onToggleRedirectPythonStdout);
|
||||
@@ -664,6 +702,12 @@ bool ReportOutput::isNormalMessage() const
|
||||
return bMsg;
|
||||
}
|
||||
|
||||
|
||||
bool ReportOutput::isCritical() const
|
||||
{
|
||||
return bCritical;
|
||||
}
|
||||
|
||||
void ReportOutput::onToggleError()
|
||||
{
|
||||
bErr = bErr ? false : true;
|
||||
@@ -688,6 +732,12 @@ void ReportOutput::onToggleNormalMessage()
|
||||
getWindowParameter()->SetBool( "checkMessage", bMsg );
|
||||
}
|
||||
|
||||
void ReportOutput::onToggleCritical()
|
||||
{
|
||||
bCritical = bCritical ? false : true;
|
||||
getWindowParameter()->SetBool( "checkCritical", bCritical );
|
||||
}
|
||||
|
||||
void ReportOutput::onToggleShowReportViewOnWarning()
|
||||
{
|
||||
ReportOutputParameter::toggleShowOnWarning();
|
||||
@@ -703,6 +753,11 @@ void ReportOutput::onToggleShowReportViewOnNormalMessage()
|
||||
ReportOutputParameter::toggleShowOnMessage();
|
||||
}
|
||||
|
||||
void ReportOutput::onToggleShowReportViewOnCritical()
|
||||
{
|
||||
ReportOutputParameter::toggleShowOnCritical();
|
||||
}
|
||||
|
||||
void ReportOutput::onToggleShowReportViewOnLogMessage()
|
||||
{
|
||||
ReportOutputParameter::toggleShowOnLogMessage();
|
||||
@@ -761,10 +816,17 @@ void ReportOutput::OnChange(Base::Subject<const char*> &rCaller, const char * sR
|
||||
else if (strcmp(sReason, "checkMessage") == 0) {
|
||||
bMsg = rclGrp.GetBool( sReason, bMsg );
|
||||
}
|
||||
else if (strcmp(sReason, "checkCritical") == 0) {
|
||||
bMsg = rclGrp.GetBool( sReason, bMsg );
|
||||
}
|
||||
else if (strcmp(sReason, "colorText") == 0) {
|
||||
unsigned long col = rclGrp.GetUnsigned( sReason );
|
||||
reportHl->setTextColor(App::Color::fromPackedRGB<QColor>(col));
|
||||
}
|
||||
else if (strcmp(sReason, "colorCriticalText") == 0) {
|
||||
unsigned long col = rclGrp.GetUnsigned( sReason );
|
||||
reportHl->setTextColor( QColor( (col >> 24) & 0xff,(col >> 16) & 0xff,(col >> 8) & 0xff) );
|
||||
}
|
||||
else if (strcmp(sReason, "colorLogging") == 0) {
|
||||
unsigned long col = rclGrp.GetUnsigned( sReason );
|
||||
reportHl->setLogColor(App::Color::fromPackedRGB<QColor>(col));
|
||||
|
||||
@@ -70,10 +70,11 @@ class GuiExport ReportHighlighter : public QSyntaxHighlighter
|
||||
{
|
||||
public:
|
||||
enum Paragraph {
|
||||
Message = 0, /**< normal text */
|
||||
Warning = 1, /**< Warning */
|
||||
Error = 2, /**< Error text */
|
||||
LogText = 3 /**< Log text */
|
||||
Message = 0, /**< normal text */
|
||||
Warning = 1, /**< Warning */
|
||||
Error = 2, /**< Error text */
|
||||
LogText = 3, /**< Log text */
|
||||
Critical = 4, /**< critical text */
|
||||
};
|
||||
|
||||
public:
|
||||
@@ -87,6 +88,7 @@ public:
|
||||
* @see ReportOutput::Message
|
||||
* @see ReportOutput::Warning
|
||||
* @see ReportOutput::Error
|
||||
* @see ReportOutput::Critical
|
||||
*/
|
||||
void setParagraphType(Paragraph);
|
||||
|
||||
@@ -110,11 +112,16 @@ public:
|
||||
*/
|
||||
void setErrorColor( const QColor& col );
|
||||
|
||||
/**
|
||||
* Sets the text color to \a col.
|
||||
*/
|
||||
void setCriticalColor( const QColor& col );
|
||||
|
||||
private:
|
||||
/** @name for internal use only */
|
||||
//@{
|
||||
Paragraph type;
|
||||
QColor txtCol, logCol, warnCol, errCol;
|
||||
QColor txtCol, logCol, warnCol, errCol, criticalCol;
|
||||
//@}
|
||||
};
|
||||
|
||||
@@ -134,7 +141,7 @@ public:
|
||||
/** Observes its parameter group. */
|
||||
void OnChange(Base::Subject<const char*> &rCaller, const char * sReason) override;
|
||||
|
||||
void SendLog(const std::string& msg, Base::LogStyle level) override;
|
||||
void SendLog(const std::string& notifiername, const std::string& msg, Base::LogStyle level) override;
|
||||
|
||||
/// returns the name for observer handling
|
||||
const char* Name() override {return "ReportOutput";}
|
||||
@@ -150,6 +157,8 @@ public:
|
||||
bool isLogMessage() const;
|
||||
/** Returns true whether normal messages are reported. */
|
||||
bool isNormalMessage() const;
|
||||
/** Returns true whether critical messages are reported. */
|
||||
bool isCritical() const;
|
||||
|
||||
protected:
|
||||
/** For internal use only */
|
||||
@@ -172,12 +181,16 @@ public Q_SLOTS:
|
||||
void onToggleLogMessage();
|
||||
/** Toggles the report of normal messages. */
|
||||
void onToggleNormalMessage();
|
||||
/** Toggles the report of normal messages. */
|
||||
void onToggleCritical();
|
||||
/** Toggles whether to show report view on warnings*/
|
||||
void onToggleShowReportViewOnWarning();
|
||||
/** Toggles whether to show report view on errors*/
|
||||
void onToggleShowReportViewOnError();
|
||||
/** Toggles whether to show report view on normal messages*/
|
||||
void onToggleShowReportViewOnNormalMessage();
|
||||
/** Toggles whether to show report view on normal messages*/
|
||||
void onToggleShowReportViewOnCritical();
|
||||
/** Toggles whether to show report view on log messages*/
|
||||
void onToggleShowReportViewOnLogMessage();
|
||||
/** Toggles the redirection of Python stdout. */
|
||||
|
||||
@@ -123,8 +123,10 @@ public:
|
||||
{
|
||||
return "SplashObserver";
|
||||
}
|
||||
void SendLog(const std::string& msg, Base::LogStyle level) override
|
||||
void SendLog(const std::string& notifiername, const std::string& msg, Base::LogStyle level) override
|
||||
{
|
||||
Q_UNUSED(notifiername)
|
||||
|
||||
#ifdef FC_DEBUG
|
||||
Log(msg.c_str());
|
||||
Q_UNUSED(level)
|
||||
|
||||
@@ -70,11 +70,11 @@ FC_LOG_LEVEL_INIT("Tree", false, true, true)
|
||||
|
||||
#define _TREE_PRINT(_level,_func,_msg) \
|
||||
_FC_PRINT(FC_LOG_INSTANCE,_level,_func, '['<<getTreeName()<<"] " << _msg)
|
||||
#define TREE_MSG(_msg) _TREE_PRINT(FC_LOGLEVEL_MSG,NotifyMessage,_msg)
|
||||
#define TREE_WARN(_msg) _TREE_PRINT(FC_LOGLEVEL_WARN,NotifyWarning,_msg)
|
||||
#define TREE_ERR(_msg) _TREE_PRINT(FC_LOGLEVEL_ERR,NotifyError,_msg)
|
||||
#define TREE_LOG(_msg) _TREE_PRINT(FC_LOGLEVEL_LOG,NotifyLog,_msg)
|
||||
#define TREE_TRACE(_msg) _TREE_PRINT(FC_LOGLEVEL_TRACE,NotifyLog,_msg)
|
||||
#define TREE_MSG(_msg) _TREE_PRINT(FC_LOGLEVEL_MSG,Notify<Base::LogStyle::Message>,_msg)
|
||||
#define TREE_WARN(_msg) _TREE_PRINT(FC_LOGLEVEL_WARN,Notify<Base::LogStyle::Warning>,_msg)
|
||||
#define TREE_ERR(_msg) _TREE_PRINT(FC_LOGLEVEL_ERR,Notify<Base::LogStyle::Error>,_msg)
|
||||
#define TREE_LOG(_msg) _TREE_PRINT(FC_LOGLEVEL_LOG,Notify<Base::LogStyle::Log>,_msg)
|
||||
#define TREE_TRACE(_msg) _TREE_PRINT(FC_LOGLEVEL_TRACE,Notify<Base::LogStyle::Log>,_msg)
|
||||
|
||||
using namespace Gui;
|
||||
namespace bp = boost::placeholders;
|
||||
|
||||
Reference in New Issue
Block a user