Class: Agoo::ErrorStream

Inherits:
Object
  • Object
show all
Defined in:
ext/agoo/error_stream.c,
ext/agoo/error_stream.c

Overview

Used in a reqquest as the rack.errors attribute. Writing to the stream and flushing will make an error log entry.

Instance Method Summary collapse

Instance Method Details

#closeObject

call-seq: close()

Closes the stream.



114
115
116
117
118
119
120
121
122
123
# File 'ext/agoo/error_stream.c', line 114

static VALUE
es_close(VALUE self) {
    ErrorStream	es = (ErrorStream)DATA_PTR(self);

    es_flush(self);
    DATA_PTR(self) = NULL;
    es_free(es);

    return self;
}

#flushObject

call-seq: flush()

Flushs the accumulated text in the stream as an error log entry.



94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'ext/agoo/error_stream.c', line 94

static VALUE
es_flush(VALUE self) {
    ErrorStream	es = (ErrorStream)DATA_PTR(self);

    if (NULL == es) {
	rb_raise(rb_eIOError, "error stream has been closed.");
    }
    if (NULL != es->text) {
	agoo_log_cat(&agoo_error_cat, "%s", es->text->text);
	es->text->len = 0;
    }
    return self;
}

#puts(str) ⇒ Object

call-seq: puts(str)

Write the str to the stream along with a newline character, accumulating it until flush is called.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'ext/agoo/error_stream.c', line 45

static VALUE
es_puts(VALUE self, VALUE str) {
    ErrorStream	es = (ErrorStream)DATA_PTR(self);

    if (NULL == es) {
	rb_raise(rb_eIOError, "error stream has been closed.");
    }
    if (NULL == es->text &&
	NULL == (es->text = agoo_text_allocate(1024))) {
	rb_raise(rb_eNoMemError, "Failed to allocate memory for the error stream buffer.");
    }
    es->text = agoo_text_append(es->text, StringValuePtr(str), (int)RSTRING_LEN(str));
    es->text = agoo_text_append(es->text, "\n", 1);
    if (NULL == es->text) {
	rb_raise(rb_eNoMemError, "Failed to allocate memory for the error stream puts.");
    }
    return Qnil;
}

#write(str) ⇒ Object

call-seq: write(str)

Write the str to the stream, accumulating it until flush is called.



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'ext/agoo/error_stream.c', line 70

static VALUE
es_write(VALUE self, VALUE str) {
    ErrorStream	es = (ErrorStream)DATA_PTR(self);
    int		cnt = (int)RSTRING_LEN(str);

    if (NULL == es) {
	rb_raise(rb_eIOError, "error stream has been closed.");
    }
    if (NULL == es->text &&
	NULL == (es->text = agoo_text_allocate(1024))) {
	rb_raise(rb_eNoMemError, "Failed to allocate memory for the error stream buffer.");
    }
    if (NULL == (es->text = agoo_text_append(es->text, StringValuePtr(str), cnt))) {
	rb_raise(rb_eNoMemError, "Failed to allocate memory for the error stream puts.");
    }
    return INT2NUM(cnt);
}