Class: Jlog

Inherits:
Object
  • Object
show all
Defined in:
lib/jlog.rb,
lib/jlog/version.rb,
ext/jlog/jlog.c

Direct Known Subclasses

Reader, Writer

Defined Under Namespace

Classes: Error, Reader, Writer

Constant Summary collapse

VERSION =
"0.1.0"

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Object



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
# File 'ext/jlog/jlog.c', line 58

VALUE rJlog_initialize(int argc, VALUE* argv, VALUE klass) 
{
   int options;
   Jlog jo;
   jlog_id zeroed = {0,0};
   VALUE path;
   VALUE optarg;
   VALUE size;

   rb_scan_args(argc, argv, "12", &path, &optarg, &size);

   if(NIL_P(optarg)) {
      options = O_CREAT;
   } else {
      options = (int)NUM2INT(optarg);
   }

   if(NIL_P(size)) {
      size = (size_t)INT2FIX(0);
   }

   Data_Get_Struct(klass, jlog_obj, jo);

   jo->ctx = jlog_new(StringValuePtr(path));
   jo->path = strdup(StringValuePtr(path));
   jo->auto_checkpoint = 0;
   jo->start = zeroed;
   jo->prev = zeroed;
   jo->last = zeroed;
   jo->end = zeroed;


   if(!jo->ctx) {
      rJlog_free(jo);
      rb_raise(eJlog, "jlog_new(%s) failed", StringValuePtr(path));
   }

   if(options & O_CREAT) {
      if(jlog_ctx_init(jo->ctx) != 0) {
         if(jlog_ctx_err(jo->ctx) == JLOG_ERR_CREATE_EXISTS) {
            if(options & O_EXCL) {
               rJlog_free(jo);
               rb_raise(eJlog, "file already exists: %s", StringValuePtr(path));
            }
         }else {
            rJlog_raise(jo, "Error initializing jlog");
         }
      }
      jlog_ctx_close(jo->ctx);
      jo->ctx = jlog_new(StringValuePtr(path));
      if(!jo->ctx) {
         rJlog_free(jo);
         rb_raise(eJlog, "jlog_new(%s) failed after successful init", StringValuePtr(path));
      }
      rJlog_populate_subscribers(klass);
   } 

   if(!jo) { 
      rb_raise(eJlog, "jo wasn't initialized.");
   }

   return klass;
}

Instance Method Details

#add_subscriber(*args) ⇒ Object



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
# File 'ext/jlog/jlog.c', line 129

VALUE rJlog_add_subscriber(int argc, VALUE* argv, VALUE self)
{
   VALUE s;
   VALUE w;
   unsigned int whence;
   Jlog jo;

   rb_scan_args(argc, argv, "11", &s, &w);

   if(NIL_P(w)) {
      whence = 0;
   } else {
      whence = NUM2UINT(w);
   }

   Data_Get_Struct(self, jlog_obj, jo);

   if(!jo || !jo->ctx || jlog_ctx_add_subscriber(jo->ctx, StringValuePtr(s), whence) != 0) {
      return Qfalse;
   }

   rJlog_populate_subscribers(self);

   return Qtrue;
}

#closeObject



227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'ext/jlog/jlog.c', line 227

VALUE rJlog_close(VALUE self)
{
   Jlog jo;

   Data_Get_Struct(self, jlog_obj, jo);

   if(!jo || !jo->ctx) return Qnil;

   jlog_ctx_close(jo->ctx);
   jo->ctx = NULL;

   return Qtrue;
}

#destroyObject



254
255
256
257
258
259
260
261
262
263
264
265
# File 'ext/jlog/jlog.c', line 254

VALUE rJlog_destroy(VALUE self)
{
   Jlog jo;

   Data_Get_Struct(self, jlog_obj, jo);

   if(!jo) return Qnil;

   rJlog_free(jo);

   return Qtrue;
}

#inspectObject



241
242
243
244
245
246
247
248
249
250
251
252
# File 'ext/jlog/jlog.c', line 241

VALUE rJlog_inspect(VALUE self)
{
   Jlog jo;

   Data_Get_Struct(self, jlog_obj, jo);

   if(!jo || !jo->ctx) return Qnil;

   // XXX Fill in inspect call data 

   return Qtrue;
}

#list_subscribersObject



196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
# File 'ext/jlog/jlog.c', line 196

VALUE rJlog_list_subscribers(VALUE self)
{
   Jlog jo;

   Data_Get_Struct(self, jlog_obj, jo);

   if(!jo || !jo->ctx)
   {
      rb_raise(eJlog, "Invalid jlog context");
   }

   rJlog_populate_subscribers(self);

   return rb_iv_get(self, "@subscribers");
}

#raw_sizeObject Also known as: size



212
213
214
215
216
217
218
219
220
221
222
223
224
225
# File 'ext/jlog/jlog.c', line 212

VALUE rJlog_raw_size(VALUE self)
{
   size_t size;
   Jlog jo;

   Data_Get_Struct(self, jlog_obj, jo);

   if(!jo || !jo->ctx) {
      rb_raise(eJlog, "Invalid jlog context");
   }
   size = jlog_raw_size(jo->ctx);

   return INT2NUM(size);
}

#remove_subscriber(subscriber) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'ext/jlog/jlog.c', line 156

VALUE rJlog_remove_subscriber(VALUE self, VALUE subscriber)
{
   Jlog jo;
   int res;

   Data_Get_Struct(self, jlog_obj, jo);
   res = jlog_ctx_remove_subscriber(jo->ctx, StringValuePtr(subscriber));
   if(!jo || !jo->ctx || res != 0)
   {
      return res;
   }

   rJlog_populate_subscribers(self);

   return Qtrue;
}