forked from linyacool/WebServer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogFile.cpp
More file actions
executable file
·40 lines (33 loc) · 855 Bytes
/
Copy pathLogFile.cpp
File metadata and controls
executable file
·40 lines (33 loc) · 855 Bytes
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
// @Author Lin Ya
// @Email [email protected]
#include "LogFile.h"
#include <assert.h>
#include <stdio.h>
#include <time.h>
#include "FileUtil.h"
using namespace std;
LogFile::LogFile(const string& basename, int flushEveryN)
: basename_(basename),
flushEveryN_(flushEveryN),
count_(0),
mutex_(new MutexLock) {
// assert(basename.find('/') >= 0);
file_.reset(new AppendFile(basename));
}
LogFile::~LogFile() {}
void LogFile::append(const char* logline, int len) {
MutexLockGuard lock(*mutex_);
append_unlocked(logline, len);
}
void LogFile::flush() {
MutexLockGuard lock(*mutex_);
file_->flush();
}
void LogFile::append_unlocked(const char* logline, int len) {
file_->append(logline, len);
++count_;
if (count_ >= flushEveryN_) {
count_ = 0;
file_->flush();
}
}