cleaup log

This commit is contained in:
Patrick von Reth 2015-04-18 13:57:55 +02:00
parent 08e58fd325
commit a60047587b
1 changed files with 26 additions and 47 deletions

View File

@ -23,35 +23,18 @@
#include <QApplication> #include <QApplication>
#include <QTextStream> #include <QTextStream>
#include <memory>
using namespace Snore; using namespace Snore;
class Loger static int debugLevel = 0;
{ static std::unique_ptr<QTextStream> logFile = std::unique_ptr<QTextStream>();
public: static std::unique_ptr<QFile> file = std::unique_ptr<QFile>();
static int s_debugLevel;
static inline int debugLvl() static void init(){
{ debugLevel = qgetenv("LIBSNORE_DEBUG_LVL").toInt();
if (s_debugLevel == -1) { if (qgetenv("LIBSNORE_LOG_TO_FILE").toInt() == 1) {
s_debugLevel = qgetenv("LIBSNORE_DEBUG_LVL").toInt();
}
return s_debugLevel;
}
static inline bool isLogToFileEnabled()
{
static int s_logToFile = -1;
if (s_logToFile == -1) {
s_logToFile = qgetenv("LIBSNORE_LOG_TO_FILE").toInt();
}
return s_logToFile == 1;
}
static inline QTextStream &logFile()
{
static QTextStream *s_out = nullptr;
static QFile *s_file = nullptr;
if (!s_out) {
QString name = QString("%1/libsnore/%2-log.txt").arg(QDir::tempPath(), qApp->applicationName().isEmpty() ? QString::number(qApp->applicationPid()) : qApp->applicationName()); QString name = QString("%1/libsnore/%2-log.txt").arg(QDir::tempPath(), qApp->applicationName().isEmpty() ? QString::number(qApp->applicationPid()) : qApp->applicationName());
if (!qgetenv("LIBSNORE_LOGFILE").isNull()) { if (!qgetenv("LIBSNORE_LOGFILE").isNull()) {
name = QString(qgetenv("LIBSNORE_LOGFILE")); name = QString(qgetenv("LIBSNORE_LOGFILE"));
@ -60,18 +43,14 @@ public:
} }
std::cout << "Started logging to " << name.toLocal8Bit().constData() << std::endl; std::cout << "Started logging to " << name.toLocal8Bit().constData() << std::endl;
s_file = new QFile(name); file = std::unique_ptr<QFile>(new QFile(name));
if (!s_file->open(QFile::WriteOnly)) { if (!file->open(QFile::WriteOnly)) {
qFatal("Failed to open log file %s", qPrintable(name)); qFatal("Failed to open log file %s", qPrintable(name));
} }
s_out = new QTextStream(s_file); logFile = std::unique_ptr<QTextStream>(new QTextStream(file.get()));
} }
return *s_out;
} }
Q_COREAPP_STARTUP_FUNCTION(init)
};
int Loger::s_debugLevel = -1;
SnoreLog::SnoreLog(SnoreDebugLevels lvl): SnoreLog::SnoreLog(SnoreDebugLevels lvl):
QDebug(&m_msg), QDebug(&m_msg),
@ -81,18 +60,18 @@ SnoreLog::SnoreLog(SnoreDebugLevels lvl):
SnoreLog::~SnoreLog() SnoreLog::~SnoreLog()
{ {
if (Loger::isLogToFileEnabled()) { if (logFile) {
Loger::logFile() << m_msg << "\n"; *logFile << m_msg << "\n";
Loger::logFile().flush(); logFile->flush();
} }
if (m_lvl == SNORE_WARNING) { if (m_lvl == SNORE_WARNING) {
std::cerr << m_msg.toLocal8Bit().constData() << std::endl; std::cerr << m_msg.toLocal8Bit().constData() << std::endl;
} else if (Loger::debugLvl() >= m_lvl) { } else if (debugLevel >= m_lvl) {
std::cout << m_msg.toLocal8Bit().constData() << std::endl; std::cout << m_msg.toLocal8Bit().constData() << std::endl;
} }
} }
void SnoreLog::setDebugLvl(int i) void SnoreLog::setDebugLvl(int i)
{ {
Loger::s_debugLevel = i; debugLevel = i;
} }