Skip to content

Commit

Permalink
now qlogger can handle qt4 version too and also some small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
sfadiga committed Oct 19, 2013
1 parent 6aea226 commit 9ae1bb5
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 70 deletions.
6 changes: 3 additions & 3 deletions README
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
QLogger - A tiny Qt logging framework.
QLogger - A tiny Qt log framework.

The purpose of this logging framework is to be easy to configure and use on
The purpose of this log framework is to be easy to configure and use on
a Qt project.
QLogger rely only on Qt framework, so it does not have any other
external dependencies.
Expand All @@ -20,7 +20,7 @@ The code is licensed under MIT License.
using namespace qlogger;
int main(int argc, char *argv[])
{
QLOG_FATAL("this is quick logged using the default root logger");
QLOG_FATAL("this is quick log using the default root logger");
//or for the ones who does not like macros
QLogger::fatal("this is quick logged using the default root logger");
...
Expand Down
81 changes: 43 additions & 38 deletions configfilehandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,23 @@ namespace qlogger
//! owner.fileName = { file name mask, must contain all %1 %2 %3 params, example: log_%1_%2_%3.txt
//! owner.fileNameTimeStamp = { the timestamp that will be written in param %3 of the file name mask, must follow QTimeDate string format.

static const QString LEVEL = "level";
static const QString CH_LEVEL = "level";

static const QString OUTPUT_TYPE = "outputType";
static const QString CH_OUTPUT_TYPE = "outputType";

static const QString LOG_MASK = "logMask";
static const QString CH_LOG_MASK = "logMask";

static const QString MAX_FILE_SIZE = "maxFileSize";
static const QString CH_MAX_FILE_SIZE = "maxFileSize";

static const QString PATH = "path";
static const QString CH_PATH = "path";

static const QString TIMESTAMP_FORMAT = "timestampFormat";
static const QString CH_TIMESTAMP_FORMAT = "timestampFormat";

static const QString FILE_NAME = "fileName";
static const QString CH_FILE_NAME = "fileName";

static const QString FILE_NAME_TIMESTAMP = "fileNameTimeStamp";
static const QString CH_FILE_NAME_TIMESTAMP = "fileNameTimeStamp";

static const QString CONFIG_FILE_NAME = "qlogger.cfg";
static const QString CH_CONFIG_FILE_NAME = "qlogger.cfg";

class ConfigFileHandler
{
Expand All @@ -74,25 +74,25 @@ class ConfigFileHandler

inline static void recursiveFolderSearch(QDir& dir, bool &found)
{
QDir::setCurrent(dir.absolutePath());
QDir path(dir.absolutePath());
QStringList list = dir.entryList(QDir::Dirs | QDir::NoDotAndDotDot);
const int size = list.size();
for(int i = 0; i < size ; ++i)
{
QString path = list.at(i);
QString currentPath = list.at(i);
if(found)
{
return;
}
else if(QFile::exists(CONFIG_FILE_NAME))
else if(QFile::exists(CH_CONFIG_FILE_NAME))
{
found = true;
return;
}
else
{
found = false;
dir.cd(path);
dir.cd(currentPath);
recursiveFolderSearch(dir, found);
if(!found)
dir.cdUp();
Expand All @@ -104,32 +104,33 @@ class ConfigFileHandler

inline static bool searchForConfigFile()
{
QDir dir(QCoreApplication::applicationDirPath());
QDir dir(QDir::currentPath());
//search on the folder tree
bool found = false;
recursiveFolderSearch(dir, found);
return found;
}

inline static bool createConfiguration(QMultiHash<QString, QString> &ownerConfigLines,
QMultiHash<QString, Configuration*> &loggers)
QList<Configuration*> &configs)//QMultiHash<QString, Configuration*> &loggers)
{
QList<QString> configs = ownerConfigLines.keys();
QList<QString> configTextList = ownerConfigLines.keys();
QString currentConfig = QString();
bool localTest = false; // the loggers reference could already been filled from somewhere else
const int size = configs.size();
const int size = configTextList.size();
for(int i = 0 ; i < size ; i++)
{
QString config = configs.at(i);
if(currentConfig != config)
QString configText = configTextList.at(i);
if(currentConfig != configText)
{
QStringList values = ownerConfigLines.values(config);
loggers.insert(config, parseAllLinesOfConfig(config, values));
currentConfig = config;
QStringList values = ownerConfigLines.values(configText);
//loggers.insert(config, parseAllLinesOfConfig(config, values));
configs.append(parseAllLinesOfConfig(configText, values));
currentConfig = configText;
localTest = true;
}
}
return localTest;
return localTest;
}

inline static Configuration* parseAllLinesOfConfig(QString owner, QStringList lines)
Expand All @@ -143,16 +144,18 @@ class ConfigFileHandler
QString timestampFormat = QString();
QString fileName = QString();
QString fileNameTimestamp = QString();
foreach(QString line , lines)
int size = lines.size();
for (int i = 0 ; i < size ; i++)//(QString line , lines)
{
QString line = lines.at(i);
QStringList configList = line.trimmed().split("=");
QString key = configList.isEmpty() ? QString() : configList.at(0); // left hand
QString paramValue = configList.size() < 1 ? QString() : configList.at(1); //right hand
if(key == LEVEL)
if(key == CH_LEVEL)
{
level = Configuration::levelFromString(paramValue);
}
else if(key == OUTPUT_TYPE)
else if(key == CH_OUTPUT_TYPE)
{
if(paramValue.toUpper().contains("TEXT"))
{
Expand All @@ -167,45 +170,46 @@ class ConfigFileHandler
outputType = Configuration::CONSOLE;
}
}
else if(key == LOG_MASK)
else if(key == CH_LOG_MASK)
{
logMask = paramValue;
}
else if(key == MAX_FILE_SIZE)
else if(key == CH_MAX_FILE_SIZE)
{
bool ok;
maxFileSize = paramValue.toInt(&ok);
if(!ok)
maxFileSize = 1000;
}
else if(key == PATH)
{
path = paramValue;
}
else if(key == TIMESTAMP_FORMAT)
else if(key == CH_TIMESTAMP_FORMAT)
{
timestampFormat = paramValue;
}
else if(key == FILE_NAME)
else if(key == CH_FILE_NAME)
{
fileName = paramValue;
}
else if(key == FILE_NAME_TIMESTAMP)
else if(key == CH_FILE_NAME_TIMESTAMP)
{
fileNameTimestamp = paramValue;
}
else if(key == CH_PATH)
{
path = paramValue;
}
}

return new Configuration(owner, level, outputType, timestampFormat, logMask, fileName, fileNameTimestamp, path, maxFileSize);
}

public:

//! only public method, this will receive a hash by reference and fill it with found configurations
//! false will be returned if no config is filled in
inline static bool parseConfigurationFile(QMultiHash<QString, Configuration*> &loggers)
inline static bool parseConfigurationFile(QList<Configuration*> &configs)//(QMultiHash<QString, Configuration*> &loggers)
{
QMultiHash<QString, QString> ownerConfigLines;
QFile* file = new QFile(CONFIG_FILE_NAME);
QFile* file = new QFile(CH_CONFIG_FILE_NAME);
if (ConfigFileHandler::searchForConfigFile() &&
file->open(QIODevice::ReadOnly | QIODevice::Text))
{
Expand All @@ -224,7 +228,8 @@ class ConfigFileHandler
}
}
} while (!configLine.isNull());
return createConfiguration(ownerConfigLines, loggers);
file->close();
return createConfiguration(ownerConfigLines, configs);
}
else
{
Expand Down
3 changes: 1 addition & 2 deletions configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
#define CONFIGURATION_H

#include <QString>
#include <QCoreApplication>
#include <QWeakPointer>
#include "textoutput.h"
#include "xmloutput.h"
Expand Down Expand Up @@ -64,7 +63,7 @@ class Configuration
QString logTextMask = DEFAULT_TEXT_MASK,
QString fileNameMask = QString(),
QString fileNameTimestampFormat = FILE_NAME_TIMESTAMP_FORMAT,
QString filePath = QCoreApplication::applicationDirPath(),
QString filePath = QDir::currentPath(),
int fileMaxSizeInKb = 1000);

//! destructor, check for outputLog and closes it
Expand Down
27 changes: 17 additions & 10 deletions qlogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ QLoggerDestroyer::~QLoggerDestroyer()
{
QLogger::mutex.lock();
// call the close file methods for all loggers
singleton.loadAcquire()->close();
delete singleton.loadAcquire();
QLoggerDestroyer::getPointerInstance()->close();
delete QLoggerDestroyer::getPointerInstance();
QLogger::mutex.unlock();
}

Expand Down Expand Up @@ -73,30 +73,37 @@ void QLogger::close()

QLogger* QLogger::getInstance()
{
if(!instance.load())
if(!QLogger::getPointerInstance())
{
mutex.lock();
if (instance.testAndSetOrdered(0, new QLogger()))
{
destroyer.SetSingleton(instance);
//add the default "root" logger
QLogger::instance.load()->loggers.insert("root", new Configuration("root", Configuration::q1ERROR));
// configuration file read, then start with the default logger
ConfigFileHandler::parseConfigurationFile(QLogger::instance.load()->loggers);
QList<Configuration*> lst;
//add the default "root" logger
lst.append(new Configuration("root", Configuration::q1ERROR));
// configuration file read, then start with the default logger
ConfigFileHandler::parseConfigurationFile(lst);//QLogger::getPointerInstance()->loggers);
int size = lst.size();
for(int i = 0 ; i < size ; i++)
{
Configuration* config = lst.at(i);
QLogger::getPointerInstance()->loggers.insert(config->getLogOwner(), config);
}
}
mutex.unlock();
}
return instance.loadAcquire();
return QLogger::getPointerInstance();
}

void QLogger::addLogger(QString logOwner, Configuration* configuration)
{
QLogger::getInstance();
mutex.lock();
if(!QLogger::instance.load()->loggers.values(logOwner).contains(configuration))
if(!QLogger::getPointerInstance()->loggers.values(logOwner).contains(configuration))
{
configuration->setLogOwner(logOwner);
QLogger::instance.load()->loggers.insert(logOwner, configuration);
QLogger::getPointerInstance()->loggers.insert(logOwner, configuration);
}
mutex.unlock();
}
Expand Down
31 changes: 25 additions & 6 deletions qlogger.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ class QLoggerDestroyer
private:
QAtomicPointer<QLogger> singleton;

public:
//! Handles the acess to QAtomicPointer methods based on Qt version
inline QLogger* getPointerInstance()
{
#if (QT_VERSION < QT_VERSION_CHECK(5,0,0))
return singleton;
#else
return singleton.loadAcquire();
#endif
}

};

