-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathRlog.hpp
293 lines (290 loc) · 8.25 KB
/
Rlog.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
#ifndef RLOGHPP
#define RLOGHPP
#include <errno.h>
#include <stdlib.h>
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
#include "Maindef.hpp"
#include "Common.hpp"
#include "Conf.hpp"
using namespace std;
//-------------------------------------------------------------------------------------------------
//sec_usec.rlog
//-------------------------------------------------------------------------------------------------
class SenderStat
{
public:
string current_filename;
off_t offset;
public:
Conf conf;
string filename;
FILE* fp;
public:
SenderStat(const Conf& c):
offset(0),conf(c),fp(NULL)
{
filename=c.stat_file_path+STAT_FILE_NAME;
}
~SenderStat()
{
if(fp!=NULL)
fclose(fp);
}
void UpdateConf(const Conf& c)
{conf=c;}
private:
SenderStat(const SenderStat& ss)
{}
public:
void Reset()//move to end of rlog
{
conf.Display();
vector<FNSortHelper> helpers;
if(ListFileFromPath(conf.rlog_path,helpers,RLOG_SURFIX)==false)
return;
if(helpers.empty())
return;
sort(helpers.begin(),helpers.end());
const string& fn=helpers.back().filefullname;
off_t fl=GetFileSize(fn);
Save(fn,fl);
if(fp!=NULL)
{
fclose(fp);
fp=NULL;
}
kobe_printf("%s\tsenderstat reset %s %lu\n",GetCurrentTime().c_str(),fn.c_str(),fl);
}
//---------------------------------------------
bool Load()
{
string content;
if(FileGetContent(filename,content)==false)
return false;
vector<string> items;
SplitString(content,items,"\t");
if(items.size()!=2)
return false;
current_filename=items[0];
offset=atol(items[1].c_str());
return true;
}
//---------------------------------------------
bool Save(const string& cf,ULL o)
{
if(cf.empty()==false&¤t_filename.empty()==false&&
cf!=current_filename)
{
kobe_printf("%s\t%s done,unlinked\n",GetCurrentTime().c_str(),
current_filename.c_str());
unlink(current_filename.c_str());//read rotate
}
current_filename=cf;
offset=o;
if(fp==NULL)
fp=fopen(filename.c_str(),"w");
if(fp==NULL)
return false;
fseek(fp,0,SEEK_SET);
fprintf(fp,"%s\t%lu",current_filename.c_str(),offset);
fflush(fp);
return true;
}
};//end class SenderStat
//-------------------------------------------------------------------------------------------------
class RlogItem
{
public:
string times;
int event_type;
string filename;
public:
RlogItem():event_type(0)
{}
};//end class RlogItem
//-------------------------------------------------------------------------------------------------
class Rlog
{
public:
Conf conf;
FILE* fp;
string current_filename;
off_t current_file_readed_offset;
off_t current_file_wrotten_offset;
time_t last_read_time;
int safe_mode;//danger rotate, cause dir not sync
public:
Rlog(const Conf& c):conf(c),fp(NULL),
current_file_readed_offset(0),current_file_wrotten_offset(0),
last_read_time(time(NULL)),safe_mode(SYNC_SAFE)
{}
void UpdateConf(const Conf& c)
{conf=c;}
private:
Rlog(const Rlog& r){}
private:
//---------------------------------------------
FILE* FindProperFileToRead(const SenderStat& ss)
{
if(ss.current_filename.empty()==false&&ss.offset<GetFileSize(ss.current_filename))
{
current_filename=ss.current_filename;
fp=fopen(ss.current_filename.c_str(),"r");
fseek(fp,ss.offset,SEEK_SET);
kobe_printf("%s\tcontinue read from %s:%lu\n",GetCurrentTime().c_str(),
ss.current_filename.c_str(),ss.offset);
return fp;
}
//new file
vector<FNSortHelper> helpers;
if(ListFileFromPath(conf.rlog_path,helpers,RLOG_SURFIX)==false)
return NULL;
if(helpers.empty())
return NULL;
sort(helpers.begin(),helpers.end());
FNSortHelper fnhelper;
fnhelper.filefullname=ss.current_filename;
fnhelper.FromString(ExtractFilename(ss.current_filename));
unsigned int i=0;
for(;i<helpers.size();i++)
{
if(helpers[i]>fnhelper)
break;
}
if(i>=helpers.size())
return NULL;
current_filename=helpers[i].filefullname;
fp=fopen(current_filename.c_str(),"r");
kobe_printf("%s\tfirst read from %s:0\n",GetCurrentTime().c_str(),
current_filename.c_str());
return fp;
}
//---------------------------------------------
FILE* FindProperFileToWrite()
{
vector<FNSortHelper> helpers;
if(ListFileFromPath(conf.rlog_path,helpers,RLOG_SURFIX)==false)
return NULL;
sort(helpers.begin(),helpers.end());
if(helpers.empty()||
(uint64_t)(GetFileSize(helpers.back().filefullname))>=conf.rlog_file_max_size)
{
struct timeval tv;
memset(&tv, 0, sizeof(tv));
gettimeofday(&tv,NULL);
string s1=Timet2String(tv.tv_sec);
char s2[8];
snprintf(s2,8,"%07u",(unsigned int)(tv.tv_usec));
string filename=conf.rlog_path+s1+"_"+string(s2)+RLOG_SURFIX;
fp=fopen(filename.c_str(),"a");
if(fp==NULL)
kobe_printf("%s\tERROR: failed to open %s,%d\n",
GetCurrentTime().c_str(),filename.c_str(),errno);
else
{
current_filename=filename;
kobe_printf("%s\tfirst write to %s\n",GetCurrentTime().c_str(),
current_filename.c_str());
if(helpers.size()+1>=(unsigned int)(conf.rlog_max_file_number))
{
unlink(helpers.front().filefullname.c_str());//rotate
kobe_printf("%s\tWARNING: rlog so full that unlink %s to rotate, maybe some update lost!\n",
GetCurrentTime().c_str(),helpers.front().filefullname.c_str());
safe_mode=SYNC_DANGER_RLOG_OVERFLOW;
}
}
return fp;
}
current_filename=helpers.back().filefullname;
fp=fopen(current_filename.c_str(),"a");
if(fp==NULL)
kobe_printf("%s\tERROR: failed to open %s,%d\n",
GetCurrentTime().c_str(),current_filename.c_str(),errno);
else
kobe_printf("%s\tcontinue write to %s\n",GetCurrentTime().c_str(),
current_filename.c_str());
return fp;
}
//---------------------------------------------
public:
bool Read(const SenderStat& ss,vector<RlogItem>& items,int& err)
{
if(fp==NULL)
fp=FindProperFileToRead(ss);
if(fp==NULL)
{
err=-1;
return false;
}
last_read_time=time(NULL);
int i=0;
static char buffer[4096];
while(i++<conf.rlog_reader_batch_item_number)
{
char* s=fgets(buffer,4096,fp);
if(s==NULL)
{
if(feof(fp))
{
current_file_readed_offset=ftell(fp);
fclose(fp);
fp=NULL;
return true;
}
err=errno;
return false;
}
buffer[4095]=0;
vector<string> tokens;
SplitString(buffer,tokens,"\t\n");
if(tokens.size()!=3)
{
kobe_printf("%s\tERROR: wrong format %s\n",GetCurrentTime().c_str(),buffer);
continue;
}
RlogItem ri;
ri.times=tokens[0];
ri.event_type=String2EventType(tokens[1]);
ri.filename=tokens[2];
items.push_back(ri);
if(time(NULL)-last_read_time>RLOG_BATCH_PROCESS_TIMEOUT)//force batch process return
break;
}//end while
current_file_readed_offset=ftell(fp);
return true;
}
//---------------------------------------------
bool Write(int event_type,const string& filename,int& err)
{
if(fp==NULL)
fp=FindProperFileToWrite();
if(fp==NULL)
{
err=-1;
kobe_printf("no file to write\n");
return false;
}
int rv=fprintf(fp,"%s\t%s\t%s\n",GetCurrentTime().c_str(),
EventType2String(event_type),filename.c_str());
if(rv<0)
{
err=errno;
return false;
}
fflush(fp);
current_file_wrotten_offset=ftell(fp);
if(GetFileSize(fp)>=(long)(conf.rlog_file_max_size))
{
fclose(fp);
fp=NULL;
kobe_printf("%s\tclosed full file %s\n",
GetCurrentTime().c_str(),current_filename.c_str());
}
return true;
}
};//end class Rlog
//-------------------------------------------------------------------------------------------------
#endif