Refactor and rename ConsoleObserver...
...Renamed to "ILogger", to designate that this is an Interface for a Logger. This "Interface" is pure virtual, so that it cannot be instantiated directly. This makes it clear that it is intended to be derived. Finally, got rid of all the individual log-style methods and replaced with SendLog. The idea here is that day-to-day users will only interact with ILogger through ConsoleSingleton (or, likely, LoggerSingleton in the future). This singleton will manage an arbirtary collection of ILogger, and call SendLog with the appropriate parameters based on what the user requests. Therefore, the singleton itself will have the individual Log, Message, Error, etc... methods, while stil allowing us to simplify the code base of ILogger and its derived classes.
This commit is contained in:
@@ -424,9 +424,9 @@ QPixmap BitmapFactoryInst::pixmapFromSvg(const QByteArray& contents, const QSize
|
||||
|
||||
QPainter p(&image);
|
||||
// tmp. disable the report window to suppress some bothering warnings
|
||||
Base::Console().SetEnabledMsgType("ReportOutput", ConsoleMsgType::MsgType_Wrn, false);
|
||||
Base::Console().SetEnabledMsgType("ReportOutput", Base::ConsoleSingleton::MsgType_Wrn, false);
|
||||
QSvgRenderer svg(contents);
|
||||
Base::Console().SetEnabledMsgType("ReportOutput", ConsoleMsgType::MsgType_Wrn, true);
|
||||
Base::Console().SetEnabledMsgType("ReportOutput", Base::ConsoleSingleton::MsgType_Wrn, true);
|
||||
svg.render(&p);
|
||||
p.end();
|
||||
|
||||
|
||||
@@ -667,7 +667,7 @@ CmdTestConsoleOutput::CmdTestConsoleOutput()
|
||||
}
|
||||
|
||||
namespace Gui {
|
||||
class TestConsoleObserver : public Base::ConsoleObserver
|
||||
class TestConsoleObserver : public Base::ILogger
|
||||
{
|
||||
QMutex mutex;
|
||||
public:
|
||||
@@ -675,25 +675,24 @@ public:
|
||||
TestConsoleObserver() : matchMsg(0), matchWrn(0), matchErr(0), matchLog(0)
|
||||
{
|
||||
}
|
||||
virtual void Warning(const char * msg)
|
||||
{
|
||||
void SendLog(const std::string& msg, Base::LogStyle level){
|
||||
|
||||
QMutexLocker ml(&mutex);
|
||||
matchWrn += strcmp(msg, "Write a warning to the console output.\n");
|
||||
}
|
||||
virtual void Message(const char * msg)
|
||||
{
|
||||
QMutexLocker ml(&mutex);
|
||||
matchMsg += strcmp(msg, "Write a message to the console output.\n");
|
||||
}
|
||||
virtual void Error(const char * msg)
|
||||
{
|
||||
QMutexLocker ml(&mutex);
|
||||
matchErr += strcmp(msg, "Write an error to the console output.\n");
|
||||
}
|
||||
virtual void Log(const char * msg)
|
||||
{
|
||||
QMutexLocker ml(&mutex);
|
||||
matchLog += strcmp(msg, "Write a log to the console output.\n");
|
||||
|
||||
switch(level){
|
||||
case Base::LogStyle::Warning:
|
||||
matchWrn += strcmp(msg.c_str(), "Write a warning to the console output.\n");
|
||||
break;
|
||||
case Base::LogStyle::Message:
|
||||
matchMsg += strcmp(msg.c_str(), "Write a message to the console output.\n");
|
||||
break;
|
||||
case Base::LogStyle::Error:
|
||||
matchErr += strcmp(msg.c_str(), "Write an error to the console output.\n");
|
||||
break;
|
||||
case Base::LogStyle::Log:
|
||||
matchLog += strcmp(msg.c_str(), "Write a log to the console output.\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -84,30 +84,27 @@ GUIConsole::~GUIConsole (void)
|
||||
FreeConsole();
|
||||
}
|
||||
|
||||
void GUIConsole::Message(const char *sMsg)
|
||||
void GUIConsole::SendLog(const std::string& msg, Base::LogStyle level)
|
||||
{
|
||||
::SetConsoleTextAttribute(::GetStdHandle(STD_OUTPUT_HANDLE), FOREGROUND_GREEN);
|
||||
printf("%s",sMsg);
|
||||
::SetConsoleTextAttribute(::GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE );
|
||||
}
|
||||
int color = -1;
|
||||
switch(level){
|
||||
case Base::LogStyle::Warning:
|
||||
color = FOREGROUND_RED | FOREGROUND_GREEN;
|
||||
break;
|
||||
case Base::LogStyle::Message:
|
||||
color = FOREGROUND_GREEN;
|
||||
break;
|
||||
case Base::LogStyle::Error:
|
||||
color = FOREGROUND_RED;
|
||||
break;
|
||||
case Base::LogStyle::Log:
|
||||
color = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
|
||||
break;
|
||||
}
|
||||
|
||||
void GUIConsole::Warning(const char *sWarn)
|
||||
{
|
||||
::SetConsoleTextAttribute(::GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_RED | FOREGROUND_GREEN);
|
||||
printf("%s",sWarn);
|
||||
::SetConsoleTextAttribute(::GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE );
|
||||
}
|
||||
|
||||
void GUIConsole::Error (const char *sErr)
|
||||
{
|
||||
::SetConsoleTextAttribute(::GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_RED );
|
||||
printf("%s",sErr);
|
||||
::SetConsoleTextAttribute(::GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE );
|
||||
}
|
||||
|
||||
void GUIConsole::Log (const char *sLog)
|
||||
{
|
||||
printf("%s",sLog);
|
||||
::SetConsoleTextAttribute(::GetStdHandle(STD_OUTPUT_HANDLE), color);
|
||||
printf("%s", msg.c_str());
|
||||
::SetConsoleTextAttribute(::GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE );
|
||||
}
|
||||
|
||||
#else /* FC_OS_LINUX */
|
||||
@@ -115,9 +112,22 @@ void GUIConsole::Log (const char *sLog)
|
||||
// safely ignore GUIConsole::s_nMaxLines and GUIConsole::s_nRefCount
|
||||
GUIConsole::GUIConsole (void) {}
|
||||
GUIConsole::~GUIConsole (void) {}
|
||||
void GUIConsole::Message(const char *sMsg) { std::cout<<sMsg; }
|
||||
void GUIConsole::Warning(const char *sWarn){ std::cerr<<"Warning: "<<sWarn; }
|
||||
void GUIConsole::Error (const char *sErr) { std::cerr<<"Error: "<<sErr;}
|
||||
void GUIConsole::Log (const char *sLog) { std::clog<<sLog;}
|
||||
void GUIConsole::SendLog(const std::string& msg, Base::LogStyle level)
|
||||
{
|
||||
switch(level){
|
||||
case Base::LogStyle::Warning:
|
||||
std::cerr << "Warning: " << msg;
|
||||
break;
|
||||
case Base::LogStyle::Message:
|
||||
std::cout << msg;
|
||||
break;
|
||||
case Base::LogStyle::Error:
|
||||
std::cerr << "Error: " << msg;
|
||||
break;
|
||||
case Base::LogStyle::Log:
|
||||
std::clog << msg;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
#endif /* FC_OS_LINUX */
|
||||
|
||||
@@ -40,21 +40,15 @@ namespace Gui {
|
||||
* @see FCConsole
|
||||
* \author Jürgen Riegel
|
||||
*/
|
||||
class GuiExport GUIConsole :public Base::ConsoleObserver
|
||||
class GuiExport GUIConsole :public Base::ILogger
|
||||
{
|
||||
public:
|
||||
/// Constructor
|
||||
GUIConsole(void);
|
||||
/// Destructor
|
||||
virtual ~GUIConsole(void);
|
||||
//@{
|
||||
/** Observer implementation */
|
||||
virtual void Warning(const char *sWarn);
|
||||
virtual void Message(const char *sMsg);
|
||||
virtual void Error (const char *sErr);
|
||||
virtual void Log (const char *sErr);
|
||||
void SendLog(const std::string& msg, Base::LogStyle level) override;
|
||||
const char* Name(void){return "GUIConsole";}
|
||||
//@}
|
||||
|
||||
protected:
|
||||
static const unsigned int s_nMaxLines;
|
||||
|
||||
@@ -1350,10 +1350,10 @@ void MainWindow::loadWindowSettings()
|
||||
this->move(pos);
|
||||
|
||||
// tmp. disable the report window to suppress some bothering warnings
|
||||
Base::Console().SetEnabledMsgType("ReportOutput", ConsoleMsgType::MsgType_Wrn, false);
|
||||
Base::Console().SetEnabledMsgType("ReportOutput", Base::ConsoleSingleton::MsgType_Wrn, false);
|
||||
this->restoreState(config.value(QString::fromLatin1("MainWindowState")).toByteArray());
|
||||
std::clog << "Main window restored" << std::endl;
|
||||
Base::Console().SetEnabledMsgType("ReportOutput", ConsoleMsgType::MsgType_Wrn, true);
|
||||
Base::Console().SetEnabledMsgType("ReportOutput", Base::ConsoleSingleton::MsgType_Wrn, true);
|
||||
|
||||
bool max = config.value(QString::fromLatin1("Maximized"), false).toBool();
|
||||
max ? showMaximized() : show();
|
||||
@@ -1969,43 +1969,26 @@ void StatusBarObserver::OnChange(Base::Subject<const char*> &rCaller, const char
|
||||
}
|
||||
}
|
||||
|
||||
/** Get called when a message is issued.
|
||||
* The message is displayed on the ststus bar.
|
||||
*/
|
||||
void StatusBarObserver::Message(const char * m)
|
||||
void StatusBarObserver::SendLog(const std::string& msg, Base::LogStyle level)
|
||||
{
|
||||
// Send the event to the main window to allow thread-safety. Qt will delete it when done.
|
||||
CustomMessageEvent* ev = new CustomMessageEvent(MainWindow::Msg, QString::fromUtf8(m));
|
||||
QApplication::postEvent(getMainWindow(), ev);
|
||||
}
|
||||
int messageType = -1;
|
||||
switch(level){
|
||||
case Base::LogStyle::Warning:
|
||||
messageType = MainWindow::Wrn;
|
||||
break;
|
||||
case Base::LogStyle::Message:
|
||||
messageType = MainWindow::Msg;
|
||||
break;
|
||||
case Base::LogStyle::Error:
|
||||
messageType = MainWindow::Err;
|
||||
break;
|
||||
case Base::LogStyle::Log:
|
||||
messageType = MainWindow::Log;
|
||||
break;
|
||||
}
|
||||
|
||||
/** Get called when a warning is issued.
|
||||
* The message is displayed on the ststus bar.
|
||||
*/
|
||||
void StatusBarObserver::Warning(const char *m)
|
||||
{
|
||||
// Send the event to the main window to allow thread-safety. Qt will delete it when done.
|
||||
CustomMessageEvent* ev = new CustomMessageEvent(MainWindow::Wrn, QString::fromUtf8(m));
|
||||
QApplication::postEvent(getMainWindow(), ev);
|
||||
}
|
||||
|
||||
/** Get called when an error is issued.
|
||||
* The message is displayed on the ststus bar.
|
||||
*/
|
||||
void StatusBarObserver::Error (const char *m)
|
||||
{
|
||||
// Send the event to the main window to allow thread-safety. Qt will delete it when done.
|
||||
CustomMessageEvent* ev = new CustomMessageEvent(MainWindow::Err, QString::fromUtf8(m));
|
||||
QApplication::postEvent(getMainWindow(), ev);
|
||||
}
|
||||
|
||||
/** Get called when a log message is issued.
|
||||
* The message is used to create an Inventor node for debug purposes.
|
||||
*/
|
||||
void StatusBarObserver::Log(const char *m)
|
||||
{
|
||||
// Send the event to the main window to allow thread-safety. Qt will delete it when done.
|
||||
CustomMessageEvent* ev = new CustomMessageEvent(MainWindow::Log, QString::fromUtf8(m));
|
||||
CustomMessageEvent* ev = new CustomMessageEvent(messageType, QString::fromUtf8(msg.c_str()));
|
||||
QApplication::postEvent(getMainWindow(), ev);
|
||||
}
|
||||
|
||||
|
||||
@@ -330,10 +330,10 @@ inline MainWindow* getMainWindow()
|
||||
* error messages are in red. Log messages are completely ignored.
|
||||
* The class is implemented to be thread-safe.
|
||||
* @see Console
|
||||
* @see ConsoleObserver
|
||||
* @see ILogger
|
||||
* @author Werner Mayer
|
||||
*/
|
||||
class StatusBarObserver: public WindowParameter, public Base::ConsoleObserver
|
||||
class StatusBarObserver: public WindowParameter, public Base::ILogger
|
||||
{
|
||||
public:
|
||||
StatusBarObserver();
|
||||
@@ -342,14 +342,8 @@ public:
|
||||
/** Observes its parameter group. */
|
||||
void OnChange(Base::Subject<const char*> &rCaller, const char * sReason);
|
||||
|
||||
/// get called when a Warning is issued
|
||||
void Warning(const char *m);
|
||||
/// get called when a Message is issued
|
||||
void Message(const char * m);
|
||||
/// get called when a Error is issued
|
||||
void Error (const char *m);
|
||||
/// get called when a Log Message is issued
|
||||
void Log (const char *);
|
||||
void SendLog(const std::string& msg, Base::LogStyle level) override;
|
||||
|
||||
/// name of the observer
|
||||
const char *Name(void){return "StatusBar";}
|
||||
|
||||
|
||||
@@ -381,36 +381,31 @@ void ReportOutput::restoreFont()
|
||||
setFont(serifFont);
|
||||
}
|
||||
|
||||
void ReportOutput::Warning(const char * s)
|
||||
void ReportOutput::SendLog(const std::string& msg, Base::LogStyle level)
|
||||
{
|
||||
// Send the event to itself to allow thread-safety. Qt will delete it when done.
|
||||
CustomReportEvent* ev = new CustomReportEvent(ReportHighlighter::Warning, QString::fromUtf8(s));
|
||||
QApplication::postEvent(this, ev);
|
||||
}
|
||||
|
||||
void ReportOutput::Message(const char * s)
|
||||
{
|
||||
// Send the event to itself to allow thread-safety. Qt will delete it when done.
|
||||
CustomReportEvent* ev = new CustomReportEvent(ReportHighlighter::Message, QString::fromUtf8(s));
|
||||
QApplication::postEvent(this, ev);
|
||||
}
|
||||
|
||||
void ReportOutput::Error (const char * s)
|
||||
{
|
||||
// Send the event to itself to allow thread-safety. Qt will delete it when done.
|
||||
CustomReportEvent* ev = new CustomReportEvent(ReportHighlighter::Error, QString::fromUtf8(s));
|
||||
QApplication::postEvent(this, ev);
|
||||
}
|
||||
|
||||
void ReportOutput::Log (const char * s)
|
||||
{
|
||||
QString msg = QString::fromUtf8(s);
|
||||
if(messageSize>0 && msg.size()>messageSize) {
|
||||
msg.truncate(messageSize);
|
||||
msg += QString::fromLatin1("...\n");
|
||||
ReportHighlighter::Paragraph style = ReportHighlighter::LogText;
|
||||
switch(level){
|
||||
case Base::LogStyle::Warning:
|
||||
style = ReportHighlighter::Warning;
|
||||
break;
|
||||
case Base::LogStyle::Message:
|
||||
style = ReportHighlighter::Message;
|
||||
break;
|
||||
case Base::LogStyle::Error:
|
||||
style = ReportHighlighter::Error;
|
||||
break;
|
||||
case Base::LogStyle::Log:
|
||||
style = ReportHighlighter::LogText;
|
||||
break;
|
||||
}
|
||||
// This truncates messages that are too long
|
||||
QString qMsg = QString::fromUtf8(msg.c_str());
|
||||
if(messageSize > 0 && qMsg.size()>messageSize) {
|
||||
qMsg.truncate(messageSize);
|
||||
qMsg += QString::fromLatin1("...\n");
|
||||
}
|
||||
// Send the event to itself to allow thread-safety. Qt will delete it when done.
|
||||
CustomReportEvent* ev = new CustomReportEvent(ReportHighlighter::LogText, msg);
|
||||
CustomReportEvent* ev = new CustomReportEvent(style, qMsg);
|
||||
QApplication::postEvent(this, ev);
|
||||
}
|
||||
|
||||
|
||||
@@ -120,11 +120,11 @@ private:
|
||||
};
|
||||
|
||||
/** Output window to show messages.
|
||||
* @see Base::ConsoleObserver
|
||||
* @see Base::ILogger
|
||||
* @see QTextEdit
|
||||
* \author Werner Mayer
|
||||
*/
|
||||
class GuiExport ReportOutput : public QTextEdit, public WindowParameter, public Base::ConsoleObserver
|
||||
class GuiExport ReportOutput : public QTextEdit, public WindowParameter, public Base::ILogger
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
@@ -135,14 +135,7 @@ public:
|
||||
/** Observes its parameter group. */
|
||||
void OnChange(Base::Subject<const char*> &rCaller, const char * sReason);
|
||||
|
||||
/** Writes warnings */
|
||||
void Warning(const char * s);
|
||||
/** Writes normal text */
|
||||
void Message(const char * s);
|
||||
/** Writes errors */
|
||||
void Error (const char * s);
|
||||
/** Does not do anything */
|
||||
void Log (const char * s);
|
||||
void SendLog(const std::string& msg, Base::LogStyle level) override;
|
||||
|
||||
/// returns the name for observer handling
|
||||
const char* Name(void){return "ReportOutput";}
|
||||
|
||||
@@ -63,7 +63,7 @@ namespace Gui {
|
||||
/** Displays all messages at startup inside the splash screen.
|
||||
* \author Werner Mayer
|
||||
*/
|
||||
class SplashObserver : public Base::ConsoleObserver
|
||||
class SplashObserver : public Base::ILogger
|
||||
{
|
||||
public:
|
||||
SplashObserver(QSplashScreen* splasher=0)
|
||||
@@ -110,28 +110,14 @@ public:
|
||||
{
|
||||
return "SplashObserver";
|
||||
}
|
||||
void Warning(const char * s)
|
||||
void SendLog(const std::string& msg, Base::LogStyle level) override
|
||||
{
|
||||
(void) level; // to eliminate unused parameter warning
|
||||
|
||||
#ifdef FC_DEBUG
|
||||
Log(s);
|
||||
Log(msg.c_str());
|
||||
#else
|
||||
Q_UNUSED(s);
|
||||
#endif
|
||||
}
|
||||
void Message(const char * s)
|
||||
{
|
||||
#ifdef FC_DEBUG
|
||||
Log(s);
|
||||
#else
|
||||
Q_UNUSED(s);
|
||||
#endif
|
||||
}
|
||||
void Error (const char * s)
|
||||
{
|
||||
#ifdef FC_DEBUG
|
||||
Log(s);
|
||||
#else
|
||||
Q_UNUSED(s);
|
||||
Q_UNUSED(msg.c_str());
|
||||
#endif
|
||||
}
|
||||
void Log (const char * s)
|
||||
|
||||
Reference in New Issue
Block a user