-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbcvlog.c
315 lines (281 loc) · 8.45 KB
/
bcvlog.c
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
** 2023-04-01
**
******************************************************************************
**
*/
#include "bcv_int.h"
#include "blockcachevfs.h"
#include <assert.h>
#include <string.h>
/*
** Each log entry is stored in memory as an instance of the following
** structure.
*/
typedef struct BcvLogHttp BcvLogHttp;
struct BcvLogHttp {
i64 iLogId; /* Logging id */
i64 iRequestTime; /* Request timestamp */
i64 iReplyTime; /* Reply timestamp */
int eMethod; /* SQLITE_BCV_METHOD_ constant */
int nRetry; /* # attempts before this one */
char *zFile; /* File tail of of logged URI */
char *zClientId; /* Client ID */
char *zMsg; /* Log message */
int httpcode; /* HTTP reply code from server */
BcvLogHttp *pNext;
BcvLogHttp *pPrev;
};
/*
** Logging object used by daemon and local VFS.
*/
struct BcvLog {
i64 iTimeout;
i64 iMaxEntry;
i64 iNextLogId;
sqlite3_mutex *mutex;
int nEntry; /* Number of entries in pFirst/pLast list */
BcvLogHttp *pFirst;
BcvLogHttp *pLast;
};
/*
** Defaults for the two sqlite3_bcv_config() parameters.
*/
#define BCVLOG_DEFAULT_TIMEOUT 3600
#define BCVLOG_DEFAULT_MAXENTRY -1
/*
** Allocate and return pointer to a new logging object.
*/
BcvLog *bcvLogNew(){
BcvLog *p = sqlite3_malloc(sizeof(BcvLog));
if( p ){
memset(p, 0, sizeof(BcvLog));
p->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_RECURSIVE);
if( p->mutex==0 && sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MAIN) ){
/* OOM in sqlite3_mutex_alloc() */
sqlite3_free(p);
p = 0;
}else{
p->iTimeout = BCVLOG_DEFAULT_TIMEOUT;
p->iMaxEntry = BCVLOG_DEFAULT_MAXENTRY;
}
}
return p;
}
/*
** Delete a logging object.
*/
void bcvLogDelete(BcvLog *pLog){
if( pLog ){
while( pLog->pFirst ){
BcvLogHttp *pNext = pLog->pFirst->pNext;
sqlite3_free(pLog->pFirst);
pLog->pFirst = pNext;
}
sqlite3_mutex_free(pLog->mutex);
sqlite3_free(pLog);
}
}
/*
** Return the number of bytes in the URI passed as the only argument,
** up to but not including the first '?' character.
*/
static int bcvLogUriLength(const char *zUri){
int nByte;
for(nByte=0; zUri[nByte] && zUri[nByte]!='?'; nByte++);
return nByte;
}
/*
** The logging-object mutex must be held to call this function. It
** frees any log entries that should be freed according to the configured
** values of the SQLITE_BCV_HTTPLOG_TIMEOUT and SQLITE_BCV_HTTPLOG_NENTRY
** parameters.
*/
static void bcvLogEnforceLimits(BcvLog *pLog){
if( pLog->iTimeout>0 || pLog->iMaxEntry>0 ){
i64 iLimit = sqlite_timestamp() - (pLog->iTimeout*1000);
while( (pLog->pFirst && pLog->pFirst->iRequestTime<iLimit)
|| (pLog->iMaxEntry>=0 && pLog->nEntry>pLog->iMaxEntry)
){
BcvLogHttp *pDel = pLog->pFirst;
pLog->pFirst = pDel->pNext;
sqlite3_free(pDel);
pLog->nEntry--;
}
if( pLog->pFirst==0 ) pLog->pLast = 0;
}
}
/*
** Log an HTTP request.
*/
int bcvLogRequest(
BcvLog *pLog,
const char *zClientId, /* Id of client that made this request */
const char *zLogMsg, /* Log message */
int eMethod, /* SQLITE_BCV_METHOD_* constant */
int nRetry, /* 0 for first attempt, 1 for second... */
const char *zUri, /* Logging URI of request */
i64 *piLogId /* OUT: Id to pass to bcvLogReply() */
){
int rc = SQLITE_OK;
if( pLog ){
BcvLogHttp *pNew = 0;
int nByte = sizeof(BcvLogHttp);
int nUri = bcvLogUriLength(zUri);
int nClient = bcvStrlen(zClientId);
int nLogMsg = bcvStrlen(zLogMsg);
nByte += nUri+1 + nClient+1 + nLogMsg+1;
pNew = bcvMallocRc(&rc, nByte);
if( pNew ){
i64 iRequestTime = 0;
pNew->eMethod = eMethod;
pNew->nRetry = nRetry;
pNew->zFile = (char*)&pNew[1];
if( nUri ) memcpy(pNew->zFile, zUri, nUri);
pNew->zClientId = &pNew->zFile[nUri+1];
if( nClient ) memcpy(pNew->zClientId, zClientId, nClient);
pNew->zMsg = &pNew->zClientId[nClient+1];
if( nLogMsg ) memcpy(pNew->zMsg, zLogMsg, nLogMsg);
iRequestTime = pNew->iRequestTime = sqlite_timestamp();
sqlite3_mutex_enter(pLog->mutex);
*piLogId = pNew->iLogId = pLog->iNextLogId++;
pNew->pPrev = pLog->pLast;
if( pLog->pLast ){
assert( pLog->pLast->pNext==0 );
pLog->pLast->pNext = pNew;
}else{
assert( pLog->pFirst==0 );
pLog->pFirst = pNew;
}
pLog->pLast = pNew;
pLog->nEntry++;
bcvLogEnforceLimits(pLog);
sqlite3_mutex_leave(pLog->mutex);
}
}
return rc;
}
/*
** Log an HTTP reply.
*/
int bcvLogReply(
BcvLog *pLog,
i64 iLogId, /* Id from earlier bcvLogRequest() call */
int httpcode, /* HTTP reponse code */
i64 *piMs /* OUT: Total ms for this request */
){
int rc = SQLITE_OK;
if( pLog ){
BcvLogHttp *pHttp = 0;
i64 iReplyTime = sqlite_timestamp();
sqlite3_mutex_enter(pLog->mutex);
for(pHttp=pLog->pLast; pHttp; pHttp=pHttp->pPrev){
if( pHttp->iLogId==iLogId ) break;
}
if( pHttp ){
pHttp->iReplyTime = iReplyTime;
pHttp->httpcode = httpcode;
*piMs = pHttp->iReplyTime - pHttp->iRequestTime;
}
sqlite3_mutex_leave(pLog->mutex);
}
return rc;
}
/*
** Return a string corresponding to the SQLITE_BCV_METHOD_XXX constant
** passed as the only argment. e.g. SQLITE_BCV_METHOD_GET -> "GET".
*/
static const char *bcvLogMethodString(int eMethod){
const char *azRet[] = {
0, "GET", "PUT", "DELETE", "HEAD"
};
assert( SQLITE_BCV_METHOD_GET==1 );
assert( SQLITE_BCV_METHOD_PUT==2 );
assert( SQLITE_BCV_METHOD_DELETE==3 );
assert( SQLITE_BCV_METHOD_HEAD==4 );
assert( eMethod>=1 && eMethod<sizeof(azRet)/sizeof(azRet[0]) );
return azRet[eMethod];
}
/*
** Populate the buffer passed as the second argument with a blob containing
** the current http log contents. Return SQLITE_OK if successful, or an
** SQLite error code (e.g. SQLITE_NOMEM) otherwise.
*/
int bcvLogGetData(BcvLog *pLog, BcvBuffer *pBuf){
int rc = SQLITE_OK;
if( pLog ){
BcvLogHttp *p;
sqlite3_mutex_enter(pLog->mutex);
bcvLogEnforceLimits(pLog);
for(p=pLog->pFirst; p; p=p->pNext){
bcvBufferAppendU64(&rc, pBuf, p->iLogId);
bcvBufferAppendU64(&rc, pBuf, p->iRequestTime);
bcvBufferAppendU64(&rc, pBuf, p->iReplyTime);
bcvBufferMsgString(&rc, pBuf, bcvLogMethodString(p->eMethod));
bcvBufferMsgString(&rc, pBuf, p->zClientId);
bcvBufferMsgString(&rc, pBuf, p->zMsg);
bcvBufferMsgString(&rc, pBuf, p->zFile);
bcvBufferAppendU32(&rc, pBuf, p->httpcode);
}
sqlite3_mutex_leave(pLog->mutex);
}
return rc;
}
/*
** Parameter op must be either SQLITE_BCV_HTTPLOG_TIMEOUT or
** SQLITE_BCV_HTTPLOG_NENTRY. This function sets the value of the corresponding
** parameter to iVal.
*/
void bcvLogConfig(BcvLog *pLog, int op, i64 iVal){
assert(
op==SQLITE_BCV_HTTPLOG_TIMEOUT
|| op==SQLITE_BCV_HTTPLOG_NENTRY
);
if( pLog ){
sqlite3_mutex_enter(pLog->mutex);
if( op==SQLITE_BCV_HTTPLOG_TIMEOUT ){
pLog->iTimeout = iVal;
}else{
pLog->iMaxEntry = iVal;
}
bcvLogEnforceLimits(pLog);
sqlite3_mutex_leave(pLog->mutex);
}
}
/*
** Parameter iTime is a julian-day value multiplied by 86400000, as returned
** by an SQLite VFS xCurrentTimeInt64() method. This function formats the
** time value as an ISO-8601 time string and writes the results into buffer
** zBuf. The caller is responsible for ensuring that zBuf is large enough.
** Example time string:
**
** "2015-09-20 17:57:22.432"
*/
void bcvTimeToString(i64 iTime, char *zBuf){
int iDay, iMonth, iYear;
int iSec, iMin, iHour;
int Z, A, B, C, D, E, X1;
int s;
Z = (int)((iTime + 43200000)/86400000);
A = (int)((Z - 1867216.25)/36524.25);
A = Z + 1 + A - (A/4);
B = A + 1524;
C = (int)((B - 122.1)/365.25);
D = (36525*(C&32767))/100;
E = (int)((B-D)/30.6001);
X1 = (int)(30.6001*E);
iDay = B - D - X1;
iMonth = E<14 ? E-1 : E-13;
iYear = iMonth>2 ? C - 4716 : C - 4715;
s = (int)((iTime + 43200000) % 86400000);
iSec = s/1000.0;
s = (int)iSec;
iSec -= s;
iHour = s/3600;
s -= iHour*3600;
iMin = s/60;
iSec += s - iMin*60;
sqlite3_snprintf(64, zBuf, "%04d-%02d-%02d %02d:%02d:%02d.%03d",
iYear, iMonth, iDay, iHour, iMin, iSec, (iTime % 1000)
);
}