zoukankan      html  css  js  c++  java
  • log4cpp日志不能是溶液子体积

    

    我们的项目用途log4cpp由于日志输出模块,但在使用中发现,假设Services,或者是在Windows Server版本号。不会有一个正常的日志切削现象。该日志已被写入到文件中,持续,即使超过规定的文件大小。也不会分卷。

    log4cpp中切割日志的核心算法为:(如果同意的最大文件个数为4)
    1.关闭xxx.log.
    2.删除 xxx.log.4
    3.是一个loop, 将xxx.log.3--->xxx.log.4,xxx.log.2--->xxx.log.3,xxx.log.1----->xxx.log.2
    4.将xxx.log--->xxx.log.1
    5.打开xxx.log.

    相关代码为:  

    void RollingFileAppender::rollOver() {  
          ::close(_fd); // 1  
          if (_maxBackupIndex > 0) {  
               std::ostringstream oldName;  
               oldName << _fileName << "." << _maxBackupIndex << std::ends;  
               ::remove(oldName.str().c_str());   //2  
      
               size_t n = _fileName.length() + 1;  
               for(unsigned int i = _maxBackupIndex; i > 1; i--) {  //3  
                     std::string newName = oldName.str();  
                     oldName.seekp(n);  
                     oldName << i-1 << std::ends;  
                     ::rename(oldName.str().c_str(), newName.c_str());   
                }  
      
               ::rename(_fileName.c_str(), oldName.str().c_str()); //4  
          }  
          _fd = ::open(_fileName.c_str(), _flags, _mode);//5  
    }  
    

    日志文件无法切割(眼下发现仅仅在win server版本号无法work),原因出在步骤1,4,5上。
    关闭文件后,随后将文件重命名,会导致重命名失败。通过打印错误码得知,错误码为32,意思为:文件句柄被占用。
    解决方式为。往两个不同的文件中中写日志。不再仅仅往一个文件名称里写日志,交替写日志,交替关闭文件。write(A),close(B)---->Write(B),Close(A),----->Write(A),Close(B).
    改动后的代码为:

    void RollingFileAppender::rollOver() {  
            ::close(_fd);  
            if (_maxBackupIndex > 0) {  
                std::ostringstream oldName;  
                oldName << _fileName << "." << _maxBackupIndex << std::ends;  
                ::remove(oldName.str().c_str());  
                size_t n = _fileName.length() + 1;  
                for(unsigned int i = _maxBackupIndex; i > 1; i--) {  
                    std::string newName = oldName.str();  
                    oldName.seekp(n);  
      
                    oldName << i-1 << std::ends;  
                    ::rename(oldName.str().c_str(), newName.c_str());  
                }  
    if(_bUsingTempFile)  
                ::rename(_fileNameTmp.c_str(), oldName.str().c_str());  
    else  
                ::rename(_fileName.c_str(), oldName.str().c_str());  
            }  
    if(_bUsingTempFile)  
            _fd = ::open(_fileName.c_str(), _flags, _mode);  
    else  
            _fd = ::open(_fileNameTmp.c_str(), _flags, _mode);  
            _bUsingTempFile = !_bUsingTempFile;  
        }  

    版权声明:本文博主原创文章。博客,未经同意不得转载。

  • 相关阅读:
    9月7日总结
    Arbitrage题解
    杀蚂蚁题解
    8月11日总结
    8月10总结
    PHP 关于获取客户端ip的方法
    PHP内置函数大全
    PHP header函数设置http头
    获取两个日期之间的全部的日期数据(包括两个日期)
    根据周日获取这周的周日到周六的日期(周日为这周的第一天)
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/4875222.html
Copyright © 2011-2022 走看看