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:
Abdullah Tahiri
2023-03-03 12:34:15 +01:00
committed by abdullahtahiriyo
parent 2dac347526
commit c604d1741d
28 changed files with 655 additions and 285 deletions

View File

@@ -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));