Skip to content

Commit

Permalink
Gradually replacing custom log functions with logger objs. Refs #6202
Browse files Browse the repository at this point in the history
  • Loading branch information
martyngigg committed Apr 15, 2013
1 parent 9c2c3ea commit 9a02df9
Show file tree
Hide file tree
Showing 13 changed files with 208 additions and 115 deletions.
39 changes: 10 additions & 29 deletions Code/Mantid/MantidPlot/src/ApplicationWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,11 +278,12 @@ void ApplicationWindow::init(bool factorySettings, const QStringList& args)
logWindow->setObjectName("logWindow"); // this is needed for QMainWindow::restoreState()
logWindow->setWindowTitle(tr("Results Log"));
addDockWidget( Qt::TopDockWidgetArea, logWindow );

using MantidQt::API::MessageDisplay;
using MantidQt::API::Message;
qRegisterMetaType<Message>("Message"); // Required to use it in signals-slots
resultsLog = new MantidQt::API::MessageDisplay(MessageDisplay::EnableLogLevelControl, logWindow);
logWindow->setWidget(resultsLog);
using MantidQt::API::Message;
qRegisterMetaType<Message>(); // Required to use it in signals-slots

// Start Mantid
// Set the Paraview path BEFORE libaries are loaded. Doing it here prevents
Expand Down Expand Up @@ -4785,7 +4786,8 @@ bool ApplicationWindow::setScriptingLanguage(const QString &lang)

if( m_bad_script_envs.contains(lang) )
{
this->writeErrorToLogWindow("Previous initialization of " + lang + " failed, cannot retry.");
using MantidQt::API::Message;
writeToLogWindow(Message("Previous initialization of " + lang + " failed, cannot retry.",Message::Priority::PRIO_ERROR));
return false;
}

Expand Down Expand Up @@ -17716,37 +17718,16 @@ void ApplicationWindow::ICatLogout()
}

