Log to file added, with flush option

This commit is contained in:
Klaas Freitag 2012-06-15 13:04:23 +02:00
parent 4d1fed3d00
commit c90eb1cca6
3 changed files with 62 additions and 7 deletions

View file

@ -341,6 +341,19 @@ void Application::setupLogBrowser()
slotOpenLogBrowser();
}
// check for command line option for a log file.
int lf = arguments().indexOf("--logfile");
if( lf > -1 && lf+1 < arguments().count() ) {
QString logfile = arguments().at( lf+1 );
bool flush = false;
if( arguments().contains("--logflush")) flush = true;
qDebug() << "Logging into logfile: " << logfile << " with flush " << flush;
_logBrowser->setLogFile( logfile, flush );
}
qDebug() << QString( "################## %1 %2 %3 ").arg(_theme->appName())
.arg( QLocale::system().name() )
.arg(_theme->version());

View file

@ -105,7 +105,7 @@ void Logger::setEnabled( bool state )
// ==============================================================================
LogWidget::LogWidget(QWidget *parent)
:QTextEdit(parent)
:QTextBrowser(parent)
{
setReadOnly( true );
setLineWrapMode( QTextEdit::NoWrap );
@ -124,7 +124,9 @@ LogWidget::LogWidget(QWidget *parent)
LogBrowser::LogBrowser(QWidget *parent) :
QDialog(parent),
_logWidget( new LogWidget(parent) )
_logWidget( new LogWidget(parent) ),
_logstream(0),
_doFileFlush(false)
{
setWindowTitle(tr("Log Output"));
setMinimumWidth(600);
@ -177,6 +179,13 @@ LogBrowser::LogBrowser(QWidget *parent) :
connect(Logger::instance(), SIGNAL(newLog(QString)),this,SLOT(slotNewLog(QString)), Qt::QueuedConnection);
}
LogBrowser::~LogBrowser()
{
if( _logstream ) {
_logFile.close();
}
}
void LogBrowser::show()
{
QDialog::show();
@ -191,7 +200,31 @@ void LogBrowser::close()
void LogBrowser::slotNewLog( const QString& msg )
{
_logWidget->append( msg );
if( _logWidget->isVisible() ) {
_logWidget->append( msg );
}
if( _logstream ) {
(*_logstream) << msg << endl;
if( _doFileFlush ) _logstream->flush();
}
}
void LogBrowser::setLogFile( const QString & name, bool flush )
{
_logFile.setFileName( name );
if(!_logFile.open(QIODevice::WriteOnly)) {
QMessageBox::warning(
this,
tr("Error"),
QString(tr("<nobr>File '%1'<br/>cannot be opened for writing.<br/><br/>"
"The log output can <b>not</b> be saved!</nobr>"))
.arg(name));
return;
}
_doFileFlush = flush;
_logstream = new QTextStream( &_logFile );
}
void LogBrowser::slotFind()
@ -236,10 +269,9 @@ void LogBrowser::slotSave()
if( ! saveFile.isEmpty() ) {
QFile file(saveFile);
if (file.open(QIODevice::ReadWrite)) {
if (file.open(QIODevice::WriteOnly)) {
QTextStream stream(&file);
stream << _logWidget->toPlainText();
file.flush();
file.close();
} else {
QMessageBox::critical(this, tr("Error"), tr("Could not write to log file ")+ saveFile);

View file

@ -15,7 +15,9 @@
#ifndef LOGBROWSER_H
#define LOGBROWSER_H
#include <QTextEdit>
#include <QTextBrowser>
#include <QTextStream>
#include <QFile>
#include <QObject>
#include <QList>
#include <QDateTime>
@ -65,7 +67,7 @@ protected:
static Logger* _instance;
};
class LogWidget : public QTextEdit
class LogWidget : public QTextBrowser
{
Q_OBJECT
public:
@ -80,6 +82,10 @@ class LogBrowser : public QDialog
Q_OBJECT
public:
explicit LogBrowser(QWidget *parent = 0);
~LogBrowser();
void setLogFile(const QString& , bool );
signals:
public slots:
@ -97,6 +103,10 @@ private:
QLineEdit *_findTermEdit;
QPushButton *_saveBtn;
QLabel *_statusLabel;
QFile _logFile;
bool _doFileFlush;
QTextStream *_logstream;
};
} // namespace