blob: 650be581eccb2d74e1de84baba7588768aa992d3 (
plain) (
blame)
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
|
#include "file.h"
#include "record.h"
#include <util/system/file.h>
#include <util/system/rwlock.h>
#include <util/generic/string.h>
/*
* file log
*/
class TFileLogBackend::TImpl {
public:
inline TImpl(const TString& path)
: File_(OpenFile(path))
{
}
inline void WriteData(const TLogRecord& rec) {
//many writes are thread-safe
TReadGuard guard(Lock_);
File_.Write(rec.Data, rec.Len);
}
inline void ReopenLog() {
//but log rotate not thread-safe
TWriteGuard guard(Lock_);
File_.LinkTo(OpenFile(File_.GetName()));
}
private:
static inline TFile OpenFile(const TString& path) {
return TFile(path, OpenAlways | WrOnly | ForAppend | Seq | NoReuse);
}
private:
TRWMutex Lock_;
TFile File_;
};
TFileLogBackend::TFileLogBackend(const TString& path)
: Impl_(new TImpl(path))
{
}
TFileLogBackend::~TFileLogBackend() {
}
void TFileLogBackend::WriteData(const TLogRecord& rec) {
Impl_->WriteData(rec);
}
void TFileLogBackend::ReopenLog() {
TAtomicSharedPtr<TImpl> copy = Impl_;
if (copy) {
copy->ReopenLog();
}
}
|