muduo之LogFile
生活随笔
收集整理的這篇文章主要介紹了
muduo之LogFile
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
? ? ? ??muduo之LogFile,LogFile是控制日志怎么和文件打交道,其中包含常用的對日志處理的一些操作。AsyncLogging異步日志需要調用LogFile的接口將日志寫入文件,其中包含了AppendFile類,相當于再AppendFile上面封裝了一層。
LogFile.h
// Use of this source code is governed by a BSD-style license // that can be found in the License file. // // Author: Shuo Chen (chenshuo at chenshuo dot com)#ifndef MUDUO_BASE_LOGFILE_H #define MUDUO_BASE_LOGFILE_H#include "muduo/base/Mutex.h" #include "muduo/base/Types.h"#include <memory>namespace muduo {namespace FileUtil { class AppendFile; }//控制日志怎樣和文件打交道 class LogFile : noncopyable {public:LogFile(const string& basename,off_t rollSize,bool threadSafe = true,int flushInterval = 3,int checkEveryN = 1024);~LogFile();void append(const char* logline, int len);void flush();bool rollFile();private:void append_unlocked(const char* logline, int len); //不加鎖的append方式static string getLogFileName(const string& basename, time_t* now); //獲取日志文件的名稱const string basename_; //日志文件basenameconst off_t rollSize_; //日志文件達到rolsize生成一個新文件const int flushInterval_; //日志寫入間隔時間const int checkEveryN_; // 每調用checkEveryN_次日志寫,就滾動一次日志int count_; // 寫入的次數std::unique_ptr<MutexLock> mutex_; //加鎖time_t startOfPeriod_; //開始記錄日志時間time_t lastRoll_; //上一次滾動日志文件時間time_t lastFlush_; //上一次日志寫入文件時間std::unique_ptr<FileUtil::AppendFile> file_; //文件智能指針const static int kRollPerSeconds_ = 60*60*24; //即時間一天 };} // namespace muduo #endif // MUDUO_BASE_LOGFILE_HLogFile.cc
// Use of this source code is governed by a BSD-style license // that can be found in the License file. // // Author: Shuo Chen (chenshuo at chenshuo dot com)#include "muduo/base/LogFile.h"#include "muduo/base/FileUtil.h" #include "muduo/base/ProcessInfo.h"#include <assert.h> #include <stdio.h> #include <time.h>using namespace muduo;控制日志怎樣和文件打交道 LogFile::LogFile(const string& basename,off_t rollSize,bool threadSafe,int flushInterval,int checkEveryN): basename_(basename),rollSize_(rollSize),flushInterval_(flushInterval),checkEveryN_(checkEveryN),count_(0),mutex_(threadSafe ? new MutexLock : NULL), //不是線程安全就不需要構造mutex_startOfPeriod_(0),lastRoll_(0),lastFlush_(0) {assert(basename.find('/') == string::npos); //斷言basename不包含'/'rollFile(); }LogFile::~LogFile() = default;void LogFile::append(const char* logline, int len) {if (mutex_){MutexLockGuard lock(*mutex_);append_unlocked(logline, len);}else{append_unlocked(logline, len);} }void LogFile::flush() {if (mutex_){MutexLockGuard lock(*mutex_);file_->flush();}else{file_->flush();} }void LogFile::append_unlocked(const char* logline, int len)//不加鎖的append方式 {file_->append(logline, len);//AppendFile->appendif (file_->writtenBytes() > rollSize_){rollFile();}else{++count_;if (count_ >= checkEveryN_){count_ = 0;time_t now = ::time(NULL);time_t thisPeriod_ = now / kRollPerSeconds_ * kRollPerSeconds_;//比較事件是否相等,不等就是第二天0點,那么滾動if (thisPeriod_ != startOfPeriod_){rollFile();}else if (now - lastFlush_ > flushInterval_)//判斷是否超過flush間隔時間,超過了就flush,否則什么都不做{lastFlush_ = now;file_->flush();}}} }bool LogFile::rollFile()//滾動 {time_t now = 0;string filename = getLogFileName(basename_, &now);獲取生成一個文件名稱//注意,這里先除KRollPerSeconds然后乘KPollPerSeconds表示對齊值KRollPerSeconds的整數倍,也就是事件調整到當天零點(/除法會引發取整)time_t start = now / kRollPerSeconds_ * kRollPerSeconds_;if (now > lastRoll_){lastRoll_ = now;lastFlush_ = now;startOfPeriod_ = start;file_.reset(new FileUtil::AppendFile(filename));return true;}return false; }string LogFile::getLogFileName(const string& basename, time_t* now)//獲取日志文件的名稱 {string filename;filename.reserve(basename.size() + 64);//預分配內存filename = basename;char timebuf[32];struct tm tm;*now = time(NULL);gmtime_r(now, &tm); // FIXME: localtime_r ? //獲取當前時間,UTC時間,_r是線程安全strftime(timebuf, sizeof timebuf, ".%Y%m%d-%H%M%S.", &tm);//格式化時間,strftime函數filename += timebuf;filename += ProcessInfo::hostname();char pidbuf[32];snprintf(pidbuf, sizeof pidbuf, ".%d", ProcessInfo::pid());filename += pidbuf;filename += ".log";return filename; }?
總結
以上是生活随笔為你收集整理的muduo之LogFile的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: muduo之FileUtil
- 下一篇: muduo之GzipFile