Skip to content

Commit

Permalink
• Improved handling of logging functionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
Fredo Erxleben committed May 21, 2015
1 parent 2bb60b8 commit e2d7d8e
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 25 deletions.
15 changes: 7 additions & 8 deletions Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

using namespace q2d;
using namespace q2d::constants;
using namespace q2d::logging;
using namespace std;

Application::Application(int &argc, char** argv[])
: QApplication(argc, *argv), m_context(new ApplicationContext(this)), m_quantorInterface() {
Expand All @@ -31,7 +33,7 @@ Application::Application(int &argc, char** argv[])
this->m_appSettings = new QSettings();
this->checkSettings();

m_logManager->logger("General")->log("Settings loaded", m_logManager->logLevel("INFO"));
m_logger->log("Settings loaded", LogLevel::INFO);

// Application -> Quantor
connect(this, &Application::signal_quantorTriggered,
Expand Down Expand Up @@ -72,18 +74,15 @@ Application::defaultSetting(QString name, QVariant defaultValue) {

void
Application::setupLogging() {
m_logManager = std::shared_ptr<logging::LogManager>(new logging::LogManager(this));
m_logManager = shared_ptr<LogManager>(new LogManager(this));
// TODO put the default log levels in an enum
auto levelDebug = m_logManager->logLevel("DEBUG");
m_logManager->logLevel("INFO");
m_logManager->logLevel("ERROR");
auto generalLogger = m_logManager->logger("General");
m_logger = m_logManager->logger("Application");

m_consoleLogger = std::shared_ptr<logging::ConsoleLogger>(new logging::ConsoleLogger(this));
m_consoleLogger->connect(generalLogger);
m_consoleLogger->connect(m_logger);

// print an initial message to show it works
generalLogger->log("General logger says hello.", levelDebug);
m_logger->log("Logging initialized", LogLevel::DEBUG);
}

void
Expand Down
8 changes: 7 additions & 1 deletion Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@

namespace q2d {

class Application : public QApplication {
class Application
: public QApplication {
Q_OBJECT
private:
QSettings* m_appSettings;
Expand All @@ -28,6 +29,11 @@ class Application : public QApplication {
void checkSettings();
void setupLogging();

/**
* @brief m_logger the logger used by everything related to the application itself.
*/
std::shared_ptr<logging::Logger> m_logger;

public:

explicit Application(int &argc, char** argv[]);
Expand Down
15 changes: 12 additions & 3 deletions logging/LogLevel.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
#include "LogManager.h"
#include "LogLevel.h"


using namespace q2d::logging;
using namespace std;

QString LogLevel::DEBUG = "Debug";
QString LogLevel::ERROR = "Error";
QString LogLevel::INFO = "Info";
QString LogLevel::WARNING = "Warning";

LogLevel::LogLevel(QString name, QColor fontColor, QColor backgroundColor){
LogLevel::LogLevel(QString name, shared_ptr<LogManager> manager){
m_name = name;
m_fontColor = fontColor;
m_backgroundColor = backgroundColor;
m_manager = manager;
m_fontColor = Qt::black;
m_backgroundColor = Qt::white;
}

QString
Expand Down
29 changes: 25 additions & 4 deletions logging/LogLevel.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,57 @@
#include <QColor>
#include <QString>

#include <memory>

namespace q2d {
namespace logging {

class LogManager;

class LogLevel {
friend class LogManager;
private:
QString m_name;
QColor m_fontColor;
QColor m_backgroundColor;
std::shared_ptr<LogManager> m_manager;

public:

// TODO store name in QObjects name property
/**
* @brief LogLevel instantiates a new log level.
* Only the LogManager might instantiate a new LogLevel.
*
* @param name is the name under which the log level is known.
* The name must be unique amongst the used log levels.
* The name can not be changed once set.
* @param fontColor is the color in which output text for this log level will be rendered
*
* The fontColor is the color in which output text for this log level will be rendered
* (if supported by the output interface).
* The default font color is black.
* @param backgroundColor is the color in which background for this log level will be rendered
* The backgroundColor is the color in which background for this log level will be rendered
* (if supported by the output interface).
* The default background color is white.
*/
LogLevel(QString name, QColor fontColor = Qt::black, QColor backgroundColor = Qt::white);
LogLevel(QString name, std::shared_ptr<LogManager> manager);
public:


QString name() const;
QColor fontColor() const;
QColor backgroundColor() const;

void setFontColor(QColor color);
void setBackgroundColor(QColor color);


// constants for naming log levels
// I found no feasible alternative of enumerating these yet…

static QString DEBUG;
static QString ERROR;
static QString INFO;
static QString WARNING;
};


Expand Down
7 changes: 4 additions & 3 deletions logging/LogManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ using namespace q2d::logging;
using namespace std;

LogManager::LogManager(QObject* parent)
: QObject(parent){}
: QObject(parent),
enable_shared_from_this<LogManager>(){}

shared_ptr<Logger>
LogManager::logger(QString name) {
Expand All @@ -13,7 +14,7 @@ LogManager::logger(QString name) {
return m_loggers.value(name);
}

shared_ptr<Logger> newLogger( new Logger(name) );
shared_ptr<Logger> newLogger( new Logger(name, shared_from_this()) );
m_loggers.insert(name, newLogger);
return newLogger;
}
Expand All @@ -30,7 +31,7 @@ LogManager::logLevel(QString name) {
return m_logLevels.value(name);
}

shared_ptr<LogLevel> newLogLevel( new LogLevel(name) );
shared_ptr<LogLevel> newLogLevel( new LogLevel(name, shared_from_this()) );
m_logLevels.insert(name, newLogLevel);
return newLogLevel;

Expand Down
3 changes: 2 additions & 1 deletion logging/LogManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ namespace q2d {
namespace logging {

class LogManager :
public QObject {
public QObject,
public std::enable_shared_from_this<LogManager> {
Q_OBJECT

private:
Expand Down
17 changes: 15 additions & 2 deletions logging/Logger.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
#include "LogEntry.h"
#include "Logger.h"
#include "LogManager.h"
#include "LogLevel.h"

using namespace q2d::logging;
using namespace std;

Logger::Logger(QString name, QObject* parent)
: QObject(parent), enable_shared_from_this<Logger>() {
Logger::Logger(QString name, std::shared_ptr<LogManager> manager, QObject* parent)
: QObject(parent),
enable_shared_from_this<Logger>() {
m_manager = manager;
m_entries = QList<std::shared_ptr<LogEntry>>();
this->setObjectName(name);
}
Expand All @@ -25,6 +28,16 @@ Logger::log(QStringList messages, shared_ptr<LogLevel> severity) {
}
}

void
Logger::log(QString message, QString severity){
this->log(message, m_manager->logLevel(severity));
}

void
Logger::log(QStringList messages, QString severity){
this->log(messages, m_manager->logLevel(severity));
}

QList<std::shared_ptr<LogEntry>>
Logger::entries() const {
return m_entries;
Expand Down
13 changes: 10 additions & 3 deletions logging/Logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,33 @@ namespace q2d {
namespace logging {

class LogEntry;
class LogManager;
class LogLevel;

class Logger
: public QObject,
public std::enable_shared_from_this<Logger> {
Q_OBJECT

friend class LogManager;
private:

std::shared_ptr<LogManager> m_manager;
QList<std::shared_ptr<LogEntry>> m_entries;

public:
/**
* @brief Logger
* Loggers may only be created by the LoggerManager.
* @param name will be stored in the objectName attribute of the QObject
* @param parent
*/
Logger(QString name, QObject* parent = nullptr);
Logger(QString name, std::shared_ptr<LogManager> manager, QObject* parent = nullptr);

public:

void log(QString message, std::shared_ptr<LogLevel> severity);
void log(QStringList messages, std::shared_ptr<LogLevel> severity);
void log(QString message, QString severity);
void log(QStringList messages, QString severity);
QList<std::shared_ptr<LogEntry>> entries() const;

signals:
Expand Down

0 comments on commit e2d7d8e

Please sign in to comment.