-
Notifications
You must be signed in to change notification settings - Fork 2
/
FileUtil.cpp
39 lines (33 loc) · 1011 Bytes
/
FileUtil.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//@author Jingyu Tong
//@email: jingyutong0806@gmail.com
#include "FileUtil.h"
AppendFile::AppendFile(std::string filename)
: fp_(fopen(filename.c_str(), "ae")) //追加,exec后关闭
{
setbuffer(fp_, buffer_, sizeof buffer_); //使用缓冲区,尽可能减少read和write调用的次数
}
AppendFile::~AppendFile() {
fclose(fp_);
}
void AppendFile::append(const char* logline, const size_t len) {
size_t n = this->write(logline, len);
size_t remain = len - n;
while(remain > 0) {
size_t x = this->write(logline + n, remain);
if(x == 0) {
int err = ferror(fp_);
if(err)
fprintf(stderr, "AppendFile::append() failed! \n");
break;
}
n += x;
remain = len - x;
}
}
//将未写的数据输出,并清空缓冲区
void AppendFile::flush() {
fflush(fp_);
}
size_t AppendFile::write(const char* logline, size_t len) {
return fwrite_unlocked(logline, 1, len, fp_); //不加锁
}