May 5, 2012

glararan glararan
Lab Rat
81 posts

Log

 

Hi, because im new in Qt i use old method from std to create log, i try project with logging

But always big problem.
After i init log (create and write main infos)
When i next use example “QLog() << “Test123”;” It shut down app

My current code:

.h

  1. // LOG CLASS & Log
  2. std::ostream& _Log();
  3. #define QLog _Log()
  4.  
  5. class Log : public QObject
  6. {
  7.     Q_OBJECT
  8.  
  9. public:
  10.     Log(QObject* parent = 0);
  11.     void Init();
  12.     void InitText();
  13.  
  14. private:
  15.     QSettings* settings;
  16.     Settings* setting_class;
  17. };

log.cpp

  1. void Log::Init()
  2. {
  3.     // Launcher Dir
  4.     QDir dir;
  5.     if(!dir.exists(setting_class->Launcher_Dir))
  6.         dir.mkdir(setting_class->Launcher_Dir);
  7.  
  8.     // Create Log & Init
  9.     const QString LogPath = setting_class->Launcher_Dir + "//" + setting_class->Launcher_LogName;
  10.  
  11.     std::ofstream LogStream;
  12.     LogStream.open(LogPath.toStdString(), std::ios_base::out | std::ios_base::trunc);
  13.     if(LogStream)
  14.     {
  15.         std::cout.rdbuf(LogStream.rdbuf());
  16.         std::clog.rdbuf(LogStream.rdbuf());
  17.         std::cerr.rdbuf(LogStream.rdbuf());
  18.     }
  19.  
  20.     QLog << "Test test" << std::endl; // THIS WORKING WRITE SUCCESS
  21. }

main.cpp

  1. include "Log.h"
  2.  
  3. int main(int argc, char* argv[])
  4. {
  5.    Log log;
  6.    log.Init();
  7.    QLog << "TEst 3333" << std::endl; // SHUT APP :/
  8. }

Other code i remove for better preview.. ofc i have MainWindow.

Where can be mistake?

4 replies

May 5, 2012

koahnig koahnig
Gene Splicer
2906 posts

For my personal opinion there are a couple of things requiring redesign. The logic is not complete to my understanding.
In case QLog is somehow connected to LogStream opened in your Init routine, it is that your ofstream is only local.
However, this is more crystal ball reading :-)

May 5, 2012

glararan glararan
Lab Rat
81 posts

How would you propose it?

May 6, 2012

koahnig koahnig
Gene Splicer
2906 posts

I would propose to use plain ofstream. You try to make it fancy with reassigning rdbuf. I guess this makes it too complicated for yourself. As already explained in the post above. You are creating an ofstream “LogStream” and it is killed as soon as leave the Init routine.

Line 2 and 3 of your header: What is this for? Dangling something without relation to the rest. I do not see a relation to your Log class.

May be you should have a look to QDebug [qt-project.org]

May 6, 2012

sierdzio sierdzio
Area 51 Engineer
4842 posts

Apart from redesign issues:

your code exits on line 7 (8 actually) because that is where your main() ends – so it should not surprise you. In Qt, we usually use QApplication [qt-project.org] or QCoreApplication [qt-project.org], esp. their exec() methods. Take a look at some examples to get to know how to use them, or look how Qt Creator auto-generates code there.

 Signature 

(Z(:^

 
  ‹‹ How do you use Phonon to play a multimedia file in a Qt application on Ubuntu Linux ?      I’m having a problem with a loop not sure whats going on... please help. ››

You must log in to post a reply. Not a member yet? Register here!