Solved Small problem with a log file
-
Hi,
you have not shown how you open log for writing but my guess is that you use wrong mode for your purpose. Please consider QFile docs along with links to QTextStream, QDataStream and QIODevice. -
o my bad: I updated the OP
-
@bask185 QIODevice::WriteOnly implies truncate of the file. You should open with QIODevice::Append instead - that would just... well... append new data at the end of the file.
-
-
Side note: you should also include the
QIODevice::Text
flag -
I find it vague that file.open() can actually deletes the content rather than...just open it when given the wrong argument.
Regardless I succeeded:
Bullseye set at 6.33333 7.33333 << 80 << 3 0x80 command case 3 << 37 Bullseye set at 7.33333 7.33333 << 80 << 3 0x80 command case 3 << 38 Bullseye set at 8.33333 7.33333
In the setup I open the file with Write Only and close it so it becomes.. empty :D
But the output is almost the exact same as qDebug. So tnx. I'd like to mark your answer as the correct one, but one can only mark his own ;)
-
@VRonin said in Small problem with a log file:
Side note: you should also include the
QIODevice::Text
flagIf you say something like: "you should do...". Arguments 'why' are not redundant. Not long ago I held a short speech about it ;) and I came across a nice saying a few days ago: "Give a man a fish and you feed him one day, teach a man how to fish and you feed him for his life"
Right now I have a perfectly fine working log file, so I don't feel like including something called 'Text flag' ;)
-
@bask185 said in Small problem with a log file:
"Give a man a fish and you feed him one day, teach a man how to fish and you feed him for his life"
that's what the docs are for. it takes 5 seconds to Google
QIODevice::Text
@bask185 said in Small problem with a log file:
Right now I have a perfectly fine working log file, so I don't feel like including something called 'Text flag' ;)
As long as you don't care about newlines.
-
@jsulm said in Small problem with a log file:
@bask185 To add to @artwaw : why don't you keep the file open? Opening and closing it for each incoming byte sounds like a huge waste of resources.
Now you mention it... I turned it off but I have another question.
I would like to write data to the log also in other functions but it is a local variable. I tried working with the header file making it more global but I could not get it to work. I also tried
QFile log("logboek.txt"); QTextStream LOG(&log); LOG << cAsHex << " >>";
in other functions, but no luck :(
-
@bask185 Implement a class which handles the logging. This class then creates a private static QFile instance and provides public static methods to write logs. Be careful if you use different threads - then you will need to serialize log writing.
-
@VRonin said in Small problem with a log file:
@bask185 said in Small problem with a log file:
"Give a man a fish and you feed him one day, teach a man how to fish and you feed him for his life"
that's what the docs are for. it takes 5 seconds to Google
QIODevice::Text
@bask185 said in Small problem with a log file:
Right now I have a perfectly fine working log file, so I don't feel like including something called 'Text flag' ;)
As long as you don't care about newlines.
I do care about newlines alot, that is why add them like: LOG << "\nText to translate : "
I've googled and read about it but I am not getting the usage for me, from what I understand it is a windows thingy and it is used for reading files. It sets \r\n to \n for use with C++. -
@jsulm said in Small problem with a log file:
you will need to serialize log writing
You mean working with connects and signals? when class X emits a signal to mainwindow with a string so mainwindow can puts the string in the log using the log-class??
-
@bask185 said in Small problem with a log file:
it is a windows thingy
Mainly yes
and it is used for reading files
Mainly no
From http://doc.qt.io/qt-5/qiodevice.html:
When reading, the end-of-line terminators are translated to '\n'. When writing, the end-of-line terminators are translated to the local encoding, for example '\r\n' for Win32.
That means that
\n
will be converted to "\r\n" on windows automatically if you add| QIODevice::Text
to theopen
argument -
@bask185 No, I did not write anything about signals/slots. I mean a class which manages the log writing. There is no need to do it in main window. Just provide static methods:
class Logger { public: static void log(const QString &text); private: static QFile logFile; }; ... Logger::log("Some text");