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
- // LOG CLASS & Log
- std::ostream& _Log();
- #define QLog _Log()
- {
- Q_OBJECT
- public:
- void Init();
- void InitText();
- private:
- 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
- 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?
4 replies
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]
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.
You must log in to post a reply. Not a member yet? Register here!




