X-Git-Url: https://vaikene.ee/gitweb/pswgen09.html?a=blobdiff_plain;f=src%2Flibs%2FCommon%2Flogger.cpp;h=358b3146b3bb6e789670adc1203d0524956de764;hb=6fb7963d523b32158fc3d4b88a933825f8535a0b;hp=98d37708cc4f4630a15c8a5c6d86a92d4a268c05;hpb=720224734cb4f6ea2708c6b5ecf93cc666aad378;p=evaf
diff --git a/src/libs/Common/logger.cpp b/src/libs/Common/logger.cpp
index 98d3770..358b314 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.
*
@@ -19,7 +19,10 @@
#include "logger.h"
#include "iregistry.h"
-#include "ienv.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,13 +180,17 @@ 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__));
}
Logger::~Logger()
{
// Disconnect any potential receivers from this object
- disconnect(this, SIGNAL(loggerEvent(eVaf::Common::iLogger::Severity,QString,QString,QString)), 0, 0);
+ disconnect(this, SIGNAL(loggerEvent(Common::iLogger::Severity,QString,QString,QString)), 0, 0);
// Destroy the worker thread
if (mWorker) {
@@ -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
@@ -382,13 +419,19 @@ QString Logger::printf(char const * const fmt, ...) const
#endif
va_list ap;
- ::va_start(ap, fmt);
#ifdef Q_OS_WIN32
+ va_start(ap, fmt);
+# ifdef Q_CC_GNU
+ vsnprintf(str, sizeof(str), fmt, ap);
+# else
_vsnprintf_s(str, sizeof(str), _TRUNCATE, fmt, ap);
+# endif
+ va_end(ap);
#else
- if (::vasprintf(&str, fmt, ap)); // IF is needed to avoid the compiler warning
-#endif
+ ::va_start(ap, fmt);
+ if (::vasprintf(&str, fmt, ap)) {}; // IF is needed to avoid the compiler warning
::va_end(ap);
+#endif
QString rval(str);
@@ -434,6 +477,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();
@@ -443,7 +489,7 @@ LoggerSource * Logger::getSource(QString const & source)
mSources.insert(source, src);
// Initialize the new source
- src->init(source.isEmpty() ? mDefaultSource : source, iEnv::instance()->logDir(), iEnv::instance()->etcDir());
+ src->init(source);
return src.data();
}