//! Main QLogger class, its a singleton responsible for register the log messages to
Expand Down Expand Up @@ -82,15 +93,23 @@ class QLogger
//! this static instance is responsible for call the singleton destructor when program ends
static QLoggerDestroyer destroyer;

//! This constructor is the one used for initial set of the level,
//! one call only after the program starts there is no way to change the log level
//QLogger(Configuration);
//! this methods adds a loger based on a configuration
static void addLogger(QString logOwner, Configuration* configuration);

//! Stop the compiler generating methods of copy the object
private:
//! Stop the compiler generating methods of copy the object
QLogger(QLogger const& copy); // Not Implemented
QLogger& operator=(QLogger const& copy); // Not Implemented

static void addLogger(QString logOwner, Configuration* configuration);
//! Handles the acess to QAtomicPointer methods based on Qt version
static inline QLogger* getPointerInstance()
{
#if (QT_VERSION < QT_VERSION_CHECK(5,0,0))
return instance;
#else
return instance.loadAcquire();
#endif
}


private:
Expand Down Expand Up @@ -150,7 +169,7 @@ class QLogger

};


// MACROS FOR THE PEOPLE!
#define QLOG_FATAL(message, ...) QLogger::log(Configuration::q0FATAL, message, __FUNCTION__ , __LINE__, ##__VA_ARGS__);
#define QLOG_ERROR(message, ...) QLogger::log(Configuration::q1ERROR, message, __FUNCTION__ , __LINE__ , ##__VA_ARGS__);
#define QLOG_WARN(message, ...) QLogger::log(Configuration::q2WARN, message, __FUNCTION__ , __LINE__ , ##__VA_ARGS__);
Expand Down
Loading

0 comments on commit 9ae1bb5

Please sign in to comment.