Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello to all,

I wrote a GUI in Qt to control hardware: the control parameters are temperature, several voltages, several pressures, several switchers and so on..

Now I would like to create a LogFile where all the parameters will be saved every time one measurement is done (for instance with TimerEvent every second).

One Measurement in the LogFile should be introduced by the measuring time then followed by the description of the measured parameter and finally followed by the paramter value.

I would like to learn from the experience of people who already dealt with this kind of problem!!



1) What is the best method to GENERATE such a LogFile?

2) What builtin means do Qt offer for creating a LogFile?




Thanks for all hints!!!
Posted

Qt provides the QFile[^] class that can be used with together with the QTextStream[^] class .

But it is not really necessary to use the Qt classes. You can just use the C or C++ standard library file I/O functions to create a file or open an existing one and write to it.

To write formatted text to files, I still prefer the C library functions fopen, fclose, and fprintf because formatting is quite simple with fprintf.

I usually create a class to implement logging and use an instance of that class in the main application module (e.g. the QMainWindow derived class with Qt applications).

Such a class would typical implement functions to open, close, and write to the file. The write functions may accept strings or even a format string with variable arguments like printf using va_list[^]. The write functions may also prefix the output with a time stamp. This can be optionally controlled by a parameter to the functions. It is also common to have a log level parameter and a corresponding member variable. So only those messages are logged where the passed level is below or equal to the member variable.

If you have many log operations in short intervals you should not close the log file after writing to it but let it stay open until the application terminates. However, when doing so the file may get corrupted when your application crashes.
[EDIT] Correction: The file usually gets not corrupted but the recent outputs will be not present.
 
Share this answer
 
v2
Comments
Afzaal Ahmad Zeeshan 6-Nov-15 8:35am    
5ed.
Jochen Arndt 6-Nov-15 8:41am    
Thank you. You were quite fast (just after I have posted the solution).
Afzaal Ahmad Zeeshan 6-Nov-15 9:35am    
The answer was obvious in your second paragraph and the rest deserved the 5!
Ramiien 9-Nov-15 11:05am    
What exactly i am looking for is as following
Check the size of the file if size > 50 MB
than
generate new logger_i+1.trc file
and write on the file
Else
write on logger_i.trc file

which method would be better and easy?
Jochen Arndt 9-Nov-15 11:20am    
There is no "better" or "easier" version. It is up to you to decide. If you want to know the size you can use the size() member function of an opened QFile or the QFileInfo class for an unopened file. But all these Qt classes will call the standard library functions internally (the stat() function for file information).

There may be two reasons for the standard library functions:
- They are even more portable (does not require Qt)
- They are a little bit faster (no overhead)
But the last does not really care because the disk access itself requires much more time than any overhead by a wrapping class.
The first solution is fine, but I dont see any problems if you like to use some classes, especially you know them.

Some tips from my experience:

- write some statistics, like time and date
- make sure, that the data is easily readable. (txt, xml, or html)
- write and save the file every time, if it interesting data is available.
- Think about some logic for the file size, date or runtime.
- a bonus is, to outsource the logging to an extra thread.

Take a look at this a simple logfile article.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900