| 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 "jlog.h" |
|---|
| 34 |
|
|---|
| 35 |
#define SUBSCRIBER "voyeur" |
|---|
| 36 |
#define LOGNAME "/tmp/jtest.foo" |
|---|
| 37 |
jlog_ctx *ctx; |
|---|
| 38 |
|
|---|
| 39 |
void usage() { |
|---|
| 40 |
fprintf(stderr, "options:\n\tinit\n\tread <count>\n\twrite <len> <count>\n"); |
|---|
| 41 |
} |
|---|
| 42 |
void jcreate() { |
|---|
| 43 |
ctx = jlog_new(LOGNAME); |
|---|
| 44 |
jlog_ctx_alter_journal_size(ctx, 1024); |
|---|
| 45 |
if(jlog_ctx_init(ctx) != 0) { |
|---|
| 46 |
fprintf(stderr, "jlog_ctx_init failed: %d %s\n", jlog_ctx_err(ctx), jlog_ctx_err_string(ctx)); |
|---|
| 47 |
} else { |
|---|
| 48 |
jlog_ctx_add_subscriber(ctx, SUBSCRIBER, JLOG_BEGIN); |
|---|
| 49 |
} |
|---|
| 50 |
jlog_ctx_close(ctx); |
|---|
| 51 |
} |
|---|
| 52 |
|
|---|
| 53 |
void jopenw(char *foo, int count) { |
|---|
| 54 |
int i; |
|---|
| 55 |
ctx = jlog_new(LOGNAME); |
|---|
| 56 |
if(jlog_ctx_open_writer(ctx) != 0) { |
|---|
| 57 |
fprintf(stderr, "jlog_ctx_open_writer failed: %d %s\n", jlog_ctx_err(ctx), jlog_ctx_err_string(ctx)); |
|---|
| 58 |
exit(-1); |
|---|
| 59 |
} |
|---|
| 60 |
for(i=0; i<count; i++) |
|---|
| 61 |
if(jlog_ctx_write(ctx, foo, strlen(foo)) != 0) |
|---|
| 62 |
fprintf(stderr, "jlog_ctx_write_message failed: %d %s\n", jlog_ctx_err(ctx), jlog_ctx_err_string(ctx)); |
|---|
| 63 |
|
|---|
| 64 |
jlog_ctx_close(ctx); |
|---|
| 65 |
} |
|---|
| 66 |
void jopenr(char *s, int expect) { |
|---|
| 67 |
char begins[20], ends[20]; |
|---|
| 68 |
jlog_id begin, end; |
|---|
| 69 |
int count; |
|---|
| 70 |
jlog_message message; |
|---|
| 71 |
|
|---|
| 72 |
ctx = jlog_new(LOGNAME); |
|---|
| 73 |
if(jlog_ctx_open_reader(ctx, s) != 0) { |
|---|
| 74 |
fprintf(stderr, "jlog_ctx_open_reader failed: %d %s\n", jlog_ctx_err(ctx), jlog_ctx_err_string(ctx)); |
|---|
| 75 |
exit(-1); |
|---|
| 76 |
} |
|---|
| 77 |
while(expect > 0) { |
|---|
| 78 |
if((count = jlog_ctx_read_interval(ctx, &begin, &end)) == -1) { |
|---|
| 79 |
fprintf(stderr, "jlog_ctx_read_interval failed: %d %s\n", jlog_ctx_err(ctx), jlog_ctx_err_string(ctx)); |
|---|
| 80 |
exit(-1); |
|---|
| 81 |
} |
|---|
| 82 |
jlog_snprint_logid(begins, sizeof(begins), &begin); |
|---|
| 83 |
jlog_snprint_logid(ends, sizeof(ends), &end); |
|---|
| 84 |
/* printf("reader [%s] (%s, %s] count: %d\n", s, begins, ends, count); */ |
|---|
| 85 |
if(count > 0) { |
|---|
| 86 |
int i; |
|---|
| 87 |
count = MIN(count, expect); |
|---|
| 88 |
for(i=0; i<count; i++, JLOG_ID_ADVANCE(&begin)) { |
|---|
| 89 |
end = begin; |
|---|
| 90 |
if(jlog_ctx_read_message(ctx, &begin, &message) != 0) { |
|---|
| 91 |
fprintf(stderr, "read failed: %d\n", jlog_ctx_err(ctx)); |
|---|
| 92 |
} else { |
|---|
| 93 |
expect--; |
|---|
| 94 |
jlog_snprint_logid(begins, sizeof(begins), &begin); |
|---|
| 95 |
fprintf(stderr, "[%7d] read: [%s]\n\t'%.*s'\n", expect, begins, |
|---|
| 96 |
message.mess_len, (char *)message.mess); |
|---|
| 97 |
} |
|---|
| 98 |
} |
|---|
| 99 |
if(jlog_ctx_read_checkpoint(ctx, &end) != 0) { |
|---|
| 100 |
fprintf(stderr, "checkpoint failed: %d %s\n", jlog_ctx_err(ctx), jlog_ctx_err_string(ctx)); |
|---|
| 101 |
} else { |
|---|
| 102 |
fprintf(stderr, "\tcheckpointed...\n"); |
|---|
| 103 |
} |
|---|
| 104 |
} |
|---|
| 105 |
} |
|---|
| 106 |
jlog_ctx_close(ctx); |
|---|
| 107 |
} |
|---|
| 108 |
int main(int argc, char **argv) { |
|---|
| 109 |
int i; |
|---|
| 110 |
#if _WIN32 |
|---|
| 111 |
mem_init(); |
|---|
| 112 |
#endif |
|---|
| 113 |
for(i=1; i<argc; i++) { |
|---|
| 114 |
if(!strcmp(argv[i], "init")) { |
|---|
| 115 |
jcreate(); |
|---|
| 116 |
exit(0); |
|---|
| 117 |
} else if(!strcmp(argv[i], "write")) { |
|---|
| 118 |
int len; |
|---|
| 119 |
char *message; |
|---|
| 120 |
if(i+2 >= argc) { usage(); exit(-1); } |
|---|
| 121 |
i++; |
|---|
| 122 |
len = atoi(argv[i]); |
|---|
| 123 |
message = malloc(len+1); |
|---|
| 124 |
memset(message, 'X', len-1); |
|---|
| 125 |
message[len-1] = '\n'; |
|---|
| 126 |
message[len] = '\0'; |
|---|
| 127 |
i++; |
|---|
| 128 |
jopenw(message, atoi(argv[i])); |
|---|
| 129 |
exit(0); |
|---|
| 130 |
} else if(!strcmp(argv[i], "read")) { |
|---|
| 131 |
if(i+1 >= argc) { usage(); exit(-1); } |
|---|
| 132 |
i++; |
|---|
| 133 |
jopenr(SUBSCRIBER, atoi(argv[i])); |
|---|
| 134 |
exit(0); |
|---|
| 135 |
} else { |
|---|
| 136 |
fprintf(stderr, "command '%s' not understood\n", argv[i]); |
|---|
| 137 |
usage(); |
|---|
| 138 |
} |
|---|
| 139 |
} |
|---|
| 140 |
return 0; |
|---|
| 141 |
} |
|---|