webrtc 维护方法三(webrtc 日志写文件方法)

一、定义类

class FileLog : public rtc::LogSink {
  public:
    FileLog(const std::string& LogPath)
      :logfile_(NULL),
      log_path_(LogPath){}
    
    virtual ~FileLog() {
      if (logfile_) {
        fclose(logfile_);
        logfile_ = NULL;
      }
    }
  
    inline void FileDate() {
      char logdate[125];
      
      time_t curtm;
      struct tm* tm_info;
      
      curtm = time(NULL);
      tm_info = localtime(&curtm);
      
      strftime(logdate, sizeof(logdate), "%Y-%m-%d_%H-%M-%S", tm_info);
      
      logfileName_ = log_path_+"webrtc_"+logdate+".log";
    }
    
    inline size_t Size()
    {
      size_t size = 0;
      if (logfile_ != NULL) {
        size = filelength(fileno(logfile_));
      }
      return size;
    }
  
    inline void Start(void)
    {
      #define MAX_LOG_FILE_SIZE (1024*1024)
      
      if (NULL == logfile_) {
        FileDate();
        logfile_ = fopen(logfileName_.c_str(), "w");
      }
      else if (Size() > MAX_LOG_FILE_SIZE)
      {
        Close();
        FileDate();
        logfile_ = fopen(logfileName_.c_str(), "w");
      }
    }
  
    inline void Close(void)
    {
      if(logfile_) {
        fclose(logfile_);
        logfile_ = NULL;
      }
    }

    virtual void OnLogMessage(const std::string& message) {
      rtc::CritScope lock(&log_crit_);
      Start();
      
      if (NULL == logfile_)
        return;

      if(fwrite(message.c_str(), 1, message.length(), logfile_) < 0) {
        Close();
      } else if(fflush(logfile_) < 0) {
        Close();
      }
    }
  
  private:
    
    FILE* logfile_;
    const std::string log_path_;
    std::string logfileName_;


    rtc::CriticalSection log_crit_;
  };

二、调用类

        rtc::LoggingSeverity min_sev = rtc::LS_ERROR;
        switch (webrtc_log_level)
        {
            case 1 :
                min_sev = rtc::LS_ERROR;
                break;
            case 2 :
                min_sev = rtc::LS_WARNING;
                break;
            case 3 :
                min_sev = rtc::LS_INFO;
                break;
        }
        FileLog* _LogStream = new FileLog("logs");
        rtc::LogMessage::AddLogToStream(_LogStream, min_sev); 

 

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值