ui: logview logic in controller
* logfilter in applicationsettings * logview limit 500 entries
This commit is contained in:
@@ -73,6 +73,8 @@ void Application::loadSettings()
|
||||
std::shared_ptr<ApplicationSettings> settings = ApplicationSettings::getInstance();
|
||||
settings->load(FilePath(UserPaths::getAppSettingsPath()));
|
||||
|
||||
LogManager::getInstance()->setLoggingEnabled(settings->getLoggingEnabled());
|
||||
|
||||
loadStyle(settings->getColorSchemePath());
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -175,6 +175,16 @@ void ApplicationSettings::setVerboseIndexerLoggingEnabled(bool value)
|
||||
setValue<bool>("application/verbose_indexer_logging_enabled", value);
|
||||
}
|
||||
|
||||
void ApplicationSettings::setLogFilter(int mask)
|
||||
{
|
||||
setValue<int>("application/log_filter", mask);
|
||||
}
|
||||
|
||||
int ApplicationSettings::getLogFilter() const
|
||||
{
|
||||
return getValue<int>("application/log_filter", Logger::LOG_WARNINGS | Logger::LOG_ERRORS);
|
||||
}
|
||||
|
||||
std::vector<FilePath> ApplicationSettings::getIndexingFilePaths() const
|
||||
{
|
||||
return getPathValues("application/state/indexing_paths/indexing_path");
|
||||
|
||||
@@ -47,12 +47,16 @@ public:
|
||||
float getScrollSpeed() const;
|
||||
void setScrollSpeed(float scrollSpeed);
|
||||
|
||||
// logging
|
||||
bool getLoggingEnabled() const;
|
||||
void setLoggingEnabled(bool loggingEnabled);
|
||||
|
||||
bool getVerboseIndexerLoggingEnabled() const;
|
||||
void setVerboseIndexerLoggingEnabled(bool loggingEnabled);
|
||||
|
||||
int getLogFilter() const;
|
||||
void setLogFilter(int mask);
|
||||
|
||||
std::vector<FilePath> getIndexingFilePaths() const;
|
||||
bool setIndexingFilePaths(const std::vector<FilePath>& indexingFiles);
|
||||
|
||||
|
||||
@@ -53,6 +53,16 @@ void LogManager::removeLoggersByType(const std::string& type)
|
||||
m_logManagerImplementation.removeLoggersByType(type);
|
||||
}
|
||||
|
||||
Logger* LogManager::getLogger(std::shared_ptr<Logger> logger)
|
||||
{
|
||||
return m_logManagerImplementation.getLogger(logger);
|
||||
}
|
||||
|
||||
Logger* LogManager::getLoggerByType(const std::string& type)
|
||||
{
|
||||
return m_logManagerImplementation.getLoggerByType(type);
|
||||
}
|
||||
|
||||
void LogManager::clearLoggers()
|
||||
{
|
||||
m_logManagerImplementation.clearLoggers();
|
||||
|
||||
@@ -22,6 +22,8 @@ public:
|
||||
void removeLoggersByType(const std::string& type);
|
||||
void clearLoggers();
|
||||
int getLoggerCount() const;
|
||||
Logger* getLoggerByType(const std::string& type);
|
||||
Logger* getLogger(std::shared_ptr<Logger> logger);
|
||||
|
||||
void logInfo(
|
||||
const std::string& message,
|
||||
|
||||
@@ -49,6 +49,31 @@ void LogManagerImplementation::removeLoggersByType(const std::string& type)
|
||||
}
|
||||
}
|
||||
|
||||
Logger* LogManagerImplementation::getLogger(std::shared_ptr<Logger> logger)
|
||||
{
|
||||
std::lock_guard<std::mutex> lockGuard(m_loggerMutex);
|
||||
std::vector<std::shared_ptr<Logger>>::iterator it = std::find(m_loggers.begin(), m_loggers.end(), logger);
|
||||
if (it != m_loggers.end())
|
||||
{
|
||||
return (*it).get();
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Logger* LogManagerImplementation::getLoggerByType(const std::string& type)
|
||||
{
|
||||
std::lock_guard<std::mutex> lockGuard(m_loggerMutex);
|
||||
for (unsigned int i = 0; i < m_loggers.size(); i++)
|
||||
{
|
||||
if (m_loggers[i]->getType() == type)
|
||||
{
|
||||
|
||||
return m_loggers[i].get();
|
||||
}
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void LogManagerImplementation::clearLoggers()
|
||||
{
|
||||
m_loggers.clear();
|
||||
|
||||
@@ -26,6 +26,8 @@ public:
|
||||
void removeLoggersByType(const std::string& type);
|
||||
void clearLoggers();
|
||||
int getLoggerCount() const;
|
||||
Logger* getLogger(std::shared_ptr<Logger> logger);
|
||||
Logger* getLoggerByType(const std::string& type);
|
||||
|
||||
void logInfo(
|
||||
const std::string& message,
|
||||
@@ -54,4 +56,4 @@ private:
|
||||
mutable std::mutex m_loggerMutex;
|
||||
};
|
||||
|
||||
#endif // LOG_MANAGER_IMPLEMENTATION_H
|
||||
#endif // LOG_MANAGER_IMPLEMENTATION_H
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
#ifndef MESSAGE_LOG_FILTER_CHANGED_H
|
||||
#define MESSAGE_LOG_FILTER_CHANGED_H
|
||||
|
||||
#include "utility/messaging/Message.h"
|
||||
#include "utility/logging/Logger.h"
|
||||
|
||||
class MessageLogFilterChanged
|
||||
: public Message<MessageLogFilterChanged>
|
||||
{
|
||||
public:
|
||||
MessageLogFilterChanged(const Logger::LogLevelMask filter)
|
||||
: logFilter(filter)
|
||||
{
|
||||
}
|
||||
|
||||
static const std::string getStaticType()
|
||||
{
|
||||
return "MessageLogFilterChanged";
|
||||
}
|
||||
|
||||
const Logger::LogLevelMask logFilter;
|
||||
};
|
||||
|
||||
#endif // MESSAGE_LOG_FILTER_CHANGED_H
|
||||
Reference in New Issue
Block a user