Small problem · QFlie of Qt development

describe

        Saving the log should be one of the must-haves for development, and the saving methods vary widely, but in the end it must be saved to a file.

        It will definitely involve the operation of writing files, but writing files is a very time-consuming process. If improper operations are added, such as writing byte by byte, the time consumption can be imagined. Most of the solutions are to use the cache to temporarily store the content to be written, and then write it once when the content to be written reaches a certain requirement, which can avoid improper operation of the developer, and at the same time add the cache application when writing the file When , you only need to write to the cache and then return, giving the application the illusion of writing to the file, so that it can do more things.

        In this way, there will be a problem. If the write operation requires high real-time performance, this will definitely not work. For example, if the database is cached, when a large amount of data is modified, there will inevitably be a problem of data out-of-sync, so the cache is generally It is optional or provides a method to write the cache to a file and return it, such as the Linux system function -sync.

        When using the QFlie class in Qt to save the log, it is found that the latest log is always not saved, that is, the real-time performance is not high, and then QFlie does not seem to provide a method for refreshing the cache like sync --- maybe I do not Found it, let me know if you know it . But the close method brings sync, but there is no separate sync method, which makes it impossible to improve the real-time performance of the log, but the overhead of calling close frequently is relatively high.

 Solution

1. Instead of using QFlie, use the system call write

2. Do not use QFlie, use the library to call fwrite, and call sync after each write operation

3. Use QFlie to find methods like sync

Supongo que te gusta

Origin blog.csdn.net/m0_64560763/article/details/130942510
Recomendado
Clasificación