forked from chenshuo/recipes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogFile.cc
More file actions
183 lines (160 loc) · 3.35 KB
/
LogFile.cc
File metadata and controls
183 lines (160 loc) · 3.35 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include "LogFile.h"
#include <assert.h>
#include <stdio.h>
#include <time.h>
using namespace muduo;
// not thread safe
class LogFile::File : boost::noncopyable
{
public:
explicit File(const string& filename)
: fp_(::fopen(filename.data(), "ae")), // FIXME: check
writtenBytes_(0)
{
::setbuffer(fp_, buffer_, sizeof buffer_);
}
~File()
{
::fclose(fp_);
}
void append(const char* logline, const size_t len)
{
size_t n = write(logline, len);
size_t remain = len - n;
while (remain > 0)
{
size_t x = write(logline + n, remain);
if (x == 0)
{
int err = ferror(fp_);
if (err)
{
char buf[128];
strerror_r(err, buf, sizeof buf); // FIXME: strerror_l
fprintf(stderr, "LogFile::File::append() failed %s\n", buf);
}
break;
}
n += x;
remain = len - n; // remain -= x
}
writtenBytes_ += len;
}
void flush()
{
::fflush(fp_);
}
size_t writtenBytes() const { return writtenBytes_; }
private:
size_t write(const char* logline, size_t len)
{
#undef fwrite_unlocked
return ::fwrite_unlocked(logline, 1, len, fp_);
}
FILE* fp_;
char buffer_[64*1024];
size_t writtenBytes_;
};
LogFile::LogFile(const string& basename,
size_t rollSize,
bool threadSafe,
int flushInterval)
: basename_(basename),
rollSize_(rollSize),
flushInterval_(flushInterval),
count_(0),
mutex_(threadSafe ? new MutexLock : NULL),
startOfPeriod_(0),
lastRoll_(0),
lastFlush_(0)
{
assert(basename.find('/') == string::npos);
rollFile();
}
LogFile::~LogFile()
{
}
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)
{
file_->append(logline, len);
if (file_->writtenBytes() > rollSize_)
{
rollFile();
}
else
{
if (count_ > kCheckTimeRoll_)
{
count_ = 0;
time_t now = ::time(NULL);
time_t thisPeriod_ = now / kRollPerSeconds_ * kRollPerSeconds_;
if (thisPeriod_ != startOfPeriod_)
{
rollFile();
}
else if (now - lastFlush_ > flushInterval_)
{
lastFlush_ = now;
file_->flush();
}
}
else
{
++count_;
}
}
}
void LogFile::rollFile()
{
time_t now = 0;
string filename = getLogFileName(basename_, &now);
time_t start = now / kRollPerSeconds_ * kRollPerSeconds_;
if (now > lastRoll_)
{
lastRoll_ = now;
lastFlush_ = now;
startOfPeriod_ = start;
file_.reset(new File(filename));
}
}
string LogFile::getLogFileName(const string& basename, time_t* now)
{
string filename;
filename.reserve(basename.size() + 32);
filename = basename;
char timebuf[32];
char pidbuf[32];
struct tm tm;
*now = time(NULL);
gmtime_r(now, &tm); // FIXME: localtime_r ?
strftime(timebuf, sizeof timebuf, ".%Y%m%d-%H%M%S", &tm);
filename += timebuf;
snprintf(pidbuf, sizeof pidbuf, ".%d", ::getpid()); // FIXME: ProcessInfo::pid();
filename += pidbuf;
filename += ".log";
return filename;
}