* @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.
*
#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 <QtCore>
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();
+ }
}
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
Logger::Logger()
: iLogger()
+ , mReady(false)
, mFatalMsgHandler(defFatalMsgHandler)
, mConsoleSeverity(iLogger::Fatal)
- , mDefaultSource("evaf")
, mThread(0)
, mWorker(0)
{
qRegisterMetaType<LoggerSource>("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) {
// 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;
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)
void Logger::setMaxSize(uint maxSize, QString const & source)
{
- getSource(source)->maxSize = maxSize;
+ getSource(source)->maxSize = maxSize * 1024;
}
uint Logger::maxCount(QString const & source)
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
#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);
LoggerSource * Logger::getSource(QString const & source)
{
+ if (source.isEmpty() || source == mDefaultSource->name)
+ return mDefaultSource.data();
+
QHash<QString, QExplicitlySharedDataPointer<LoggerSource> >::const_iterator it = mSources.constFind(source);
if (it != mSources.constEnd())
return it->data();
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();
}