使用C ++或QT生成.trc文件 [英] generating .trc file in C++ or QT

查看:100
本文介绍了使用C ++或QT生成.trc文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我在Qt中编写了一个GUI来控制硬件:控制参数是温度,几个电压,几个压力,几个切换器等等..



现在我想创建一个LogFile,每次完成一次测量时都会保存所有参数(例如每秒使用TimerEvent)。



LogFile中的一个测量应该在测量时间之后引入,然后是测量参数的描述,最后是参数值。



我想从已经处理过这类问题的人的经验中学习!!






1)生成这样一个LogFile的最佳方法是什么?



2)Qt提供的内置意味着什么用于创建LogFile?









感谢所有提示! !!

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!!!

推荐答案

Qt提供 QFile [ ^ ]可以一起使用的课程使用 QTextStream [ ^ ]类。



但是没有必要使用Qt类。您可以使用C或C ++标准库文件I / O函数来创建文件或打开现有文件并写入文件。



要将格式化文本写入文件,我更喜欢C库函数 fopen fclose fprintf 因为 fprintf 格式很简单。



我通常创建一个类来实现日志记录和在主应用程序模块中使用该类的实例(例如,带有Qt应用程序的 QMainWindow 派生类)。



这样的类通常会实现打开,关闭和写入文件的函数。 write函数可以使用 printf /> va_list [ ^ ]。写入功能还可以在输出前加上时间戳。这可以通过函数的参数可选地控制。具有日志级别参数和相应的成员变量也是常见的。因此,只有那些消息被记录在传递的级别低于或等于成员变量的位置。



如果您在短时间内有许多日志操作,则不应关闭日志写入之后的文件,但让它保持打开,直到应用程序终止。但是,当这样做时,文件可能会在应用程序崩溃时损坏。

更正:文件通常没有损坏,但最近的输出将不存在。
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.
Correction: The file usually gets not corrupted but the recent outputs will be not present.


第一个解决方案很好,但是如果你想使用某些类,我没有看到任何问题,特别是你知道它们。



我的经验提示:



- 写一些统计数据,比如时间和日期
- 确保数据易于阅读。 (txt,xml或html)

- 如果有趣的数据可用,则每次都写入并保存文件。

- 考虑一下文件大小,日期或运行时的逻辑。

- 奖励是将日志记录外包给额外的线程。



看看这个一个简单的日志文件文章。
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.


这篇关于使用C ++或QT生成.trc文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