/**
* Write a message to the log window
* Write a message to the log window. The message priority will be information
* or error if error=true
* @param message :: A string containing the message
* @param error :: A boolean indicating if this is an error
*/
void ApplicationWindow::writeToLogWindow(const QString& message,bool error)
{
if(error)
{
results->setTextColor(Qt::red);
}
else
{
results->setTextColor(Qt::black);
}
QTextCursor cursor = results->textCursor();
cursor.movePosition(QTextCursor::End);
results->setTextCursor(cursor);
results->insertPlainText(message + "\n");
cursor = results->textCursor();
cursor.movePosition(QTextCursor::End);
void ApplicationWindow::writeToLogWindow(const MantidQt::API::Message & msg)
{
resultsLog->append(msg);
}

/**
* Write an error message to the log window (convenience slot)
* @param message :: The string to send the log window
*/
void ApplicationWindow::writeErrorToLogWindow(const QString& message)
{
writeToLogWindow(message, true);
}

/* This method executes loadraw asynchrnously
* @param fileName - name of the file to load
* @param wsName :: -name of the workspace to store data
Expand Down
5 changes: 2 additions & 3 deletions Code/Mantid/MantidPlot/src/ApplicationWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ namespace MantidQt
{
namespace API
{
class Message;
class MessageDisplay;
}
namespace MantidWidgets
Expand Down Expand Up @@ -1030,9 +1031,7 @@ public slots:
void savetoNexusFile();

//Slot for writing to log window
void writeToLogWindow(const QString& message,bool error = false);
/// Write an error message to the log window (convenience slot)
void writeErrorToLogWindow(const QString& message);
void writeToLogWindow(const MantidQt::API::Message& message);
/// execute loadraw asynchronously
void executeLoadRawAsynch(const QString& fileName,const QString& wsName ) ;

Expand Down
11 changes: 1 addition & 10 deletions Code/Mantid/MantidPlot/src/Mantid/MantidDock.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <MantidGeometry/MDGeometry/IMDDimension.h>
#include <MantidGeometry/Crystal/OrientedLattice.h>
#include <MantidQtAPI/InterfaceManager.h>
#include <MantidQtAPI/Message.h>

#include <Poco/Path.h>
#include <boost/algorithm/string.hpp>
Expand Down Expand Up @@ -170,16 +171,6 @@ QDockWidget(tr("Workspaces"),parent), m_mantidUI(mui), m_known_groups(), m_rerun
connect(this,SIGNAL(rerunFindAbandonedWorkspaces()),this,SLOT(findAbandonedWorkspaces()),Qt::QueuedConnection);
}

/**
Generate a warning message and use MantidUI to display it.
@param message : message contents to display
*/
void MantidTreeWidget::logWarningMessage(const std::string& message)
{
Poco::Message msg("MantidPlot",message,Poco::Message::PRIO_WARNING);
m_mantidUI->logMessage(msg);
}

/** Returns the name of the selected workspace
* (the first one if more than one is selected)
*/
Expand Down
79 changes: 25 additions & 54 deletions Code/Mantid/MantidPlot/src/Mantid/MantidUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,12 @@ namespace
{
/// The number of detectors to show within a group before eliding
size_t DET_TABLE_NDETS_GROUP = 10;

}

// Initialize logger
Mantid::Kernel::Logger & MantidUI::g_log = Mantid::Kernel::Logger::get("MantidUI");

MantidUI::MantidUI(ApplicationWindow *aw):
m_finishedLoadDAEObserver(*this, &MantidUI::handleLoadDAEFinishedNotification),
m_addObserver(*this,&MantidUI::handleAddWorkspace),
Expand All @@ -89,8 +93,7 @@ m_finishedLoadDAEObserver(*this, &MantidUI::handleLoadDAEFinishedNotification),
m_groupworkspacesObserver(*this,&MantidUI::handleGroupWorkspaces),
m_ungroupworkspaceObserver(*this,&MantidUI::handleUnGroupWorkspace),
m_workspaceGroupUpdateObserver(*this,&MantidUI::handleWorkspaceGroupUpdate),
m_appWindow(aw), m_vatesSubWindow(NULL),
g_log(Mantid::Kernel::Logger::get("MantidUI"))
m_appWindow(aw), m_vatesSubWindow(NULL)
{

// To be able to use them in queued signals they need to be registered
Expand Down Expand Up @@ -566,11 +569,11 @@ void MantidUI::showMDPlot()
}
catch (std::invalid_argument &e)
{
logMessage(Poco::Message("MantidPlot",e.what(),Poco::Message::PRIO_WARNING));
g_log.warning() << e.what() << std::endl;
}
catch (std::runtime_error &e)
{
logMessage(Poco::Message("MantidPlot",e.what(),Poco::Message::PRIO_WARNING));
g_log.warning() << e.what() << std::endl;
}
catch (...)
{
Expand Down Expand Up @@ -691,10 +694,10 @@ void MantidUI::showImageViewer()
}
else
{
m_appWindow->writeToLogWindow(
"Only event or matrix workspaces are currently supported.");
m_appWindow->writeToLogWindow(
"Please convert to one of these before using the ImageView.");
const char * msg =
"Only event or matrix workspaces are currently supported.\n"
"Please convert to one of these before using the ImageView.";
g_log.information() << msg << std::endl;
}
}
catch (std::runtime_error &e)
Expand All @@ -703,7 +706,7 @@ void MantidUI::showImageViewer()
}
catch (...)
{
m_appWindow->writeToLogWindow("Exception getting workspace " );
g_log.error() << "Image View: Exception getting workspace " << std::endl;
}

}
Expand Down Expand Up @@ -1306,7 +1309,7 @@ void MantidUI::loadfromICatInterface(const QString& fileName,const QString& wsNa

void MantidUI ::executeloadAlgorithm(const QString& algName, const QString& fileName, const QString& wsName)
{

using MantidQt::API::Message;
Mantid::API::IAlgorithm_sptr alg = this->createAlgorithm(algName, -1);
if( !alg ) return;
try
Expand All @@ -1316,14 +1319,12 @@ void MantidUI ::executeloadAlgorithm(const QString& algName, const QString& file
}
catch(std::invalid_argument& e)
{
//emit error(e.what());
m_appWindow->writeToLogWindow(QString::fromStdString(e.what()), true);
g_log.error() << e.what() << std::endl;
return ;
}
catch (Mantid::Kernel::Exception::NotFoundError& e)
{
//emit error(e.what());
m_appWindow->writeToLogWindow(QString::fromStdString(e.what()), true);
g_log.error() << e.what() << std::endl;
return ;
}

Expand Down Expand Up @@ -1736,7 +1737,7 @@ void MantidUI::executeDownloadDataFiles(const std::vector<std::string>& filenNam
}
catch(...)
{
m_appWindow->writeToLogWindow("Error when getting/downloading data file from server ", true);
g_log.error() << "Error when getting/downloading data file from server " << std::endl;
return;
}
try
Expand All @@ -1747,13 +1748,13 @@ void MantidUI::executeDownloadDataFiles(const std::vector<std::string>& filenNam
}
catch(std::invalid_argument& e)
{
m_appWindow->writeToLogWindow(QString::fromStdString(e.what()), true);
g_log.error() << e.what() << std::endl;
return;

}
catch (Mantid::Kernel::Exception::NotFoundError& e)
{
m_appWindow->writeToLogWindow(QString::fromStdString(e.what()), true);
g_log.error() << e.what() << std::endl;
return;
}

Expand All @@ -1767,7 +1768,7 @@ void MantidUI::executeDownloadDataFiles(const std::vector<std::string>& filenNam
}
catch(...)
{
m_appWindow->writeToLogWindow("Error when getting/downloading data file from server", true);
g_log.error() << "Error when getting/downloading data file from server" << std::endl;
return;
}
try
Expand All @@ -1776,7 +1777,7 @@ void MantidUI::executeDownloadDataFiles(const std::vector<std::string>& filenNam
}
catch (Mantid::Kernel::Exception::NotFoundError&e)
{
m_appWindow->writeToLogWindow(QString::fromStdString(e.what()), true);
g_log.error() << e.what() << std::endl;
return;
}

Expand Down Expand Up @@ -1872,35 +1873,6 @@ void MantidUI::handleWorkspaceGroupUpdate(Mantid::API::GroupUpdatedNotification_
emit workspace_group_updated( name );
}

void MantidUI::logMessage(const Poco::Message& msg)
{
if (!appWindow()->results) return;
QString str = msg.getText().c_str();

if (msg.getPriority() <= Poco::Message::PRIO_ERROR)
{
appWindow()->logWindow->show();
}
if (msg.getPriority() < Poco::Message::PRIO_ERROR)
appWindow()->results->setTextColor(Qt::red);
else if (msg.getPriority() <= Poco::Message::PRIO_WARNING)
appWindow()->results->setTextColor(QColor::fromRgb(255, 100, 0)); // Orange
else if (msg.getPriority() > Poco::Message::PRIO_INFORMATION)
appWindow()->results->setTextColor(Qt::gray);
else if (msg.getPriority() == Poco::Message::PRIO_NOTICE)
appWindow()->results->setTextColor(Qt::darkBlue);
else
appWindow()->results->setTextColor(Qt::black);
appWindow()->results->insertPlainText(str+"\n");

QTextCursor cur = appWindow()->results->textCursor();
cur.movePosition(QTextCursor::End);
appWindow()->results->setTextCursor(cur);
//set the colour back to the default (black)
appWindow()->results->setTextColor(Qt::black);
}


void MantidUI::manageMantidWorkspaces()
{
#ifdef _WIN32
Expand Down Expand Up @@ -3042,12 +3014,11 @@ MultiLayer* MantidUI::plotSpectraList(const QMultiMap<QString,int>& toPlot, bool
}
catch (Mantid::Kernel::Exception::NotFoundError&)
{
// Get here if workspace name is invalid
const std::string message("Workspace "+it.key().toStdString()+" not found");
logMessage(Poco::Message("MantidPlot",message,Poco::Message::PRIO_WARNING));
} catch (std::invalid_argument& ex) {
// Get here if invalid spectrum number given
logMessage(Poco::Message("MantidPlot",ex.what(),Poco::Message::PRIO_WARNING));
g_log.warning() << "Workspace " << it.key().toStdString() << " not found" << std::endl;
}
catch (std::invalid_argument& ex)
{
g_log.warning() << ex.what() << std::endl;
}
}

Expand Down
8 changes: 2 additions & 6 deletions Code/Mantid/MantidPlot/src/Mantid/MantidUI.h
Original file line number Diff line number Diff line change
Expand Up @@ -324,9 +324,6 @@ public slots:
void x_range_from_picker(double, double);
void test();

// Display a message in QtiPlot's results window. Used by MantidLog class to display Mantid log information.
void logMessage(const Poco::Message& msg);

void showSequentialPlot(Ui::SequentialFitDialog* ui, MantidQt::MantidWidgets::FitPropertyBrowser* fitbrowser);

// Import the workspace selected in the Workspace dock window
Expand Down Expand Up @@ -530,9 +527,8 @@ public slots:

QMdiSubWindow *m_vatesSubWindow; ///< Holder for the Vates interface sub-window

/// Logger object
Mantid::Kernel::Logger & g_log;

/// Logger
static Mantid::Kernel::Logger & g_log;
};


Expand Down
7 changes: 5 additions & 2 deletions Code/Mantid/MantidPlot/src/Mantid/PeakPickerTool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
#include "../FunctionCurve.h"
#include "MantidQtMantidWidgets/PropertyHandler.h"


#include "MantidAPI/CompositeFunction.h"
#include "MantidAPI/IPeakFunction.h"
#include "MantidAPI/FunctionFactory.h"
#include "MantidKernel/Exception.h"
#include "MantidKernel/Logger.h"

#include "qwt_painter.h"
#include <QPainter>
Expand All @@ -22,6 +24,8 @@

#include <iostream>

Mantid::Kernel::Logger & PeakPickerTool::g_log = Mantid::Kernel::Logger::get("PeakPickerTool");

PeakPickerTool::PeakPickerTool(Graph *graph, MantidQt::MantidWidgets::FitPropertyBrowser *fitPropertyBrowser, MantidUI *mantidUI, bool showFitPropertyBrowser, bool customInterface) :
QwtPlotPicker(graph->plotWidget()->canvas()),
PlotToolInterface(graph),
Expand Down Expand Up @@ -587,8 +591,7 @@ void PeakPickerTool::algorithmFinished(const QString& out)
}
catch(Mantid::Kernel::Exception::NotFoundError&)
{
const std::string error = "PeakPicker cannot find output workspace '" + out.toStdString() + "'";
m_mantidUI->logMessage(Poco::Message("", error, Poco::Message::PRIO_WARNING));
g_log.warning() << "PeakPicker cannot find output workspace '" + out.toStdString() + "'" << std::endl;
}
}

