root/jtest.c

Revision 81ac86a15e3940ff0b787698853fb6a6157f800b, 3.2 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 7 years ago)

initial import with Ecelerity bits removed and some autoconf glue added in. Could certainly use some work on the build/install. Needs shared lib support for multiple platforms

  • Property mode set to 100644
Line 
1 #include "jlog.h"
2
3 #define SUBSCRIBER "voyeur"
4 #define LOGNAME    "/tmp/jtest.foo"
5 jlog_ctx *ctx;
6
7 void usage() {
8   fprintf(stderr, "options:\n\tinit\n\tread <count>\n\twrite <len> <count>\n");
9 }
10 void jcreate() {
11   ctx = jlog_new(LOGNAME);
12   jlog_ctx_alter_journal_size(ctx, 1024);
13   if(jlog_ctx_init(ctx) != 0) {
14     fprintf(stderr, "jlog_ctx_init failed: %d %s\n", jlog_ctx_err(ctx), jlog_ctx_err_string(ctx));
15   } else {
16     jlog_ctx_add_subscriber(ctx, SUBSCRIBER, JLOG_BEGIN);
17   }
18   jlog_ctx_close(ctx);
19 }
20
21 void jopenw(char *foo, int count) {
22   int i;
23   ctx = jlog_new(LOGNAME);
24   if(jlog_ctx_open_writer(ctx) != 0) {
25     fprintf(stderr, "jlog_ctx_open_writer failed: %d %s\n", jlog_ctx_err(ctx), jlog_ctx_err_string(ctx));
26     exit(-1);
27   }
28   for(i=0; i<count; i++)
29     if(jlog_ctx_write(ctx, foo, strlen(foo)) != 0)
30       fprintf(stderr, "jlog_ctx_write_message failed: %d %s\n", jlog_ctx_err(ctx), jlog_ctx_err_string(ctx));
31
32   jlog_ctx_close(ctx);
33 }
34 void jopenr(char *s, int expect) {
35   char begins[20], ends[20];
36   jlog_id begin, end;
37   int count;
38   jlog_message message;
39
40   ctx = jlog_new(LOGNAME);
41   if(jlog_ctx_open_reader(ctx, s) != 0) {
42     fprintf(stderr, "jlog_ctx_open_reader failed: %d %s\n", jlog_ctx_err(ctx), jlog_ctx_err_string(ctx));
43     exit(-1);
44   }
45   while(expect > 0) {
46     if((count = jlog_ctx_read_interval(ctx, &begin, &end)) == -1) {
47       fprintf(stderr, "jlog_ctx_read_interval failed: %d %s\n", jlog_ctx_err(ctx), jlog_ctx_err_string(ctx));
48       exit(-1);
49     }
50     jlog_snprint_logid(begins, sizeof(begins), &begin);
51     jlog_snprint_logid(ends, sizeof(ends), &end);
52     /* printf("reader [%s]  (%s, %s] count: %d\n", s, begins, ends, count); */
53     if(count > 0) {
54       int i;
55       count = MIN(count, expect);
56       for(i=0; i<count; i++, JLOG_ID_ADVANCE(&begin)) {
57         end = begin;
58         if(jlog_ctx_read_message(ctx, &begin, &message) != 0) {
59           fprintf(stderr, "read failed: %d\n", jlog_ctx_err(ctx));
60         } else {
61           expect--;
62           jlog_snprint_logid(begins, sizeof(begins), &begin);
63           fprintf(stderr, "[%7d] read: [%s]\n\t'%.*s'\n", expect, begins,
64                   message.mess_len, (char *)message.mess);
65         }
66       }
67       if(jlog_ctx_read_checkpoint(ctx, &end) != 0) {
68         fprintf(stderr, "checkpoint failed: %d %s\n", jlog_ctx_err(ctx), jlog_ctx_err_string(ctx));
69       } else {
70         fprintf(stderr, "\tcheckpointed...\n");
71       }
72     }
73   }
74   jlog_ctx_close(ctx);
75 }
76 int main(int argc, char **argv) {
77   int i;
78 #if _WIN32
79   mem_init();
80 #endif
81   for(i=1; i<argc; i++) {
82     if(!strcmp(argv[i], "init")) {
83       jcreate();
84       exit(0);
85     } else if(!strcmp(argv[i], "write")) {
86       int len;
87       char *message;
88       if(i+2 >= argc) { usage(); exit(-1); }
89       i++;
90       len = atoi(argv[i]);
91       message = malloc(len+1);
92       memset(message, 'X', len-1);
93       message[len-1] = '\n';
94       message[len] = '\0';
95       i++;
96       jopenw(message, atoi(argv[i]));
97       exit(0);
98     } else if(!strcmp(argv[i], "read")) {
99       if(i+1 >= argc) { usage(); exit(-1); }
100       i++;
101       jopenr(SUBSCRIBER, atoi(argv[i]));
102       exit(0);
103     } else {
104       fprintf(stderr, "command '%s' not understood\n", argv[i]);
105       usage();
106     }
107   }
108   return 0;
109 }
Note: See TracBrowser for help on using the browser.