Class: Jlog::Writer

Inherits:
Jlog
  • Object
show all
Defined in:
ext/jlog/jlog.c

Constant Summary

Constants inherited from Jlog

VERSION

Instance Method Summary collapse

Methods inherited from Jlog

#add_subscriber, #close, #destroy, #inspect, #list_subscribers, #raw_size, #remove_subscriber

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

#openObject



267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
# File 'ext/jlog/jlog.c', line 267

VALUE rJlog_W_open(VALUE self)
{
   Jlog_Writer jo;

   Data_Get_Struct(self, jlog_obj, jo);

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

   if(jlog_ctx_open_writer(jo->ctx) != 0) {
      rJlog_raise(jo, "jlog_ctx_open_writer failed");
   }

   return Qtrue;
}

#write(msg) ⇒ Object



284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'ext/jlog/jlog.c', line 284

VALUE rJlog_W_write(VALUE self, VALUE msg)
{
   int err;
   Jlog_Writer jo;

   Data_Get_Struct(self, jlog_obj, jo);

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

#if !defined(RSTRING_LEN)
#  define RSTRING_LEN(x) (RSTRING(x)->len)
#endif
   err = jlog_ctx_write(jo->ctx, StringValuePtr(msg), (size_t) RSTRING_LEN(msg));
   if(err != 0) {
      return Qfalse;
   } else {
      return Qtrue;
   }
}