-
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 appMy current code:
.h
@// LOG CLASS & Log
std::ostream& _Log();
#define QLog _Log()class Log : public QObject
{
Q_OBJECTpublic:
Log(QObject* parent = 0);
void Init();
void InitText();private:
QSettings* settings;
Settings* setting_class;
};@log.cpp
@void Log::Init()
{
// Launcher Dir
QDir dir;
if(!dir.exists(setting_class->Launcher_Dir))
dir.mkdir(setting_class->Launcher_Dir);// Create Log & Init const QString LogPath = setting_class->Launcher_Dir + "//" + setting_class->Launcher_LogName; std::ofstream LogStream; LogStream.open(LogPath.toStdString(), std::ios_base::out | std::ios_base::trunc); if(LogStream) { std::cout.rdbuf(LogStream.rdbuf()); std::clog.rdbuf(LogStream.rdbuf()); std::cerr.rdbuf(LogStream.rdbuf()); } QLog << "Test test" << std::endl; // THIS WORKING WRITE SUCCESS
}
@main.cpp
@include "Log.h"int main(int argc, char* argv[])
{
Log log;
log.Init();
QLog << "TEst 3333" << std::endl; // SHUT APP :/
}@Other code i remove for better preview.. ofc i have MainWindow.
Where can be mistake?
-
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 :-) -
How would you propose it?
-
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":http://qt-project.org/doc/qt-4.8/qdebug.html
-
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":http://qt-project.org/doc/qt-4.8/QApplication.html or "QCoreApplication":http://qt-project.org/doc/qt-4.8/qcoreapplication.html, 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.