[Interest] QTextStream doesn't write to QFile
Hi. I'm doing a simple log class. I'm sure that Log creates after QCoreApplication. QCoreApplication is static. I create QCoreApplication as: static QSharedPointer QCoreApplication application( int argc = 0, char ** argv = 0 ) { static QSharedPointer QCoreApplication app( new QCoreApplication( argc, argv ) ); return app; } But this Log doesn't write anything to the file. File creates, I'm sure in it. But nothing writes. What is the reason? And another problem is that QFile in destructor crashes application. Why? --- //! Severity enum Severity { //! Info. Info = 0, //! Warning. Warning = 1, //! Error. Error = 2, //! Critical. Critical = 3 }; // enum Severity // // Log // class LogPrivate; //! Log. class Log { public: static Log instance(); Log operator [] ( Severity s ); Log operator ( const QString data ); Log operator ( const char * data ); private: Log(); ~Log(); private: Q_DISABLE_COPY( Log ) QScopedPointer LogPrivate d; }; // class Log #define LOG( S ) Log::instance()[ S ] --- And here is implementation: --- // // LogPrivate // class LogPrivate { public: LogPrivate() : severity( Info ) { stream.setCodec( QTextCodec::codecForName( UTF-8 ) ); } ~LogPrivate() { stream.flush(); if( file.isOpen() ) // HERE APPLICATION CRASHES !!! file.close(); } void initLogFile( const QDate d ) { if( date != d ) { stream.flush(); if( file.isOpen() ) file.close(); file.setFileName( QLatin1String( ./log/test. ) + d.toString( QLatin1String( yy.MM.dd ) ) + QLatin1String( .log ) ); file.open( QIODevice::WriteOnly | QIODevice::Append ); stream.setDevice( file ); date = d; } } QString severityToString() const { switch( severity ) { case Info : return QLatin1String( info ); case Warning : return QLatin1String( warning ); case Error : return QLatin1String( error ); case Critical : return QLatin1String( critical ); default : return QString(); } } //! Severity. Severity severity; //! Stream. QTextStream stream; //! File. QFile file; //! Date. QDate date; }; // class LogPrivate // // Log // Log Log::instance() { static Log log; return log; } Log Log::operator [] ( Severity s ) { d-severity = s; d-initLogFile( QDate::currentDate() ); d-stream \n [ QTime::currentTime().toString( QLatin1String( hh:mm:ss ) ) ] [ d-severityToString() ] ; d-severity = Info; return *this; } Log Log::operator ( const QString data ) { d-stream data; return *this; } Log Log::operator ( const char * data ) { Log::operator ( QString( data ) ); return *this; } Log::Log() : d( new LogPrivate ) { } Log::~Log() { } --- -- Best Regards, Igor Mironchik. ___ Interest mailing list Interest@qt-project.org http://lists.qt-project.org/mailman/listinfo/interest
Re: [Interest] QTextStream doesn't write to QFile
Den 16-01-2015 kl. 13:01 skrev Igor Mironchik: Hi. On Fri, 16 Jan 2015 14:44:08 +0300, Koehne Kai kai.koe...@theqtcompany.com wrote: -Original Message- From: interest-bounces+kai.koehne=theqtcompany@qt-project.org [mailto:interest-bounces+kai.koehne=theqtcompany@qt-project.org] On Behalf Of Igor Mironchik Sent: Friday, January 16, 2015 12:24 PM To: interest@qt-project.org Subject: [Interest] QTextStream doesn't write to QFile Hi. I'm doing a simple log class. I'm sure that Log creates after QCoreApplication. QCoreApplication is static. I create QCoreApplication as: static QSharedPointer QCoreApplication application( int argc = 0, char ** argv = 0 ) { static QSharedPointer QCoreApplication app( new QCoreApplication( argc, argv ) ); return app; } It seems you're desperately trying here to extend QCoreApplications lifetime as long as possible. This is just asking for trouble: just let QCoreApplication be destructed when your application exits, in main.cpp. I'm not desperately trying to extens QCoreApplication lifetime. I need it for that reason that Log is singleton and uses QObjects. So Log have to be destructed before QCoreApplication. It's normal practice when using singletons in Qt apps. It works well in other my application. If you want to delete the singletons before the QApplication, call a delete on them after exec() returns. The order of static deletes is undefined. It does not work well in any application other than by pure chance. But why would you think that a QObject can't be deleted after QApplication? That works fine, as long as you don't do something else in the destructors that use any objects deleted by the QApplication. One thing you can do to make certain objects are deleted before the qApp is to make qApp the parent of the singletons. This also works if QApplication is instantiated on the stack. Bo. -- Viking Software Qt and C++ developers for hire http://www.vikingsoft.eu ___ Interest mailing list Interest@qt-project.org http://lists.qt-project.org/mailman/listinfo/interest
Re: [Interest] QTextStream doesn't write to QFile
On Fri, 16 Jan 2015 15:25:44 +0300, Bo Thorsen b...@vikingsoft.eu wrote: Den 16-01-2015 kl. 13:01 skrev Igor Mironchik: Hi. On Fri, 16 Jan 2015 14:44:08 +0300, Koehne Kai kai.koe...@theqtcompany.com wrote: -Original Message- From: interest-bounces+kai.koehne=theqtcompany@qt-project.org [mailto:interest-bounces+kai.koehne=theqtcompany@qt-project.org] On Behalf Of Igor Mironchik Sent: Friday, January 16, 2015 12:24 PM To: interest@qt-project.org Subject: [Interest] QTextStream doesn't write to QFile Hi. I'm doing a simple log class. I'm sure that Log creates after QCoreApplication. QCoreApplication is static. I create QCoreApplication as: static QSharedPointer QCoreApplication application( int argc = 0, char ** argv = 0 ) { static QSharedPointer QCoreApplication app( new QCoreApplication( argc, argv ) ); return app; } It seems you're desperately trying here to extend QCoreApplications lifetime as long as possible. This is just asking for trouble: just let QCoreApplication be destructed when your application exits, in main.cpp. I'm not desperately trying to extens QCoreApplication lifetime. I need it for that reason that Log is singleton and uses QObjects. So Log have to be destructed before QCoreApplication. It's normal practice when using singletons in Qt apps. It works well in other my application. If you want to delete the singletons before the QApplication, call a delete on them after exec() returns. The order of static deletes is undefined. It does not work well in any application other than by pure chance. But why would you think that a QObject can't be deleted after QApplication? That works fine, as long as you don't do something else in the destructors that use any objects deleted by the QApplication. Few years ago in this list Nils Jeisecke wrote to me that QWidget's destructor needs QApplication instance. So I think that QObject's destructor needs QCoreApplication. One thing you can do to make certain objects are deleted before the qApp is to make qApp the parent of the singletons. This also works if QApplication is instantiated on the stack. Thank you for your suggestions. -- Best Regards, Igor Mironchik. ___ Interest mailing list Interest@qt-project.org http://lists.qt-project.org/mailman/listinfo/interest