ui: logview logic in controller
* logfilter in applicationsettings * logview limit 500 entries
This commit is contained in:
@@ -57,7 +57,7 @@ void ComponentManager::setup(ViewLayout* viewLayout)
|
||||
m_dialogView = m_componentFactory->getViewFactory()->createDialogView(viewLayout, m_componentFactory->getStorageAccess());
|
||||
|
||||
std::shared_ptr<TabbedView> tabbedView =
|
||||
m_componentFactory->getViewFactory()->createTabbedView(viewLayout, "Log");
|
||||
m_componentFactory->getViewFactory()->createTabbedView(viewLayout, "Log/Error");
|
||||
m_tabbedViews.push_back(tabbedView);
|
||||
|
||||
std::shared_ptr<Component> errorComponent = m_componentFactory->createErrorComponent(tabbedView.get());
|
||||
|
||||
@@ -2,9 +2,12 @@
|
||||
|
||||
#include "data/access/StorageAccess.h"
|
||||
#include "utility/logging/LogManager.h"
|
||||
#include "settings/ApplicationSettings.h"
|
||||
|
||||
LogController::LogController()
|
||||
: Logger("WindowLogger")
|
||||
, m_enabled(false)
|
||||
, m_previousLogCount(0)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -12,6 +15,16 @@ LogController::~LogController()
|
||||
{
|
||||
}
|
||||
|
||||
void LogController::setEnabled(bool enabled)
|
||||
{
|
||||
m_enabled = enabled;
|
||||
}
|
||||
|
||||
bool LogController::getEnabled() const
|
||||
{
|
||||
return m_enabled;
|
||||
}
|
||||
|
||||
LogView* LogController::getView() const
|
||||
{
|
||||
return Controller::getView<LogView>();
|
||||
@@ -24,21 +37,87 @@ void LogController::clear()
|
||||
|
||||
void LogController::logInfo(const LogMessage& message )
|
||||
{
|
||||
addLog(LOG_INFOS, message);
|
||||
if (!m_enabled)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (m_logLevel & Logger::LOG_INFOS)
|
||||
{
|
||||
addLog(LOG_INFOS, message);
|
||||
}
|
||||
}
|
||||
|
||||
void LogController::logError(const LogMessage& message )
|
||||
{
|
||||
addLog(LOG_ERRORS, message);
|
||||
if (!m_enabled)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (m_logLevel & Logger::LOG_ERRORS)
|
||||
{
|
||||
addLog(LOG_ERRORS, message);
|
||||
}
|
||||
}
|
||||
|
||||
void LogController::logWarning(const LogMessage& message )
|
||||
{
|
||||
addLog(LOG_WARNINGS, message);
|
||||
if (!m_enabled)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (m_logLevel & Logger::LOG_WARNINGS)
|
||||
{
|
||||
addLog(LOG_WARNINGS, message);
|
||||
}
|
||||
}
|
||||
|
||||
void LogController::handleMessage(MessageLogFilterChanged* message)
|
||||
{
|
||||
m_logLevel = message->logFilter;
|
||||
ApplicationSettings* settings = ApplicationSettings::getInstance().get();
|
||||
settings->setLogFilter(m_logLevel);
|
||||
settings->save();
|
||||
syncLogs();
|
||||
}
|
||||
|
||||
void LogController::addLog(Logger::LogLevel type, const LogMessage& message)
|
||||
{
|
||||
getView()->addLog(type, message);
|
||||
|
||||
m_logs.push_back(
|
||||
Log(
|
||||
type,
|
||||
(message.getFileName().empty() ? "" : message.getFileName() + ": ") + message.message,
|
||||
message.getTimeString("%H:%M:%S")
|
||||
)
|
||||
);
|
||||
|
||||
if (m_waiting.try_lock())
|
||||
{
|
||||
std::thread([&]()
|
||||
{
|
||||
std::this_thread::sleep_for( std::chrono::seconds(1) );
|
||||
syncLogs();
|
||||
m_waiting.unlock();
|
||||
}).detach();
|
||||
}
|
||||
}
|
||||
|
||||
void LogController::syncLogs()
|
||||
{
|
||||
int logCount = m_logs.size();
|
||||
if (logCount > getView()->LogLimit)
|
||||
{
|
||||
m_logs.erase(m_logs.begin(),m_logs.begin() + logCount - LogView::LogLimit);
|
||||
}
|
||||
std::vector<Log> logs;
|
||||
for( Log log : m_logs )
|
||||
{
|
||||
if (log.type & m_logLevel)
|
||||
{
|
||||
logs.push_back(log);
|
||||
}
|
||||
}
|
||||
|
||||
getView()->addLogs(logs);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,23 +1,32 @@
|
||||
#ifndef LOG_CONTROLLER_H
|
||||
#define LOG_CONTROLLER_H
|
||||
|
||||
#include <mutex>
|
||||
|
||||
#include "component/controller/Controller.h"
|
||||
#include "component/view/LogView.h"
|
||||
|
||||
#include "utility/logging/Logger.h"
|
||||
#include "utility/logging/LogMessage.h"
|
||||
#include "utility/messaging/MessageListener.h"
|
||||
#include "utility/messaging/type/MessageLogFilterChanged.h"
|
||||
|
||||
class StorageAccess;
|
||||
|
||||
class LogController
|
||||
: public Controller
|
||||
, public Logger
|
||||
, public MessageListener<MessageLogFilterChanged>
|
||||
{
|
||||
public:
|
||||
LogController();
|
||||
~LogController();
|
||||
|
||||
void setEnabled(bool enabled);
|
||||
bool getEnabled() const;
|
||||
|
||||
private:
|
||||
bool m_enabled;
|
||||
LogView* getView() const;
|
||||
|
||||
virtual void clear();
|
||||
@@ -26,7 +35,15 @@ private:
|
||||
virtual void logWarning(const LogMessage& message);
|
||||
virtual void logError(const LogMessage& message);
|
||||
|
||||
virtual void handleMessage(MessageLogFilterChanged* message);
|
||||
|
||||
std::vector<Log> m_logs;
|
||||
|
||||
void addLog(Logger::LogLevel type, const LogMessage& message);
|
||||
void syncLogs();
|
||||
std::mutex m_waiting;
|
||||
int m_previousLogCount;
|
||||
Logger::LogLevelMask m_logLevel;
|
||||
};
|
||||
|
||||
#endif // LOG_CONTROLLER_H
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
#include "component/view/LogView.h"
|
||||
|
||||
const int LogView::LogLimit = 500;
|
||||
|
||||
LogView::LogView(ViewLayout* viewLayout)
|
||||
: View(viewLayout)
|
||||
{
|
||||
@@ -13,3 +15,8 @@ std::string LogView::getName() const
|
||||
{
|
||||
return "Logs";
|
||||
}
|
||||
|
||||
bool LogView::hasLogLevel(const Logger::LogLevel type, const Logger::LogLevelMask mask) const
|
||||
{
|
||||
return mask & type;
|
||||
}
|
||||
|
||||
@@ -6,6 +6,17 @@
|
||||
#include "utility/logging/LogMessage.h"
|
||||
#include "utility/logging/Logger.h"
|
||||
|
||||
struct Log
|
||||
{
|
||||
Log(Logger::LogLevel type, std::string message, std::string timestamp)
|
||||
: type(type)
|
||||
, message(message)
|
||||
, timestamp(timestamp){}
|
||||
Logger::LogLevel type;
|
||||
std::string message;
|
||||
std::string timestamp;
|
||||
};
|
||||
|
||||
class LogView
|
||||
: public View
|
||||
{
|
||||
@@ -13,10 +24,14 @@ public:
|
||||
LogView(ViewLayout* viewLayout);
|
||||
virtual ~LogView();
|
||||
|
||||
|
||||
virtual std::string getName() const;
|
||||
virtual bool hasLogLevel(const Logger::LogLevel type, const Logger::LogLevelMask mask) const;
|
||||
|
||||
virtual void clear() = 0;
|
||||
virtual void addLog(Logger::LogLevel type, const LogMessage& message) = 0;
|
||||
virtual void addLogs(const std::vector<Log>& logs) = 0;
|
||||
static const int LogLimit;
|
||||
};
|
||||
|
||||
#endif // LOG_VIEW_H
|
||||
|
||||
Reference in New Issue
Block a user