X-Git-Url: https://vaikene.ee/gitweb/pswgen09.html?a=blobdiff_plain;ds=sidebyside;f=src%2Flibs%2FCommon%2Flogger.cpp;h=bb8807395aa7c735c07972a9a94110d1fae209df;hb=c8050984a289a6ea98812395313ef37d4c7fd059;hp=01d56876f9caa0e4bbedc53d33be751a7b0a6956;hpb=8d713d5c7982d5ed6daa131bcc976147c32268d5;p=evaf
diff --git a/src/libs/Common/logger.cpp b/src/libs/Common/logger.cpp
index 01d5687..bb88073 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
@@ -388,7 +425,7 @@ QString Logger::printf(char const * const fmt, ...) const
va_end(ap);
#else
::va_start(ap, fmt);
- if (::vasprintf(&str, fmt, ap)); // IF is needed to avoid the compiler warning
+ if (::vasprintf(&str, fmt, ap)) {}; // IF is needed to avoid the compiler warning
::va_end(ap);
#endif
@@ -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();
}