0

我有一个问题,我在应用程序中记录了一些信息,例如请求、消息、响应,并且在这部分代码中发生了这种情况。我的目标是在应用程序运行时查看日志文件,这似乎现在不会发生,因此用户必须退出应用程序然后转到公用文件夹才能查看日志。如果需要,我可以提供更多信息。

 void LogClass::log(const char* pcszComponent, const char* pcszLevel, const CF1String& title, const CF1String& message)
    {
        m_guard.lock();

        QByteArray byteArray(QTime::currentTime().toString("HH:mm:ss.zzz").toAscii());
        XXString logMessage = XXFormatString("[%s] [%7s] [%4s] [%08X] [%s] [%s]\r\n", byteArray.constData(), pcszLevel, pcszComponent, QThread::currentThreadId(), title.c_str(), message.c_str());

        if (!m_pLogFile) {
            createLogFile();
        }

        if (m_pLogFile) {
            fputs(logMessage.c_str(), m_pLogFile);
            fflush(m_pLogFile);
        }

        m_guard.unlock();
    }

提前致谢。

4

1 回答 1

1

您在每条消息之后使用带有 fflush 的缓冲输出,因此您可能应该能够看到文件更新(因为数据已写入操作系统)。您看不到它的可能原因是操作系统没有将其内部文件缓冲区刷新到磁盘。你可以试试Windows 上的 _commit 函数Linux 上的 fsync

于 2014-04-10T02:29:43.257 回答