STTNet 0.7.0
Loading...
Searching...
No Matches
stt::file::LogFile Class Reference

日志文件操作类 More...

#include <sttnet.h>

Inheritance diagram for stt::file::LogFile:
stt::time::DateTime stt::file::File stt::file::FileTool

Public Member Functions

 LogFile (const size_t &logQueue_cap=8192)
 
bool openFile (const std::string &fileName, const std::string &timeFormat=ISO8086A, const std::string &contentFormat=" ")
 打开一个日志文件
 
bool isOpen ()
 获取对象是否打开日志文件的状态
 
std::string getFileName ()
 获取对象打开的文件名
 
bool closeFile (const bool &del=false)
 关闭对象打开的日志文件
 
void writeLog (const std::string &data)
 写一行日志
 
uint64_t getDroppedLogCount () const noexcept
 返回日志队列满时累计丢弃的日志条数。
 
bool clearLog ()
 清空所有日志
 
bool deleteLogByTime (const std::string &date1="1", const std::string &date2="2")
 删除指定时间区间内的日志
 
 ~LogFile ()
 析构函数 写完日志 关闭消费者线程
 

Additional Inherited Members

- Protected Member Functions inherited from stt::file::File
bool openFile (const std::string &fileName, const bool &create=true, const int &multiple=0, const size_t &size=0, const mode_t &mode=0666)
 打开文件
 
bool closeFile (const bool &del=false)
 关闭已打开了的文件
 
 ~File ()
 析构函数
 
bool isOpen ()
 判断对象是否打开了文件
 
bool isBinary ()
 判断对象是否以二进制模式打开文件
 
std::string getFileName ()
 获取打开的文件名字
 
uint64_t getFileLine ()
 获取打开的文件的行数
 
size_t getFileSize ()
 获取二进制打开的文件的大小
 
size_t getSize1 ()
 获取二进制打开的文件在内存中的大小
 
bool lockMemory ()
 把数据从磁盘读入内存
 
bool unlockMemory (const bool &rec=false)
 把数据从内存写入磁盘
 
int findC (const std::string &targetString, const int linePos=1)
 查找行
 
bool appendLineC (const std::string &data, const int &linePos=0)
 插入行
 
bool deleteLineC (const int &linePos=0)
 删除行
 
bool deleteAllC ()
 删除全部
 
bool chgLineC (const std::string &data, const int &linePos=0)
 修改行
 
bool readLineC (std::string &data, const int linePos)
 读取单行
 
std::string & readC (std::string &data, const int &linePos, const int &num)
 读取行
 
std::string & readAllC (std::string &data)
 读取全部
 
bool readC (char *data, const size_t &pos, const size_t &size)
 读取数据块
 
bool writeC (const char *data, const size_t &pos, const size_t &size)
 写数据块
 
bool formatC ()
 格式化数据
 
int find (const std::string &targetString, const int linePos=1)
 查找行
 
bool appendLine (const std::string &data, const int &linePos=0)
 插入行
 
bool deleteLine (const int &linePos=0)
 删除行
 
bool deleteAll ()
 删除全部
 
bool chgLine (const std::string &data, const int &linePos=0)
 修改行
 
bool readLine (std::string &data, const int linePos)
 读取单行
 
std::string & read (std::string &data, const int &linePos, const int &num)
 读取行
 
std::string & readAll (std::string &data)
 读取全部
 
bool read (char *data, const size_t &pos, const size_t &size)
 读取数据块
 
bool write (const char *data, const size_t &pos, const size_t &size)
 写数据块
 
void format ()
 格式化数据
 
- Protected Attributes inherited from stt::file::File
std::mutex che
 
- Static Protected Attributes inherited from stt::file::File
static std::mutex l1
 
static std::unordered_map< std::string, FileThreadLockfl2
 

Detailed Description

日志文件操作类

Note
此类的读写日志是线程安全的,因为继承了File类
异步日志 会单独开一个线程进行写入操作,可以多个线程同时操作日志

Constructor & Destructor Documentation

◆ LogFile()

stt::file::LogFile::LogFile ( const size_t & logQueue_cap = 8192)
inline
       @brief 构造函数,初始化消费者线程
       @param logQueue_cap 异步日志队列容量(必须为 2 的幂)。

所有线程产生的日志会先进入该无锁队列,再由独立 logger 线程批量写入文件。 日志系统不在主业务热路径上,允许在过载时丢弃,以保护核心服务性能。

选型原则: logQueue_cap >= 峰值日志速率 × logger 最坏暂停时间

建议值(经验):

  • 默认:8192 (~8k)
  • 高频日志:16384 (~16k)

当队列已满时:日志将丢弃,框架不会阻塞调用线程。

◆ ~LogFile()

stt::file::LogFile::~LogFile ( )

析构函数 写完日志 关闭消费者线程

Member Function Documentation

◆ clearLog()

bool stt::file::LogFile::clearLog ( )

清空所有日志

Returns
true:写入成功 false:写入失败

◆ closeFile()

bool stt::file::LogFile::closeFile ( const bool & del = false)

关闭对象打开的日志文件

Parameters
deltrue:关闭并且删除日志文件 false:只关闭不删除日志文件 (默认为false 即只关闭不删除日志文件)
Returns
true:关闭成功 false:关闭失败

◆ deleteLogByTime()

bool stt::file::LogFile::deleteLogByTime ( const std::string & date1 = "1",
const std::string & date2 = "2" )

删除指定时间区间内的日志

Parameters
date1时间区间的第一个参数 (默认缺省为无限小)
date2时间区间的第二个参数 (默认缺省为无限大)
Note
区间为[date1,date2)
Returns
true:删除成功 false:删除失败

◆ getDroppedLogCount()

uint64_t stt::file::LogFile::getDroppedLogCount ( ) const
inlinenoexcept

返回日志队列满时累计丢弃的日志条数。

Returns
LogFile 构造以来的累计丢弃数;线程安全且无锁。

◆ getFileName()

std::string stt::file::LogFile::getFileName ( )
inline

获取对象打开的文件名

Returns
返回对象打开的文件名

◆ isOpen()

bool stt::file::LogFile::isOpen ( )
inline

获取对象是否打开日志文件的状态

Returns
true:打开了 false:没打开

◆ openFile()

bool stt::file::LogFile::openFile ( const std::string & fileName,
const std::string & timeFormat = ISO8086A,
const std::string & contentFormat = "   " )

打开一个日志文件

Note
不存在则创建(连带目录),默认新建的目录的权限为rwx rwx r-x,默认新建的日志文件权限为rw-,rw-,r–
Parameters
fileName日志文件名(可以用绝对路径也可以用相对路径)
timeFormat日志文件中的时间格式 yyyy年 mm月 dd日 hh时 mi分 ss秒 sss毫秒 (默认格式为'yyyy-mm-ddThh:mi:ss',即 STTNet 历史本地时间文本格式(不携带时区))
contentFormat日志文件中时间和记录之间的填充格式(默认为" " 即四个空格)
Returns
true:打开成功 false:打开失败

◆ writeLog()

void stt::file::LogFile::writeLog ( const std::string & data)

写一行日志

Parameters
data需要写入的日志内容

The documentation for this class was generated from the following file: