Skip to content

Commit

Permalink
Added convenience functions to allow user to set standard and error s…
Browse files Browse the repository at this point in the history
…treams to files. Also split closeLogger() into two functions.
  • Loading branch information
jsbackus committed Sep 20, 2016
1 parent 381f921 commit dd91fba
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ Logger::Logger(QTextStream *stream, QTextStream *errorStream,
Logger::~Logger()
{
closeLogger();
closeErrorLogger();
}

/**
Expand Down Expand Up @@ -186,7 +187,14 @@ void Logger::closeLogger(bool closeStream)
}
}
}
}

/**
* @brief Flushes output stream and closes stream if requested.
* @param Whether to close the current stream. Defaults to true.
*/
void Logger::closeErrorLogger(bool closeStream)
{
if (errorStream)
{
errorStream->flush();
Expand Down Expand Up @@ -355,3 +363,29 @@ void Logger::startPendingTimer()
instance->pendingTimer.start();
}
}

void Logger::setCurrentLogFile(QString filename) {
Q_ASSERT(instance != 0);

if( instance->outputFile.isOpen() ) {
instance->closeLogger(true);
}
instance->outputFile.setFileName( filename );
instance->outputFile.open( QIODevice::WriteOnly | QIODevice::Append );
instance->outFileStream.setDevice( &instance->outputFile );
instance->setCurrentStream( &instance->outFileStream );
instance->LogInfo(QObject::tr("Logging started"), true, true);
}

void Logger::setCurrentErrorLogFile(QString filename) {
Q_ASSERT(instance != 0);

if( instance->errorFile.isOpen() ) {
instance->closeErrorLogger(true);
}
instance->errorFile.setFileName( filename );
instance->errorFile.open( QIODevice::WriteOnly | QIODevice::Append );
instance->outErrorFileStream.setDevice( &instance->errorFile );
instance->setCurrentErrorStream( &instance->outErrorFileStream );
}

9 changes: 9 additions & 0 deletions src/logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <QMutexLocker>
#include <QTextStream>
#include <QTimer>
#include <QFile>

class Logger : public QObject
{
Expand All @@ -48,9 +49,11 @@ class Logger : public QObject
LogLevel getCurrentLogLevel();

static void setCurrentStream(QTextStream *stream);
static void setCurrentLogFile(QString filename);
static QTextStream* getCurrentStream();

static void setCurrentErrorStream(QTextStream *stream);
static void setCurrentErrorLogFile(QString filename);
static QTextStream* getCurrentErrorStream();

QTimer* getLogTimer();
Expand Down Expand Up @@ -125,9 +128,15 @@ class Logger : public QObject

protected:
void closeLogger(bool closeStream=true);
void closeErrorLogger(bool closeStream=true);
void logMessage(LogMessage msg);

QFile outputFile;
QTextStream outFileStream;
QTextStream *outputStream;

QFile errorFile;
QTextStream outErrorFileStream;
QTextStream *errorStream;
LogLevel outputLevel;
QMutex logMutex;
Expand Down

0 comments on commit dd91fba

Please sign in to comment.