From: Enar Väikene Date: Wed, 15 Feb 2012 12:41:47 +0000 (+0200) Subject: Changes and fixes to the iLogger interface: X-Git-Url: https://vaikene.ee/gitweb/overview.html?a=commitdiff_plain;h=18ba5c0014514a8248a77dd36d8ee8d9acc3227b;p=evaf Changes and fixes to the iLogger interface: * The default source is now created separately and exists always * Uses the ${APPNAME}.ini file to initialize the default source * Implemented reading settings from the logger.ini file. --- diff --git a/src/libs/Common/globals.cpp b/src/libs/Common/globals.cpp index 02aedd8..5054bd1 100644 --- a/src/libs/Common/globals.cpp +++ b/src/libs/Common/globals.cpp @@ -3,7 +3,7 @@ * @brief Global constants and macros for eVaf * @author Enar Vaikene * - * Copyright (c) 2011 Enar Vaikene + * Copyright (c) 2011-2012 Enar Vaikene * * This file is part of the eVaf C++ cross-platform application development framework. * @@ -47,12 +47,6 @@ bool eVaf::Common::init() if (!app->init()) return false; } - eVaf::Common::Internal::Logger * logger = - qobject_cast(eVaf::Common::iLogger::instance()); - if (logger) { - if (!logger->init()) - return false; - } eVaf::Common::Internal::Config * config = qobject_cast(eVaf::Common::iConfig::instance()); if (config) { @@ -65,6 +59,12 @@ bool eVaf::Common::init() if (!prop->init()) return false; } + eVaf::Common::Internal::Logger * logger = + qobject_cast(eVaf::Common::iLogger::instance()); + if (logger) { + if (!logger->init()) + return false; + } EVAF_INFO("%s-Globals initialized", VER_MODULE_NAME_STR); diff --git a/src/libs/Common/ilogger.h b/src/libs/Common/ilogger.h index 71dbd14..c89f921 100644 --- a/src/libs/Common/ilogger.h +++ b/src/libs/Common/ilogger.h @@ -3,7 +3,7 @@ * @brief Logger interface for eVaf * @author Enar Vaikene * - * Copyright (c) 2011 Enar Vaikene + * Copyright (c) 2011-2012 Enar Vaikene * * This file is part of the eVaf C++ cross-platform application development framework. * @@ -93,6 +93,8 @@ public: * * Use the setDefaultSource() function to change the default source name. If not set, then * uses the default source name "common". + * + * Changing the name of the default source resets any other settings set for the default source. */ virtual void setDefaultSource(QString const & source) = 0; diff --git a/src/libs/Common/logger.cpp b/src/libs/Common/logger.cpp index 01d5687..809670a 100644 --- a/src/libs/Common/logger.cpp +++ b/src/libs/Common/logger.cpp @@ -3,7 +3,7 @@ * @brief iLogger interface implementation * @author Enar Vaikene * - * Copyright (c) 2011 Enar Vaikene + * Copyright (c) 2011-2012 Enar Vaikene * * This file is part of the eVaf C++ cross-platform application development framework. * @@ -20,6 +20,9 @@ #include "logger.h" #include "iregistry.h" #include "iapp.h" +#include "iconfig.h" +#include "globals.h" +#include "inifile.h" #include "version.h" #include @@ -75,16 +78,36 @@ LoggerSource::LoggerSource() LoggerSource::LoggerSource(LoggerSource const & o) : QSharedData() + , name(o.name) , severity(o.severity) + , fileName(o.fileName) , maxSize(o.maxSize) , maxCount(o.maxCount) {} -void LoggerSource::init(QString const & source, QString const & logDir, QString const & etcDir) +void LoggerSource::init(QString const & source) { - Q_UNUSED(etcDir); - - fileName = logDir + source + ".log"; + name = source; + fileName = iApp::instance()->logDir() + source + ".log"; + + // Set default settings + severity = iLogger::Fatal; + maxSize = 100 * 1024; + maxCount = 3; + + // Read settings from the 'logger.ini' file + QString confFileName = iApp::instance()->etcDir() + "logger.ini"; + if (QFile::exists(confFileName)) { + IniFile ini(confFileName, QIODevice::ReadOnly); + + // Default values for all sources + maxSize = 1024 * ini.getValue(".default/log_size", maxSize / 1024).toInt(); + maxCount = ini.getValue(".default/log_count", maxCount).toInt(); + + // Default values for this source + maxSize = 1024 * ini.getValue(source.toLatin1() + "/log_size", maxSize / 1024).toInt(); + maxCount = ini.getValue(source.toLatin1() + "/log_count", maxCount).toInt(); + } } @@ -104,6 +127,9 @@ void renameBackupFile(QDir & dir, QString const & baseName, int idx) void LoggerWorker::writeToLogFile(LoggerSource const & src, QString const & msg) { + //::printf("writeToLogFile(\'%s\', \'%s\') fileName = \'%s\'\n", qPrintable(src.name), qPrintable(msg), qPrintable(src.fileName)); + if (src.fileName.isEmpty()) + return; QFile f(src.fileName); QFile::OpenMode mode; #ifdef Q_OS_LINUX @@ -144,9 +170,9 @@ void LoggerWorker::writeToLogFile(LoggerSource const & src, QString const & msg) Logger::Logger() : iLogger() + , mReady(false) , mFatalMsgHandler(defFatalMsgHandler) , mConsoleSeverity(iLogger::Fatal) - , mDefaultSource("evaf") , mThread(0) , mWorker(0) { @@ -154,6 +180,10 @@ Logger::Logger() qRegisterMetaType("LoggerSource"); + // Create the default source + mDefaultSource = new LoggerSource; + mDefaultSource->name = "common"; + write(Info, QString("%1 created").arg(objectName()), 0, printf("%s:%s:%d", __FILE__, __FUNCTION__, __LINE__)); } @@ -183,6 +213,20 @@ bool Logger::init() // Clear existing sources in case the application was restarted mSources.clear(); + // Set the default source name to the name of the application + setDefaultSource(iApp::instance()->name()); + + // Read configuration parameters from the application's INI file + QVariant v = iConfig::instance()->getValue(QString("%1/general/log_level").arg(iApp::instance()->name()), severity()); + if (v.isValid()) + setSeverity(iLogger::Severity(qBound(int(iLogger::None), v.toInt(), int(iLogger::Debug)))); + v = iConfig::instance()->getValue(QString("%1/general/log_size").arg(iApp::instance()->name()), maxSize()); + if (v.isValid()) + setMaxSize(v.toUInt()); + v = iConfig::instance()->getValue(QString("%1/general/log_cnt").arg(iApp::instance()->name()), maxCount()); + if (v.isValid()) + setMaxCount(v.toUInt()); + // Destroy the previous worker thread if (mWorker) { delete mWorker; @@ -200,14 +244,23 @@ bool Logger::init() mThread->start(QThread::IdlePriority); connect(this, SIGNAL(writeToLogFile(LoggerSource,QString)), mWorker, SLOT(writeToLogFile(LoggerSource,QString)), Qt::QueuedConnection); + mReady = true; + write(Info, QString("%1 initialized").arg(objectName()), 0, printf("%s:%s:%d", __FILE__, __FUNCTION__, __LINE__)); return true; } +QString Logger::defaultSource() const +{ + return mDefaultSource->name; +} + void Logger::setDefaultSource(QString const & source) { - mDefaultSource = source; + LoggerSource * src = getSource(); + if (src && src->name != source) + getSource(QString())->init(source); } iLogger::Severity Logger::severity(QString const & source) @@ -227,7 +280,7 @@ uint Logger::maxSize(QString const & source) void Logger::setMaxSize(uint maxSize, QString const & source) { - getSource(source)->maxSize = maxSize; + getSource(source)->maxSize = maxSize * 1024; } uint Logger::maxCount(QString const & source) @@ -262,46 +315,30 @@ void Logger::write(Severity severity, QString const & msg, QString const & sourc return; // Write to the log file - LoggerSource * src = getSource(source); - if (severity <= src->severity && src->severity != iLogger::None) { - QString buf; - QTextStream io(&buf); + if (mReady) { + LoggerSource * src = getSource(source); + if (severity <= src->severity && src->severity != iLogger::None) { + QString buf; + QTextStream io(&buf); - // Date/time stamp - io << QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz"); + // Date/time stamp + io << QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz"); - // Severity - io << " " << severityText[severity]; + // Severity + io << " " << severityText[severity]; - // Message - io << msg; + // Message + io << msg; - // Location in the source file - if (!where.isEmpty()) - io << " (occurred in " << where << ")"; + // Location in the source file + if (!where.isEmpty()) + io << " (occurred in " << where << ")"; - io << endl; - io.flush(); + io << endl; + io.flush(); - // If the worker is initialized, use the worker thread to do the job - if (mWorker) { emit writeToLogFile(*src, buf); } - // Otherwise we have to do it ourselves - else { - QFile f(src->fileName); - QFile::OpenMode mode; -#ifdef Q_OS_LINUX - mode = QFile::Append | QFile::Text | QFile::Unbuffered; -#else - mode = QFile::Append | QFile::Text; -#endif - - if (f.open(mode)) { - f.write(buf.toLocal8Bit()); - f.close(); - } - } } // Output to the console @@ -436,6 +473,9 @@ FatalMsgHandler Logger::installFatalMsgHandler(FatalMsgHandler newHandler) LoggerSource * Logger::getSource(QString const & source) { + if (source.isEmpty() || source == mDefaultSource->name) + return mDefaultSource.data(); + QHash >::const_iterator it = mSources.constFind(source); if (it != mSources.constEnd()) return it->data(); @@ -445,7 +485,7 @@ LoggerSource * Logger::getSource(QString const & source) mSources.insert(source, src); // Initialize the new source - src->init(source.isEmpty() ? mDefaultSource : source, iApp::instance()->logDir(), iApp::instance()->etcDir()); + src->init(source); return src.data(); } diff --git a/src/libs/Common/logger.h b/src/libs/Common/logger.h index 1dfdc62..6e49ef7 100644 --- a/src/libs/Common/logger.h +++ b/src/libs/Common/logger.h @@ -3,7 +3,7 @@ * @brief iLogger interface implementation * @author Enar Vaikene * - * Copyright (c) 2011 Enar Vaikene + * Copyright (c) 2011-2012 Enar Vaikene * * This file is part of the eVaf C++ cross-platform application development framework. * @@ -54,8 +54,6 @@ public: /** * Initializes the source * @param source Name of the source - * @param logDir Full path to the log directory - * @param etcDir Full path to the configuration files directory * * This function initializes the newly created logger source and sets initial * parameters for the source. @@ -73,25 +71,28 @@ public: * Example logger.ini file: * @code * [.default] - * severity = Fatal - * maxSize = 100 - * maxCount = 3 + * log_level = Fatal + * log_size = 100 + * log_count = 3 * * [my-source] - * severity = Warning - * maxSize = 1000 - * maxCount = 10 + * log_level = Warning + * log_size = 1000 + * log_count = 10 * @endcode */ - void init(QString const & source, QString const & logDir, QString const & etcDir); + void init(QString const & source); public: // Members (we don't bother adding getter/setter functions) + /// Name of the source + QString name; + /// Current severity level iLogger::Severity severity; - /// Current log file name + /// File name QString fileName; /// Current maximum size of log files @@ -142,7 +143,7 @@ public: virtual ~Logger(); /** - * Initializes the interface implementation + * Initializes the iLogger interface implementation * @return True if ok; false if initialization failed */ bool init(); @@ -151,7 +152,7 @@ public: iLogger interface */ - virtual QString defaultSource() const { return mDefaultSource; } + virtual QString defaultSource() const; virtual void setDefaultSource(QString const & source); @@ -187,16 +188,19 @@ signals: private: // Members + /// Flag indicating that logger is fully initialized + bool mReady; + /// Current fatal error message handler FatalMsgHandler mFatalMsgHandler; /// Console output severity level iLogger::Severity mConsoleSeverity; - /// Current default source (defaults to "evaf") - QString mDefaultSource; + /// Default logger source + QExplicitlySharedDataPointer mDefaultSource; - /// Logger sources + /// Other logger sources QHash > mSources; /// Worker thread @@ -209,7 +213,7 @@ private: // Members private: // Methods /// Returns the source by the name. The source is created if it does not exist yet. - LoggerSource * getSource(QString const & name); + LoggerSource * getSource(QString const & name = QString()); #ifdef Q_OS_WIN32 /// Changes text colors on the Windows console diff --git a/src/libs/Common/version.h b/src/libs/Common/version.h index 0a5f465..dbc0b89 100644 --- a/src/libs/Common/version.h +++ b/src/libs/Common/version.h @@ -3,7 +3,7 @@ * @brief Version information for eVaf modules * @author Enar Vaikene * - * Copyright (c) 2011 Enar Vaikene + * Copyright (c) 2011-2012 Enar Vaikene * * This file is part of the eVaf C++ cross-platform application development framework. * @@ -25,12 +25,12 @@ /** * Module/library version number in the form major,minor,release,build */ -#define VER_FILE_VERSION 0,2,3,12 +#define VER_FILE_VERSION 0,2,4,13 /** * Module/library version number in the string format (shall end with \0) */ -#define VER_FILE_VERSION_STR "0.2.3.12\0" +#define VER_FILE_VERSION_STR "0.2.4.13\0" /** * Module/library name (shall end with \0) diff --git a/src/libs/version_rc.h b/src/libs/version_rc.h index b74e6c7..ed8a20f 100644 --- a/src/libs/version_rc.h +++ b/src/libs/version_rc.h @@ -3,7 +3,7 @@ * @brief Global version information for eVaf applications and modules * @author Enar Vaikene * - * Copyright (c) 2011 Enar Vaikene + * Copyright (c) 2011-2012 Enar Vaikene * * This file is part of the eVaf C++ cross-platform application development framework. * @@ -52,7 +52,7 @@ * Legal copyright (shall end with \0) */ #ifndef VER_LEGAL_COPYRIGHT_STR -# define VER_LEGAL_COPYRIGHT_STR "(C) 2011 Enar Vaikene\0" +# define VER_LEGAL_COPYRIGHT_STR "(C) 2011-2012 Enar Vaikene\0" #endif /**