-
Notifications
You must be signed in to change notification settings - Fork 3
/
log4cWriter.hpp
218 lines (186 loc) · 6.06 KB
/
log4cWriter.hpp
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#ifndef __LOG4C_WRITER_HPP__
#define __LOG4C_WRITER_HPP__
#include <thread>
#include <future>
#include <list>
#include "log4cManager.hpp"
#include <stl/concurrence/semaphore.hpp>
#define MAX_BUFFER_SIZE 1024*100
#define MAX_FUNC_NAME_LEN 64
#define MAX_LAYER_NAME_LEN 32
#define MAX_FILE_NAME_LEN 32
#define MAX_RECORD_NO_LEN 4
#define MAX_THREAD_ID_LEN 8
#define MAX_TYPE_LEN 8
namespace logger
{
struct Details
{
bool isCrypt;
std::string layer;
std::string type;
std::string file;
std::string func;
std::string content;
tmExtend time;
unsigned long threadId;
unsigned long lineNo;
};
class CWriter
{
public:
explicit CWriter(const std::string& name_of_module, const std::string& path_to_save)
: name_of_moudle_(name_of_module)
, path_to_save_(path_to_save)
, manager_(new logger::CManager())
, exit_task_flag_(false)
, pre_detail_no_(1)
, is_fd_open_(false)
, fd_(-1)
{
}
virtual ~CWriter()
{
exit_task_flag_.store(true);
if(thread_task_.valid()){
thread_task_.get();
}
Details detail;
while(pop(detail)){
write(detail);
}
closefile();
}
int task()
{
Details detail;
while(!exit_task_flag_.load()){
if (deque_event_.wait_for(std::chrono::milliseconds(1))){
while (pop(detail)){
write(detail);
}
closefile();
}
}
return 0;
}
bool push(const Details& detail)
{
if(!thread_task_.valid()){
thread_task_ = std::async(std::launch::async, std::bind(&CWriter::task, this));
}
std::lock_guard<std::mutex> locker(deque_lock_);
details_.push_back(detail);
deque_event_.post();
return true;
}
bool pop(Details& detail)
{
std::lock_guard<std::mutex> locker(deque_lock_);
if(details_.empty()){
return false;
}
detail = details_.front();
details_.pop_front();
return true;
}
void setFileSize(int size)
{
manager_->setFileSize(size);
}
void setExpiryDate(int month)
{
manager_->setExpiryDate(month);
}
void setRoot(const std::string& directory)
{
path_to_save_ += directory;
}
void write(const Details& detail)
{
std::lock_guard<std::mutex> locker(file_lock_);
std::string file = manager_->formatFileName(path_to_save_, \
name_of_moudle_, \
detail.time);
bool file_exists = (0 == access(file.c_str(), F_OK));
if(file_exists && !is_fd_open_){
// the file will not exist after rename
file_exists = !manager_->isRename(file);
}
if( is_fd_open_ && \
manager_->detectDateChanged(pre_time_, detail.time) ){
io::flush(fd_);
io::close(fd_);
is_fd_open_ = false;
}
if (!is_fd_open_){
#ifdef __os_windows__
fd_ = io::open(file.c_str(), _O_APPEND | _O_CREAT | _O_BINARY | _O_RDWR, _S_IREAD | _S_IWRITE);
#else
fd_ = io::open(file.c_str(), O_APPEND | O_CREAT | O_RDWR/*|O_SYNC*/, S_IREAD | S_IWRITE);
#endif
if (-1 == fd_){
return;
}
}
is_fd_open_ = true;
std::unique_ptr<char[]> buffer(new char[MAX_BUFFER_SIZE]());
int bufferLen = 0;
pre_detail_no_ = 1;
if(!file_exists){
memcpy(&buffer[0], &pre_detail_no_, MAX_RECORD_NO_LEN);
bufferLen += MAX_RECORD_NO_LEN;
}else{
io::lseek(fd_, -MAX_RECORD_NO_LEN, SEEK_END);
io::read(fd_, &pre_detail_no_, MAX_RECORD_NO_LEN);
io::lseek(fd_, 0, SEEK_END);
}
auto _append_buffer_func = [&](void* param, int len, int max)->void{
memcpy(&buffer[bufferLen], param, len);
bufferLen += max;
};
pre_time_ = detail.time;
_append_buffer_func((void*)detail.layer.c_str(), detail.layer.size(), MAX_LAYER_NAME_LEN);
_append_buffer_func((void*)&detail.time, sizeof(tmExtend), sizeof(tmExtend));
_append_buffer_func((void*)detail.type.c_str(), detail.type.size(), MAX_TYPE_LEN);
std::string name = file::name(detail.file);
_append_buffer_func((void*)name.c_str(), name.size(), MAX_FILE_NAME_LEN);
_append_buffer_func((void*)&detail.lineNo, MAX_RECORD_NO_LEN, MAX_RECORD_NO_LEN);
_append_buffer_func((void*)detail.func.c_str(), detail.func.size(), MAX_FUNC_NAME_LEN);
_append_buffer_func(detail.isCrypt ? (void*)"E" : (void*)"D", 1, 1);
_append_buffer_func((void*)&detail.threadId, MAX_THREAD_ID_LEN, MAX_THREAD_ID_LEN);
int leftLen = MAX_BUFFER_SIZE - bufferLen - MAX_RECORD_NO_LEN;
int contentLen = detail.content.size()<leftLen?detail.content.size():leftLen;
_append_buffer_func((void*)&contentLen, MAX_RECORD_NO_LEN, MAX_RECORD_NO_LEN);
_append_buffer_func((void*)detail.content.c_str(), contentLen, contentLen);
pre_detail_no_++;
_append_buffer_func((void*)&pre_detail_no_, MAX_RECORD_NO_LEN, MAX_RECORD_NO_LEN);
io::write(fd_, &buffer[0], bufferLen);
return;
}
private:
inline void closefile(){
std::lock_guard<std::mutex> locker(file_lock_);
if (is_fd_open_){
io::flush(fd_);
io::close(fd_);
is_fd_open_ = false;
}
}
private:
std::shared_ptr<logger::CManager> manager_;
std::atomic<bool> exit_task_flag_;
std::future<int> thread_task_;
stl::concurrence::CSemaphore deque_event_;
std::mutex deque_lock_;
std::atomic<bool> is_fd_open_;
std::atomic<int> fd_;
std::mutex file_lock_;
tmExtend pre_time_;
int pre_detail_no_;
std::list<Details> details_;
std::string name_of_moudle_;
std::string path_to_save_;
};
}
#endif//__LOG4C_WRITER_HPP__