1 |
/* |
---|
2 |
* Copyright (c) 2005-2008, Message Systems, Inc. |
---|
3 |
* All rights reserved. |
---|
4 |
* |
---|
5 |
* Redistribution and use in source and binary forms, with or without |
---|
6 |
* modification, are permitted provided that the following conditions are |
---|
7 |
* met: |
---|
8 |
* |
---|
9 |
* * Redistributions of source code must retain the above copyright |
---|
10 |
* notice, this list of conditions and the following disclaimer. |
---|
11 |
* * Redistributions in binary form must reproduce the above |
---|
12 |
* copyright notice, this list of conditions and the following |
---|
13 |
* disclaimer in the documentation and/or other materials provided |
---|
14 |
* with the distribution. |
---|
15 |
* * Neither the name Message Systems, Inc. nor the names |
---|
16 |
* of its contributors may be used to endorse or promote products |
---|
17 |
* derived from this software without specific prior written |
---|
18 |
* permission. |
---|
19 |
* |
---|
20 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
---|
21 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
---|
22 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
---|
23 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
---|
24 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
---|
25 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
---|
26 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
---|
27 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
---|
28 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
29 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
---|
30 |
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
31 |
*/ |
---|
32 |
|
---|
33 |
#include "php.h" |
---|
34 |
#include "php_ini.h" |
---|
35 |
#include "php_jlog.h" |
---|
36 |
#include <fcntl.h> |
---|
37 |
|
---|
38 |
#if HAVE_JLOG |
---|
39 |
|
---|
40 |
typedef struct { |
---|
41 |
zend_object zo; |
---|
42 |
jlog_ctx *ctx; |
---|
43 |
char *path; |
---|
44 |
jlog_id start; |
---|
45 |
jlog_id last; |
---|
46 |
jlog_id end; |
---|
47 |
int auto_checkpoint; |
---|
48 |
} jlog_obj; |
---|
49 |
|
---|
50 |
static zend_class_entry * Jlog_ce_ptr = NULL; |
---|
51 |
static zend_class_entry * Jlog_Writer_ce_ptr = NULL; |
---|
52 |
static zend_class_entry * Jlog_Reader_ce_ptr = NULL; |
---|
53 |
static zend_object_handlers jlog_object_handlers; |
---|
54 |
|
---|
55 |
|
---|
56 |
static void FREE_JLOG_OBJ(jlog_obj *intern) |
---|
57 |
{ |
---|
58 |
if(intern) { |
---|
59 |
if(intern->ctx) { |
---|
60 |
jlog_ctx_close(intern->ctx); |
---|
61 |
intern->ctx = NULL; |
---|
62 |
} |
---|
63 |
if(intern->path) { |
---|
64 |
free(intern->path); |
---|
65 |
} |
---|
66 |
} |
---|
67 |
} |
---|
68 |
|
---|
69 |
static void jlog_obj_dtor(void *object TSRMLS_DC) |
---|
70 |
{ |
---|
71 |
zend_object *zo = (zend_object *) object; |
---|
72 |
jlog_obj *intern = (jlog_obj *) zo; |
---|
73 |
FREE_JLOG_OBJ(intern); |
---|
74 |
zend_object_std_dtor(&intern->zo TSRMLS_CC); |
---|
75 |
efree(intern); |
---|
76 |
} |
---|
77 |
|
---|
78 |
zend_object_value jlog_objects_new(zend_class_entry *class_type TSRMLS_DC) |
---|
79 |
{ |
---|
80 |
zend_object_value retval; |
---|
81 |
jlog_obj *intern; |
---|
82 |
|
---|
83 |
intern = emalloc(sizeof(*intern)); |
---|
84 |
memset(intern, 0, sizeof(*intern)); |
---|
85 |
|
---|
86 |
zend_object_std_init(&intern->zo, class_type TSRMLS_CC); |
---|
87 |
retval.handle = zend_objects_store_put(intern, (zend_objects_store_dtor_t) zend_objects_destroy_object, jlog_obj_dtor, NULL TSRMLS_CC); |
---|
88 |
retval.handlers = &jlog_object_handlers; |
---|
89 |
return retval; |
---|
90 |
} |
---|
91 |
|
---|
92 |
/* {{{ Class definitions */ |
---|
93 |
|
---|
94 |
/* {{{ Class Jlog */ |
---|
95 |
|
---|
96 |
|
---|
97 |
/* {{{ Methods */ |
---|
98 |
|
---|
99 |
|
---|
100 |
/* {{{ proto object Jlog __construct(string path [, array options]) |
---|
101 |
*/ |
---|
102 |
PHP_METHOD(Jlog, __construct) |
---|
103 |
{ |
---|
104 |
zend_class_entry * _this_ce; |
---|
105 |
zval * _this_zval; |
---|
106 |
const char * path = NULL; |
---|
107 |
int path_len = 0; |
---|
108 |
int options = 0; |
---|
109 |
int size = 0; |
---|
110 |
jlog_obj *jo; |
---|
111 |
|
---|
112 |
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|ll", &path, &path_len, &options, &size) == FAILURE) { |
---|
113 |
return; |
---|
114 |
} |
---|
115 |
|
---|
116 |
_this_zval = getThis(); |
---|
117 |
_this_ce = Z_OBJCE_P(_this_zval); |
---|
118 |
jo = (jlog_obj *) zend_object_store_get_object(_this_zval TSRMLS_CC); |
---|
119 |
|
---|
120 |
jo->ctx = jlog_new(path); |
---|
121 |
jo->path = strdup(path); |
---|
122 |
if(options & O_CREAT) { |
---|
123 |
if(size) { |
---|
124 |
jlog_ctx_alter_journal_size(jo->ctx, size); |
---|
125 |
} |
---|
126 |
if(jlog_ctx_init(jo->ctx) != 0) { |
---|
127 |
if(jlog_ctx_err(jo->ctx) == JLOG_ERR_CREATE_EXISTS) { |
---|
128 |
if(options & O_EXCL) { |
---|
129 |
FREE_JLOG_OBJ(jo); |
---|
130 |
efree(jo); |
---|
131 |
php_error(E_WARNING, "file already exists: %s", path); |
---|
132 |
} |
---|
133 |
} else { |
---|
134 |
int err = jlog_ctx_err(jo->ctx); |
---|
135 |
const char *err_string = jlog_ctx_err_string(jo->ctx); |
---|
136 |
FREE_JLOG_OBJ(jo); |
---|
137 |
efree(jo); |
---|
138 |
php_error(E_WARNING, "error initializing jlog: %d %s", err, err_string); |
---|
139 |
RETURN_FALSE; |
---|
140 |
} |
---|
141 |
} |
---|
142 |
jlog_ctx_close(jo->ctx); |
---|
143 |
jo->ctx = jlog_new(path); |
---|
144 |
if(!jo->ctx) { |
---|
145 |
FREE_JLOG_OBJ(jo); |
---|
146 |
efree(jo); |
---|
147 |
php_error(E_WARNING, "jlog_new(%s) failed after successful init", path); |
---|
148 |
RETURN_FALSE; |
---|
149 |
} |
---|
150 |
} |
---|
151 |
} |
---|
152 |
/* }}} __construct */ |
---|
153 |
|
---|
154 |
|
---|
155 |
|
---|
156 |
/* {{{ proto bool add_subscriber(string subscriber [, int whence]) |
---|
157 |
*/ |
---|
158 |
PHP_METHOD(Jlog, add_subscriber) |
---|
159 |
{ |
---|
160 |
zend_class_entry * _this_ce; |
---|
161 |
zval * _this_zval = NULL; |
---|
162 |
const char * subscriber = NULL; |
---|
163 |
int subscriber_len = 0; |
---|
164 |
long whence = 0; |
---|
165 |
jlog_obj *jo; |
---|
166 |
|
---|
167 |
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os|l", &_this_zval, Jlog_ce_ptr, &subscriber, &subscriber_len, &whence) == FAILURE) { |
---|
168 |
return; |
---|
169 |
} |
---|
170 |
|
---|
171 |
_this_ce = Z_OBJCE_P(_this_zval); |
---|
172 |
jo = (jlog_obj *) zend_object_store_get_object(_this_zval TSRMLS_CC); |
---|
173 |
if(!jo || !jo->ctx || jlog_ctx_add_subscriber(jo->ctx, subscriber, whence) != 0) |
---|
174 |
{ |
---|
175 |
RETURN_FALSE; |
---|
176 |
} |
---|
177 |
RETURN_TRUE; |
---|
178 |
} |
---|
179 |
/* }}} add_subscriber */ |
---|
180 |
|
---|
181 |
|
---|
182 |
|
---|
183 |
/* {{{ proto bool remove_subscriber(string subscriber) |
---|
184 |
*/ |
---|
185 |
PHP_METHOD(Jlog, remove_subscriber) |
---|
186 |
{ |
---|
187 |
zend_class_entry * _this_ce; |
---|
188 |
zval * _this_zval = NULL; |
---|
189 |
const char * subscriber = NULL; |
---|
190 |
int subscriber_len = 0; |
---|
191 |
jlog_obj *jo; |
---|
192 |
|
---|
193 |
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os", &_this_zval, Jlog_ce_ptr, &subscriber, &subscriber_len) == FAILURE) { |
---|
194 |
return; |
---|
195 |
} |
---|
196 |
|
---|
197 |
_this_ce = Z_OBJCE_P(_this_zval); |
---|
198 |
jo = (jlog_obj *) zend_object_store_get_object(_this_zval TSRMLS_CC); |
---|
199 |
if(!jo || !jo->ctx || jlog_ctx_remove_subscriber(jo->ctx, subscriber) != 0) |
---|
200 |
{ |
---|
201 |
RETURN_FALSE; |
---|
202 |
} |
---|
203 |
RETURN_TRUE; |
---|
204 |
} |
---|
205 |
/* }}} remove_subscriber */ |
---|
206 |
|
---|
207 |
|
---|
208 |
|
---|
209 |
/* {{{ proto array list_subscribers() |
---|
210 |
*/ |
---|
211 |
PHP_METHOD(Jlog, list_subscribers) |
---|
212 |
{ |
---|
213 |
zend_class_entry * _this_ce; |
---|
214 |
zval * _this_zval = NULL; |
---|
215 |
jlog_obj *jo; |
---|
216 |
char **list; |
---|
217 |
int i; |
---|
218 |
|
---|
219 |
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &_this_zval, Jlog_ce_ptr) == FAILURE) { |
---|
220 |
return; |
---|
221 |
} |
---|
222 |
|
---|
223 |
_this_ce = Z_OBJCE_P(_this_zval); |
---|
224 |
jo = (jlog_obj *) zend_object_store_get_object(_this_zval TSRMLS_CC); |
---|
225 |
if(!jo || !jo->ctx) { |
---|
226 |
RETURN_NULL(); |
---|
227 |
} |
---|
228 |
array_init(return_value); |
---|
229 |
jlog_ctx_list_subscribers(jo->ctx, &list); |
---|
230 |
for(i=0; list[i]; i++) { |
---|
231 |
add_index_string(return_value, i, list[i], 1); |
---|
232 |
} |
---|
233 |
jlog_ctx_list_subscribers_dispose(jo->ctx, list); |
---|
234 |
} |
---|
235 |
/* }}} list_subscribers */ |
---|
236 |
|
---|
237 |
|
---|
238 |
|
---|
239 |
/* {{{ proto int raw_size() |
---|
240 |
*/ |
---|
241 |
PHP_METHOD(Jlog, raw_size) |
---|
242 |
{ |
---|
243 |
long size; |
---|
244 |
zend_class_entry * _this_ce; |
---|
245 |
zval * _this_zval = NULL; |
---|
246 |
jlog_obj *jo; |
---|
247 |
|
---|
248 |
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &_this_zval, Jlog_ce_ptr) == FAILURE) { |
---|
249 |
return; |
---|
250 |
} |
---|
251 |
|
---|
252 |
_this_ce = Z_OBJCE_P(_this_zval); |
---|
253 |
jo = (jlog_obj *) zend_object_store_get_object(_this_zval TSRMLS_CC); |
---|
254 |
if(!jo || !jo->ctx) { |
---|
255 |
php_error(E_WARNING, "no valid context"); |
---|
256 |
RETURN_LONG(0); |
---|
257 |
} |
---|
258 |
size = jlog_raw_size(jo->ctx); |
---|
259 |
RETURN_LONG(size); |
---|
260 |
} |
---|
261 |
/* }}} raw_size */ |
---|
262 |
|
---|
263 |
|
---|
264 |
|
---|
265 |
/* {{{ proto void close() |
---|
266 |
*/ |
---|
267 |
PHP_METHOD(Jlog, close) |
---|
268 |
{ |
---|
269 |
zend_class_entry * _this_ce; |
---|
270 |
zval * _this_zval = NULL; |
---|
271 |
jlog_obj *jo; |
---|
272 |
|
---|
273 |
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &_this_zval, Jlog_ce_ptr) == FAILURE) { |
---|
274 |
return; |
---|
275 |
} |
---|
276 |
|
---|
277 |
_this_ce = Z_OBJCE_P(_this_zval); |
---|
278 |
jo = (jlog_obj *) zend_object_store_get_object(_this_zval TSRMLS_CC); |
---|
279 |
if(!jo || !jo->ctx) { return; } |
---|
280 |
jlog_ctx_close(jo->ctx); |
---|
281 |
jo->ctx = NULL; |
---|
282 |
} |
---|
283 |
/* }}} close */ |
---|
284 |
|
---|
285 |
|
---|
286 |
static zend_function_entry Jlog_methods[] = { |
---|
287 |
PHP_ME(Jlog, __construct, Jlog____construct_args, /**/ZEND_ACC_PUBLIC) |
---|
288 |
PHP_ME(Jlog, add_subscriber, Jlog__add_subscriber_args, /**/ZEND_ACC_PUBLIC) |
---|
289 |
PHP_ME(Jlog, remove_subscriber, Jlog__remove_subscriber_args, /**/ZEND_ACC_PUBLIC) |
---|
290 |
PHP_ME(Jlog, list_subscribers, NULL, /**/ZEND_ACC_PUBLIC) |
---|
291 |
PHP_ME(Jlog, raw_size, NULL, /**/ZEND_ACC_PUBLIC) |
---|
292 |
PHP_ME(Jlog, close, NULL, /**/ZEND_ACC_PUBLIC) |
---|
293 |
{ NULL, NULL, NULL } |
---|
294 |
}; |
---|
295 |
|
---|
296 |
/* }}} Methods */ |
---|
297 |
|
---|
298 |
static void class_init_Jlog(TSRMLS_D) |
---|
299 |
{ |
---|
300 |
zend_class_entry ce; |
---|
301 |
|
---|
302 |
INIT_CLASS_ENTRY(ce, "Jlog", Jlog_methods); |
---|
303 |
ce.create_object = jlog_objects_new; |
---|
304 |
Jlog_ce_ptr = zend_register_internal_class(&ce TSRMLS_CC); |
---|
305 |
Jlog_ce_ptr->ce_flags |= ZEND_ACC_EXPLICIT_ABSTRACT_CLASS; |
---|
306 |
} |
---|
307 |
|
---|
308 |
/* }}} Class Jlog */ |
---|
309 |
|
---|
310 |
/* {{{ Class Jlog_Writer */ |
---|
311 |
|
---|
312 |
|
---|
313 |
/* {{{ Methods */ |
---|
314 |
|
---|
315 |
|
---|
316 |
/* {{{ proto object Jlog_Writer open() |
---|
317 |
*/ |
---|
318 |
PHP_METHOD(Jlog_Writer, open) |
---|
319 |
{ |
---|
320 |
zend_class_entry * _this_ce; |
---|
321 |
zval * _this_zval = NULL; |
---|
322 |
jlog_obj *jo; |
---|
323 |
|
---|
324 |
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &_this_zval, Jlog_Writer_ce_ptr) == FAILURE) { |
---|
325 |
return; |
---|
326 |
} |
---|
327 |
|
---|
328 |
_this_ce = Z_OBJCE_P(_this_zval); |
---|
329 |
jo = (jlog_obj *) zend_object_store_get_object(_this_zval TSRMLS_CC); |
---|
330 |
if(!jo || !jo->ctx) { |
---|
331 |
RETURN_NULL(); |
---|
332 |
} |
---|
333 |
if(jlog_ctx_open_writer(jo->ctx) != 0) { |
---|
334 |
php_error(E_WARNING, "jlog_ctx_open_writer failed"); |
---|
335 |
RETURN_NULL(); |
---|
336 |
} |
---|
337 |
ZVAL_ADDREF(_this_zval); |
---|
338 |
return_value = _this_zval; |
---|
339 |
} |
---|
340 |
/* }}} open */ |
---|
341 |
|
---|
342 |
|
---|
343 |
|
---|
344 |
/* {{{ proto bool write(string buffer) |
---|
345 |
*/ |
---|
346 |
PHP_METHOD(Jlog_Writer, write) |
---|
347 |
{ |
---|
348 |
zend_class_entry * _this_ce; |
---|
349 |
zval * _this_zval = NULL; |
---|
350 |
const char * buffer = NULL; |
---|
351 |
int buffer_len = 0; |
---|
352 |
jlog_obj *jo; |
---|
353 |
|
---|
354 |
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os", &_this_zval, Jlog_Writer_ce_ptr, &buffer, &buffer_len) == FAILURE) { |
---|
355 |
return; |
---|
356 |
} |
---|
357 |
|
---|
358 |
_this_ce = Z_OBJCE_P(_this_zval); |
---|
359 |
jo = (jlog_obj *) zend_object_store_get_object(_this_zval TSRMLS_CC); |
---|
360 |
if(!jo || !jo->ctx) { |
---|
361 |
RETURN_FALSE; |
---|
362 |
} |
---|
363 |
if(jlog_ctx_write(jo->ctx, buffer, buffer_len) < 0) { |
---|
364 |
RETURN_FALSE; |
---|
365 |
} |
---|
366 |
RETURN_TRUE; |
---|
367 |
} |
---|
368 |
/* }}} write */ |
---|
369 |
|
---|
370 |
|
---|
371 |
static zend_function_entry Jlog_Writer_methods[] = { |
---|
372 |
PHP_ME(Jlog_Writer, open, NULL, /**/ZEND_ACC_PUBLIC) |
---|
373 |
PHP_ME(Jlog_Writer, write, Jlog_Writer__write_args, /**/ZEND_ACC_PUBLIC) |
---|
374 |
{ NULL, NULL, NULL } |
---|
375 |
}; |
---|
376 |
|
---|
377 |
/* }}} Methods */ |
---|
378 |
|
---|
379 |
static void class_init_Jlog_Writer(TSRMLS_D) |
---|
380 |
{ |
---|
381 |
zend_class_entry ce; |
---|
382 |
|
---|
383 |
INIT_CLASS_ENTRY(ce, "Jlog_Writer", Jlog_Writer_methods); |
---|
384 |
ce.create_object = jlog_objects_new; |
---|
385 |
Jlog_Reader_ce_ptr = zend_register_internal_class_ex(&ce, Jlog_ce_ptr, NULL TSRMLS_CC); |
---|
386 |
} |
---|
387 |
|
---|
388 |
/* }}} Class Jlog_Writer */ |
---|
389 |
|
---|
390 |
/* {{{ Class Jlog_Reader */ |
---|
391 |
|
---|
392 |
/* {{{ Methods */ |
---|
393 |
|
---|
394 |
|
---|
395 |
/* {{{ proto object Jlog_Reader open(string subscriber) |
---|
396 |
*/ |
---|
397 |
PHP_METHOD(Jlog_Reader, open) |
---|
398 |
{ |
---|
399 |
zend_class_entry * _this_ce; |
---|
400 |
zval * _this_zval = NULL; |
---|
401 |
const char * subscriber = NULL; |
---|
402 |
int subscriber_len = 0; |
---|
403 |
jlog_obj *jo; |
---|
404 |
|
---|
405 |
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "Os", &_this_zval, Jlog_Reader_ce_ptr, &subscriber, &subscriber_len) == FAILURE) { |
---|
406 |
return; |
---|
407 |
} |
---|
408 |
|
---|
409 |
_this_ce = Z_OBJCE_P(_this_zval); |
---|
410 |
jo = (jlog_obj *) zend_object_store_get_object(_this_zval TSRMLS_CC); |
---|
411 |
if(!jo || !jo->ctx) { |
---|
412 |
RETURN_NULL(); |
---|
413 |
} |
---|
414 |
if(jlog_ctx_open_reader(jo->ctx, subscriber) != 0) { |
---|
415 |
RETURN_NULL(); |
---|
416 |
} |
---|
417 |
ZVAL_ADDREF(_this_zval); |
---|
418 |
return_value = _this_zval; |
---|
419 |
} |
---|
420 |
/* }}} open */ |
---|
421 |
|
---|
422 |
|
---|
423 |
|
---|
424 |
/* {{{ proto string read() |
---|
425 |
*/ |
---|
426 |
PHP_METHOD(Jlog_Reader, read) |
---|
427 |
{ |
---|
428 |
zend_class_entry * _this_ce; |
---|
429 |
zval * _this_zval = NULL; |
---|
430 |
jlog_obj *jo; |
---|
431 |
const jlog_id epoch = { 0, 0 }; |
---|
432 |
jlog_id cur; |
---|
433 |
jlog_message message; |
---|
434 |
int cnt; |
---|
435 |
|
---|
436 |
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &_this_zval, Jlog_Reader_ce_ptr) == FAILURE) { |
---|
437 |
return; |
---|
438 |
} |
---|
439 |
|
---|
440 |
_this_ce = Z_OBJCE_P(_this_zval); |
---|
441 |
jo = (jlog_obj *) zend_object_store_get_object(_this_zval TSRMLS_CC); |
---|
442 |
if(!jo || !jo->ctx) { |
---|
443 |
RETURN_FALSE; |
---|
444 |
} |
---|
445 |
/* if start is unset, we need to read the interval (again) */ |
---|
446 |
if(!memcmp(&jo->start, &epoch, sizeof(jlog_id))) |
---|
447 |
{ |
---|
448 |
cnt = jlog_ctx_read_interval(jo->ctx, &jo->start, &jo->end); |
---|
449 |
if(cnt == -1) { |
---|
450 |
php_error(E_WARNING, "jlog_ctx_read_interval failed"); |
---|
451 |
} |
---|
452 |
if(cnt == 0) { |
---|
453 |
jo->start = epoch; |
---|
454 |
jo->end = epoch; |
---|
455 |
RETURN_FALSE; |
---|
456 |
} |
---|
457 |
} |
---|
458 |
/* if last is unset, start at the beginning */ |
---|
459 |
if(!memcmp(&jo->last, &epoch, sizeof(jlog_id))) { |
---|
460 |
cur = jo->start; |
---|
461 |
} else { |
---|
462 |
/* if we've already read the end, return; otherwise advance */ |
---|
463 |
if (!memcmp(&jo->last, &jo->end, sizeof(jlog_id))) { |
---|
464 |
jo->start = epoch; |
---|
465 |
jo->end = epoch; |
---|
466 |
RETURN_FALSE; |
---|
467 |
} else { |
---|
468 |
cur = jo->last; |
---|
469 |
JLOG_ID_ADVANCE(&cur); |
---|
470 |
} |
---|
471 |
} |
---|
472 |
|
---|
473 |
if(jlog_ctx_read_message(jo->ctx, &cur, &message) != 0) { |
---|
474 |
php_error(E_WARNING, "read failed"); |
---|
475 |
RETURN_FALSE; |
---|
476 |
} |
---|
477 |
if(jo->auto_checkpoint) { |
---|
478 |
if(jlog_ctx_read_checkpoint(jo->ctx, &cur) != 0) { |
---|
479 |
php_error(E_WARNING, "checkpoint failed"); |
---|
480 |
RETURN_FALSE; |
---|
481 |
} |
---|
482 |
/* we have to re-read the interval after a checkpoint */ |
---|
483 |
jo->last = epoch; |
---|
484 |
jo->start = epoch; |
---|
485 |
jo->end = epoch; |
---|
486 |
} else { |
---|
487 |
/* update last */ |
---|
488 |
jo->last = cur; |
---|
489 |
/* if we've reaached the end, clear interval so we'll re-read it */ |
---|
490 |
if(!memcmp(&jo->last, &jo->end, sizeof(jlog_id))) { |
---|
491 |
jo->start = epoch; |
---|
492 |
jo->end = epoch; |
---|
493 |
} |
---|
494 |
} |
---|
495 |
RETURN_STRINGL(message.mess, message.mess_len, 1); |
---|
496 |
end: |
---|
497 |
; |
---|
498 |
} |
---|
499 |
|
---|
500 |
/* }}} read */ |
---|
501 |
|
---|
502 |
|
---|
503 |
|
---|
504 |
/* {{{ proto object Jlog_Reader checkpoint() |
---|
505 |
*/ |
---|
506 |
PHP_METHOD(Jlog_Reader, checkpoint) |
---|
507 |
{ |
---|
508 |
jlog_id epoch = { 0, 0 }; |
---|
509 |
zend_class_entry * _this_ce; |
---|
510 |
zval * _this_zval = NULL; |
---|
511 |
jlog_obj *jo; |
---|
512 |
|
---|
513 |
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O", &_this_zval, Jlog_Reader_ce_ptr) == FAILURE) { |
---|
514 |
return; |
---|
515 |
} |
---|
516 |
|
---|
517 |
_this_ce = Z_OBJCE_P(_this_zval); |
---|
518 |
jo = (jlog_obj *) zend_object_store_get_object(_this_zval TSRMLS_CC); |
---|
519 |
if(!jo || !jo->ctx) { RETURN_NULL(); } |
---|
520 |
if(memcmp(&jo->last, &epoch, sizeof(jlog_id))) |
---|
521 |
{ |
---|
522 |
jlog_ctx_read_checkpoint(jo->ctx, &jo->last); |
---|
523 |
/* we have to re-read the interval after a checkpoint */ |
---|
524 |
jo->last = epoch; |
---|
525 |
jo->start = epoch; |
---|
526 |
jo->end = epoch; |
---|
527 |
} |
---|
528 |
ZVAL_ADDREF(_this_zval); |
---|
529 |
return_value = _this_zval; |
---|
530 |
} |
---|
531 |
/* }}} checkpoint */ |
---|
532 |
|
---|
533 |
|
---|
534 |
|
---|
535 |
/* {{{ proto bool auto_checkpoint([bool state]) |
---|
536 |
*/ |
---|
537 |
PHP_METHOD(Jlog_Reader, auto_checkpoint) |
---|
538 |
{ |
---|
539 |
zend_class_entry * _this_ce; |
---|
540 |
zval * _this_zval = NULL; |
---|
541 |
zend_bool state = 0; |
---|
542 |
jlog_obj *jo; |
---|
543 |
|
---|
544 |
if (zend_parse_method_parameters(ZEND_NUM_ARGS() TSRMLS_CC, getThis(), "O|b", &_this_zval, Jlog_Reader_ce_ptr, &state) == FAILURE) { |
---|
545 |
return; |
---|
546 |
} |
---|
547 |
|
---|
548 |
fprintf(stderr, "num_args = %d\n", ZEND_NUM_ARGS()); |
---|
549 |
_this_ce = Z_OBJCE_P(_this_zval); |
---|
550 |
jo = (jlog_obj *) zend_object_store_get_object(_this_zval TSRMLS_CC); |
---|
551 |
if(!jo || !jo->ctx) { RETURN_NULL(); } |
---|
552 |
if(ZEND_NUM_ARGS() == 1) { |
---|
553 |
jo->auto_checkpoint = state; |
---|
554 |
} |
---|
555 |
RETURN_LONG(jo->auto_checkpoint); |
---|
556 |
} |
---|
557 |
/* }}} auto_checkpoint */ |
---|
558 |
|
---|
559 |
|
---|
560 |
static zend_function_entry Jlog_Reader_methods[] = { |
---|
561 |
PHP_ME(Jlog_Reader, open, Jlog_Reader__open_args, /**/ZEND_ACC_PUBLIC) |
---|
562 |
PHP_ME(Jlog_Reader, read, NULL, /**/ZEND_ACC_PUBLIC) |
---|
563 |
PHP_ME(Jlog_Reader, checkpoint, NULL, /**/ZEND_ACC_PUBLIC) |
---|
564 |
PHP_ME(Jlog_Reader, auto_checkpoint, Jlog_Reader__auto_checkpoint_args, /**/ZEND_ACC_PUBLIC) |
---|
565 |
{ NULL, NULL, NULL } |
---|
566 |
}; |
---|
567 |
|
---|
568 |
/* }}} Methods */ |
---|
569 |
|
---|
570 |
static void class_init_Jlog_Reader(TSRMLS_D) |
---|
571 |
{ |
---|
572 |
zend_class_entry ce; |
---|
573 |
|
---|
574 |
INIT_CLASS_ENTRY(ce, "Jlog_Reader", Jlog_Reader_methods); |
---|
575 |
ce.create_object = jlog_objects_new; |
---|
576 |
Jlog_Reader_ce_ptr = zend_register_internal_class_ex(&ce, Jlog_ce_ptr, NULL TSRMLS_CC); |
---|
577 |
} |
---|
578 |
|
---|
579 |
/* }}} Class Jlog_Reader */ |
---|
580 |
|
---|
581 |
/* }}} Class definitions*/ |
---|
582 |
|
---|
583 |
/* {{{ jlog_functions[] */ |
---|
584 |
function_entry jlog_functions[] = { |
---|
585 |
{ NULL, NULL, NULL } |
---|
586 |
}; |
---|
587 |
/* }}} */ |
---|
588 |
|
---|
589 |
|
---|
590 |
/* {{{ jlog_module_entry |
---|
591 |
*/ |
---|
592 |
zend_module_entry jlog_module_entry = { |
---|
593 |
STANDARD_MODULE_HEADER, |
---|
594 |
"jlog", |
---|
595 |
jlog_functions, |
---|
596 |
PHP_MINIT(jlog), /* Replace with NULL if there is nothing to do at php startup */ |
---|
597 |
PHP_MSHUTDOWN(jlog), /* Replace with NULL if there is nothing to do at php shutdown */ |
---|
598 |
PHP_RINIT(jlog), /* Replace with NULL if there is nothing to do at request start */ |
---|
599 |
PHP_RSHUTDOWN(jlog), /* Replace with NULL if there is nothing to do at request end */ |
---|
600 |
PHP_MINFO(jlog), |
---|
601 |
"0.0.1", |
---|
602 |
STANDARD_MODULE_PROPERTIES |
---|
603 |
}; |
---|
604 |
/* }}} */ |
---|
605 |
|
---|
606 |
#ifdef COMPILE_DL_JLOG |
---|
607 |
ZEND_GET_MODULE(jlog) |
---|
608 |
#endif |
---|
609 |
|
---|
610 |
|
---|
611 |
/* {{{ PHP_MINIT_FUNCTION */ |
---|
612 |
PHP_MINIT_FUNCTION(jlog) |
---|
613 |
{ |
---|
614 |
zend_object_handlers *std_hnd = zend_get_std_object_handlers(); |
---|
615 |
|
---|
616 |
memcpy(&jlog_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers)); |
---|
617 |
jlog_object_handlers.clone_obj = NULL; |
---|
618 |
|
---|
619 |
class_init_Jlog(TSRMLS_C); |
---|
620 |
class_init_Jlog_Writer(TSRMLS_C); |
---|
621 |
class_init_Jlog_Reader(TSRMLS_C); |
---|
622 |
|
---|
623 |
|
---|
624 |
/* add your stuff here */ |
---|
625 |
|
---|
626 |
return SUCCESS; |
---|
627 |
} |
---|
628 |
/* }}} */ |
---|
629 |
|
---|
630 |
|
---|
631 |
/* {{{ PHP_MSHUTDOWN_FUNCTION */ |
---|
632 |
PHP_MSHUTDOWN_FUNCTION(jlog) |
---|
633 |
{ |
---|
634 |
|
---|
635 |
/* add your stuff here */ |
---|
636 |
|
---|
637 |
return SUCCESS; |
---|
638 |
} |
---|
639 |
/* }}} */ |
---|
640 |
|
---|
641 |
|
---|
642 |
/* {{{ PHP_RINIT_FUNCTION */ |
---|
643 |
PHP_RINIT_FUNCTION(jlog) |
---|
644 |
{ |
---|
645 |
/* add your stuff here */ |
---|
646 |
|
---|
647 |
return SUCCESS; |
---|
648 |
} |
---|
649 |
/* }}} */ |
---|
650 |
|
---|
651 |
|
---|
652 |
/* {{{ PHP_RSHUTDOWN_FUNCTION */ |
---|
653 |
PHP_RSHUTDOWN_FUNCTION(jlog) |
---|
654 |
{ |
---|
655 |
/* add your stuff here */ |
---|
656 |
|
---|
657 |
return SUCCESS; |
---|
658 |
} |
---|
659 |
/* }}} */ |
---|
660 |
|
---|
661 |
|
---|
662 |
/* {{{ PHP_MINFO_FUNCTION */ |
---|
663 |
PHP_MINFO_FUNCTION(jlog) |
---|
664 |
{ |
---|
665 |
php_info_print_box_start(0); |
---|
666 |
php_printf("<p>A sample PHP extension</p>\n"); |
---|
667 |
php_printf("<p>Version 0.0.1devel (2007-07-02)</p>\n"); |
---|
668 |
php_info_print_box_end(); |
---|
669 |
/* add your stuff here */ |
---|
670 |
|
---|
671 |
} |
---|
672 |
/* }}} */ |
---|
673 |
|
---|
674 |
#endif /* HAVE_JLOG */ |
---|
675 |
|
---|
676 |
|
---|
677 |
/* |
---|
678 |
* Local variables: |
---|
679 |
* tab-width: 4 |
---|
680 |
* c-basic-offset: 4 |
---|
681 |
* End: |
---|
682 |
* vim600: et sw=2 ts=2 sts=2 ai bs=2 fdm=marker |
---|
683 |
*/ |
---|