Expand Down
7 changes: 7 additions & 0 deletions Code/Mantid/MantidPlot/src/Mantid/PeakPickerTool.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ class PropertyHandler;

namespace Mantid
{
namespace Kernel
{
class Logger;
}
namespace API
{
class IFunction;
Expand Down Expand Up @@ -210,6 +214,9 @@ private slots:
//std::string m_defaultPeakName; // The default peak function name

QStringList m_curveNames; // Names of all curves added to graph()

/// Logger object
static Mantid::Kernel::Logger & g_log;
};


Expand Down
5 changes: 3 additions & 2 deletions Code/Mantid/MantidPlot/src/MultiLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ void LayerButton::mouseDoubleClickEvent ( QMouseEvent * )
emit showCurvesDialog();
}

Mantid::Kernel::Logger & MultiLayer::g_log = Mantid::Kernel::Logger::get("MultiLayer");

MultiLayer::MultiLayer(ApplicationWindow* parent, int layers, int rows, int cols,
const QString& label, const char* name, Qt::WFlags f)
: MdiSubWindow(parent, label, name, f),
Expand Down Expand Up @@ -1381,8 +1383,7 @@ void MultiLayer::dropOntoMDCurve(Graph *g, MantidMDCurve* originalCurve, MantidT
catch(std::invalid_argument& ex)
{
//Handle case when workspace does not have only one non-integrated dimension.
std::string message = ex.what();
tree->logWarningMessage(message);
g_log.warning() << ex.what() << std::endl;
}
}
}
Expand Down

0 comments on commit 9a02df9

Please sign in to comment.