Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

medLoggerRedirectWithBoost #784

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 53 additions & 4 deletions src/app/medInria/medLogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,27 @@
=========================================================================*/
#include "medLogger.h"
#include <QTextStream>
#include <iostream>

typedef boost::iostreams::tee_device<std::ostream, std::ofstream> TeeDevice;
typedef boost::iostreams::stream<TeeDevice> TeeStream;

class medLoggerPrivate
{
public:
static medLogger* singleton;
static bool logAccessFlag;

std::ofstream logFile;
QList<std::ostream*> redirectedStreams;
QList<std::ostream*> redirectedStreamDummies;
QList<TeeStream*> teeStreams;
QList<std::streambuf*> previousStreamBuffers;

static const qint64 maxLogSize = 5000000;
static const qint64 minLogSize = 1000000;

medLoggerPrivate() : logFile(dtkLogPath(qApp).toLocal8Bit().data(), std::ios::app) {}
};

medLogger* medLoggerPrivate::singleton = nullptr;
Expand Down Expand Up @@ -89,21 +101,29 @@ void medLogger::redirectQtMessage(QtMsgType type, const QString& message)
}
}

void medLogger::redirectMessage(const QString& message)
{
dtkTrace() << message;
}

void medLogger::redirectErrorMessage(const QString& message)
{
dtkError() << message;
}

medLogger::medLogger() : d(new medLoggerPrivate)
{
qRegisterMetaType<QtMsgType>("QtMsgType");

initializeTeeStreams();

truncateLogFileIfHeavy();

dtkLogger::instance().setLevel(logLevel);

dtkLogger::instance().attachFile(dtkLogPath(qApp));
dtkLogger::instance().attachConsole();

// Redirect cerr and cout messages
dtkLogger::instance().redirectCerr();
dtkLogger::instance().redirectCout();

// Redirect Qt messages
QObject::connect(this, SIGNAL(newQtMessage(QtMsgType,QString)), this, SLOT(redirectQtMessage(QtMsgType,QString)));
qInstallMessageHandler(qtMessageHandler);
Expand All @@ -116,6 +136,35 @@ medLogger::~medLogger()

QObject::disconnect(this, SIGNAL(newQtMessage(QtMsgType,QString)), this, SLOT(redirectQtMessage(QtMsgType,QString)));
qInstallMessageHandler(nullptr);
finalizeTeeStreams();
}

void medLogger::initializeTeeStreams()
{
createTeeStream(&std::cout);
createTeeStream(&std::cerr);
}

void medLogger::finalizeTeeStreams()
{
for (int i = 0; i < d->redirectedStreams.length(); i++)
{
d->teeStreams.first()->flush();
d->teeStreams.first()->close();
delete d->teeStreams.takeFirst();
delete d->redirectedStreamDummies.takeFirst();
d->redirectedStreams.takeFirst()->rdbuf(d->previousStreamBuffers.takeFirst());
}
}

void medLogger::createTeeStream(std::ostream* targetStream)
{
d->redirectedStreams.append(targetStream);
d->previousStreamBuffers.append(targetStream->rdbuf());
d->redirectedStreamDummies.append(new std::ostream(targetStream->rdbuf()));
TeeDevice* teeDevice = new TeeDevice(*d->redirectedStreamDummies.last(), d->logFile);
d->teeStreams.append(new TeeStream(*teeDevice));
targetStream->rdbuf(d->teeStreams.last()->rdbuf());
}

void medLogger::truncateLogFileIfHeavy()
Expand Down
14 changes: 14 additions & 0 deletions src/app/medInria/medLogger.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ PURPOSE.

=========================================================================*/

#include <fstream>

#ifndef Q_MOC_RUN
#include <boost/iostreams/tee.hpp>
#include <boost/iostreams/stream.hpp>
#endif

#include <dtkLog/dtkLog.h>

#include <QtGui>
Expand Down Expand Up @@ -41,13 +48,20 @@ public :

private slots:
void redirectQtMessage(QtMsgType type, const QString& message);
void redirectMessage(const QString& message);
void redirectErrorMessage(const QString& message);

private:
medLoggerPrivate* const d;

medLogger();
~medLogger();

void initializeTeeStreams();
void finalizeTeeStreams();

void createTeeStream(std::ostream* targetStream);

/** Test the size of the log file and cut if needed
*/
void truncateLogFileIfHeavy();
Expand Down