如何限制c++中的日志大小?

发布于 2024-10-25 02:37:03 字数 359 浏览 3 评论 0原文

在我的应用程序中,我正在创建大小为 5kb 的日志文件。如果文件大小超过 5kb,我必须借助新内容覆盖旧内容。

如果您有任何想法,请与我分享。

我还需要在 C++ 中实现此技术

我提供了一些示例

最初该文件看起来像这样的

sample.txt

示例应用程序文本 示例

假设上面的示例文本文件超过5kb,然后我在source.txt中添加了new,那么文件应该是这样的。

Sample.txt

新的示例应用程序文本 萨姆

问候, 卡蒂克

In my application i am creating log file of size 5kb .If i exceeds 5 kb of file size , I have to overwrite the old contents with the help of new contents.

If you have any ideas just share with me.

I also need implementation of this technique in c++

I provide some example

Initially the file look like this

sample.txt

sample application text
sample

Assume the above sample text file exceeds 5kb then i added new in the source.txt then the file should be like this.

sample.txt

new sample application text
sam

Regards,
Karthik

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

疯了 2024-11-01 02:37:03

以下是我最近编写的一些代码,用于实现简单的日志文件轮换:

std::ostream & libLogging::FileRotationLogSink::GetCurrentStream( 
    std::string::size_type required )
{
    if ( static_cast<std::string::size_type>(m_CurrentStream.tellp( )) + 
        required > m_Limit ) {
        m_CurrentStream.close();
        // remove old backup
        if ( boost::filesystem::exists( m_BackupPath ) ) {
            boost::filesystem::remove( m_BackupPath );
        }
        // backup current logfile
        boost::filesystem::rename( m_LogFilePath, m_BackupPath );
        // open new logfile
        m_CurrentStream.open( m_LogFilePath );
    }
    return m_CurrentStream;
}

required 给出要写入日志的下一条消息的大小。如果文件太大,则会复制该文件(覆盖旧备份),然后启动一个新备份。

Here is some code I have recently written to implement a simple log-file rotation:

std::ostream & libLogging::FileRotationLogSink::GetCurrentStream( 
    std::string::size_type required )
{
    if ( static_cast<std::string::size_type>(m_CurrentStream.tellp( )) + 
        required > m_Limit ) {
        m_CurrentStream.close();
        // remove old backup
        if ( boost::filesystem::exists( m_BackupPath ) ) {
            boost::filesystem::remove( m_BackupPath );
        }
        // backup current logfile
        boost::filesystem::rename( m_LogFilePath, m_BackupPath );
        // open new logfile
        m_CurrentStream.open( m_LogFilePath );
    }
    return m_CurrentStream;
}

required gives the size of the next message that is to be written to the log. If the file gets too big, it is copied (old backup is overwritten), and a new one is started.

手长情犹 2024-11-01 02:37:03

使用 WinApi,您应该

1) 使用 GetFileSize 检查文件是否大于限制
2) SetFilePointer 为 0,0 + SetEndOfFile

Using WinApi, you should

1) Check if file is bigger than a limit using GetFileSize
2) SetFilePointer to 0,0 + SetEndOfFile

始于初秋 2024-11-01 02:37:03

你可以统计你插入日志的内容量,并检查每次是否超过5kb。使用如下函数:

void writeToLog(char c) {
  if(writeIndex == 5000)
    writeIndex =0;
  log[writeIndex] = c;
  writeIndex += 1;
}

这样就可以实现字符串写入功能了。

You can count the amount of content you inserted into the log, and check if it is more the 5kb every time. Use a function like:

void writeToLog(char c) {
  if(writeIndex == 5000)
    writeIndex =0;
  log[writeIndex] = c;
  writeIndex += 1;
}

With this sure you can implement the string writing function.

遗心遗梦遗幸福 2024-11-01 02:37:03

由于您使用的是MFC,因此可以使用CFile类进行文件管理。此类有一个 GetLength 方法,用于返回文件的大小。

要覆盖旧内容,您可以管理代表文件内容的 5000 个字符的缓冲区。在每次写入文件时,您只需替换文件内容即可。

Since you are using MFC, you can use the CFile class for file management. This class has a GetLength method that return the size of the file.

To overwrite the old content you can manage a buffer of 5000 char representing your file content. And at each file writing you just have to replace file content.

小苏打饼 2024-11-01 02:37:03

像下面这样的方法可以工作:

  1. 使用 iostreams 将内容流式传输到文件(每次打开时都从一个干净的文件开始)
  2. 流式传输内容后,使用tellp获取最后放置的位置(以及文件的大小)迄今为止)。
  3. 如果位置大于 5Kb,则 seekp 到流的开头...

An approach like the following could work:

  1. Stream content to file using iostreams (start with a clean file each time you open)
  2. After streaming content, use tellp to get the last put position (and hence size of file so far).
  3. If the position is bigger than 5Kb, then seekp to begining of stream...
忘羡 2024-11-01 02:37:03

log4cxx 是一个很好的解决方案,它已经解决了这个问题以及更多您可能还没有解决的问题尚未考虑。

log4cxx is a good soution that has already solved this problem plus many more you probably haven't yet considered.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文