Module: Agoo::Log

Defined in:
ext/agoo/rlog.c

Class Method Summary collapse

Class Method Details

.classicObject

call-seq: classic()

Set the log format to classic format.



365
366
367
368
369
# File 'ext/agoo/rlog.c', line 365

static VALUE
rlog_classic(VALUE self) {
    the_log.classic = true;
    return Qnil;
}

.color(label) ⇒ Object

call-seq: color(label)

Returns the current color name as a Symbol for the specified label.



227
228
229
230
231
232
233
234
235
# File 'ext/agoo/rlog.c', line 227

static VALUE
rlog_color_get(VALUE self, VALUE label) {
    LogCat	cat = log_cat_find(StringValuePtr(label));

    if (NULL == cat) {
	return Qnil;
    }
    return ID2SYM(rb_intern(cat->color->name));
}

.configure(options) ⇒ Object

call-seq: configure(options)

Configures the logger

  • options [Hash] server options

    • :dir [String] directory to place log files in. If nil or empty then no log files are written.

    • :console [true|false] if true log entry are display on the console.

    • :classic [true|false] if true log entry follow a classic format. If false log entries are JSON.

    • :colorize [true|false] if true log entries are colorized.

    • :states [Hash] a map of logging categories and whether they should be on or off. Categories are:

      • :ERROR errors

      • :WARN warnings

      • :INFO infomational

      • :DEBUG debugging

      • :connect openning and closing of connections

      • :request requests

      • :response responses

      • :eval handler evaluationss

      • :push writes to WebSocket or SSE connection



48
49
50
51
52
53
54
55
56
57
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
# File 'ext/agoo/rlog.c', line 48

static VALUE
rlog_configure(VALUE self, VALUE options) {
    if (Qnil != options) {
	VALUE	v;

	if (Qnil != (v = rb_hash_lookup(options, ID2SYM(rb_intern("dir"))))) {
	    rb_check_type(v, T_STRING);
	    strncpy(the_log.dir, StringValuePtr(v), sizeof(the_log.dir));
	    the_log.dir[sizeof(the_log.dir) - 1] = '\0';
	}
	if (Qnil != (v = rb_hash_lookup(options, ID2SYM(rb_intern("max_files"))))) {
	    int	max = FIX2INT(v);

	    if (1 <= max || max < 100) {
		the_log.max_files = max;
	    } else {
		rb_raise(rb_eArgError, "max_files must be between 1 and 100.");
	    }
	}
	if (Qnil != (v = rb_hash_lookup(options, ID2SYM(rb_intern("max_size"))))) {
	    int	max = FIX2INT(v);

	    if (1 <= max) {
		the_log.max_size = max;
	    } else {
		rb_raise(rb_eArgError, "max_size must be 1 or more.");
	    }
	}
	if (Qnil != (v = rb_hash_lookup(options, ID2SYM(rb_intern("console"))))) {
	    the_log.console = (Qtrue == v);
	}
	if (Qnil != (v = rb_hash_lookup(options, ID2SYM(rb_intern("classic"))))) {
	    the_log.classic = (Qtrue == v);
	}
	if (Qnil != (v = rb_hash_lookup(options, ID2SYM(rb_intern("colorize"))))) {
	    the_log.colorize = (Qtrue == v);
	}
	if (Qnil != (v = rb_hash_lookup(options, ID2SYM(rb_intern("states"))))) {
	    if (T_HASH == rb_type(v)) {
		LogCat	cat = the_log.cats;
		VALUE	cv;
		
		for (; NULL != cat; cat = cat->next) {
		    if (Qnil != (cv = rb_hash_lookup(v, ID2SYM(rb_intern(cat->label))))) {
			if (Qtrue == cv) {
			    cat->on = true;
			} else if (Qfalse == cv) {
			    cat->on = false;
			}
		    }
		}
	    } else {
		rb_raise(rb_eArgError, "states must be a Hash.");
	    }
	}
    }
    if (NULL != the_log.file) {
	fclose(the_log.file);
	the_log.file = NULL;
    }
    if ('\0' != *the_log.dir) {
	if (0 != mkdir(the_log.dir, 0770) && EEXIST != errno) {
	    rb_raise(rb_eIOError, "Failed to create '%s'.", the_log.dir);
	}
	open_log_file();
    }
    return Qnil;
}

.console=(on) ⇒ Object

call-seq: console=(on)

If on then log output also goes to the console.



353
354
355
356
357
# File 'ext/agoo/rlog.c', line 353

static VALUE
rlog_console(VALUE self, VALUE on) {
    the_log.console = (Qtrue == on);
    return Qnil;
}

.debug(msg) ⇒ Object

call-seq: debug(msg)

Log a debug message.



215
216
217
218
219
# File 'ext/agoo/rlog.c', line 215

static VALUE
rlog_debug(VALUE self, VALUE msg) {
    log_cat(&debug_cat, "%s", StringValuePtr(msg));
    return Qnil;
}

.debug?Boolean

call-seq: debug?()

Returns true is debug entries are being logged.

Returns:

  • (Boolean)


168
169
170
171
# File 'ext/agoo/rlog.c', line 168

static VALUE
rlog_debugp(VALUE self) {
    return debug_cat.on ? Qtrue : Qfalse;
}

.error(msg) ⇒ Object

call-seq: error(msg)

Log an error message.



179
180
181
182
183
# File 'ext/agoo/rlog.c', line 179

static VALUE
rlog_error(VALUE self, VALUE msg) {
    log_cat(&error_cat, "%s", StringValuePtr(msg));
    return Qnil;
}

.error?Boolean

call-seq: error?()

Returns true is errors are being logged.

Returns:

  • (Boolean)


135
136
137
138
# File 'ext/agoo/rlog.c', line 135

static VALUE
rlog_errorp(VALUE self) {
    return error_cat.on ? Qtrue : Qfalse;
}

.flush(to) ⇒ Object

call-seq: flush

Flush the log queue and write all entries to disk or the console. The call waits for the flush to complete or the timeout to be exceeded.



325
326
327
328
329
330
331
332
333
# File 'ext/agoo/rlog.c', line 325

static VALUE
rlog_flush(VALUE self, VALUE to) {
    double	timeout = NUM2DBL(to);
    
    if (!log_flush(timeout)) {
	rb_raise(rb_eStandardError, "timed out waiting for log flush.");
    }
    return Qnil;
}

.info(msg) ⇒ Object

call-seq: info(msg)

Log an info message.



203
204
205
206
207
# File 'ext/agoo/rlog.c', line 203

static VALUE
rlog_info(VALUE self, VALUE msg) {
    log_cat(&info_cat, "%s", StringValuePtr(msg));
    return Qnil;
}

.info?Boolean

call-seq: info?()

Returns true is info entries are being logged.

Returns:

  • (Boolean)


157
158
159
160
# File 'ext/agoo/rlog.c', line 157

static VALUE
rlog_infop(VALUE self) {
    return info_cat.on ? Qtrue : Qfalse;
}

.jsonObject

call-seq: json()

Set the log format to JSON format.



377
378
379
380
381
# File 'ext/agoo/rlog.c', line 377

static VALUE
rlog_json(VALUE self) {
    the_log.classic = false;
    return Qnil;
}

.log(label, msg) ⇒ Object

call-seq: log = msg

Log a message in the specified category.



305
306
307
308
309
310
311
312
313
314
315
316
# File 'ext/agoo/rlog.c', line 305

static VALUE
rlog_log(VALUE self, VALUE label, VALUE msg) {
    const char	*label_str = StringValuePtr(label);
    LogCat	cat = log_cat_find(label_str);

    if (NULL == cat) {
	rb_raise(rb_eArgError, "%s is not a valid category.", label_str);
    }
    log_cat(cat, "%s", StringValuePtr(msg));
    
    return Qnil;
}

.max_files=(rmax) ⇒ Object

call-seq: max_files(max)

Maximum log files files is reset.



407
408
409
410
411
412
413
414
415
416
417
# File 'ext/agoo/rlog.c', line 407

static VALUE
rlog_max_files(VALUE self, VALUE rmax) {
    int	max = FIX2INT(rmax);

    if (1 <= max || max < 100) {
	the_log.max_files = max;
    } else {
	rb_raise(rb_eArgError, "max_files must be between 1 and 100.");
    }
    return Qnil;
}

.max_size=(rmax) ⇒ Object

call-seq: max_size(size)

Maximum log files size is reset.



389
390
391
392
393
394
395
396
397
398
399
# File 'ext/agoo/rlog.c', line 389

static VALUE
rlog_max_size(VALUE self, VALUE rmax) {
    int	max = FIX2INT(rmax);

    if (1 <= max) {
	the_log.max_size = max;
    } else {
	rb_raise(rb_eArgError, "max_size must be 1 or more.");
    }
    return Qnil;
}

.rotateObject

call-seq: rotate()

Rotate the log files.



341
342
343
344
345
# File 'ext/agoo/rlog.c', line 341

static VALUE
rlog_rotate(VALUE self) {
    log_rotate();
    return Qnil;
}

.set_color(label, color) ⇒ Object

call-seq: set_color(label, color_symbol)

Sets color of the category associated with a label. Valid colors are :black, :red, :green, :yellow, :blue, :magenta, :cyan, :white, :gray, :dark_red, :dark_green, :brown, :dark_blue, :purple, and :dark_cyan.



245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'ext/agoo/rlog.c', line 245

static VALUE
rlog_color_set(VALUE self, VALUE label, VALUE color) {
    const char	*label_str = StringValuePtr(label);
    const char	*color_name = StringValuePtr(color);
    LogCat	cat = log_cat_find(label_str);
    Color	c = find_color(color_name);

    if (NULL == cat) {
	rb_raise(rb_eArgError, "%s is not a valid category.", label_str);
    }
    if (NULL == c) {
	rb_raise(rb_eArgError, "%s is not a valid color.", color_name);
    }
    cat->color = c;
    
    return Qnil;
}

.set_state(label, state) ⇒ Object

call-seq: set_state(label, state)

Sets state of the category associated with a label.



286
287
288
289
290
291
292
293
294
295
296
297
# File 'ext/agoo/rlog.c', line 286

static VALUE
rlog_on_set(VALUE self, VALUE label, VALUE state) {
    const char	*label_str = StringValuePtr(label);
    LogCat	cat = log_cat_find(label_str);

    if (NULL == cat) {
	rb_raise(rb_eArgError, "%s is not a valid category.", label_str);
    }
    cat->on = (Qtrue == state);
    
    return cat->on ? Qtrue : Qfalse;
}

.shutdownObject

call-seq: shutdown()

Shutdown the logger. Writes are flushed before shutting down.



123
124
125
126
127
# File 'ext/agoo/rlog.c', line 123

static VALUE
rlog_shutdown(VALUE self) {
    log_close();
    return Qnil;
}

.state(label) ⇒ Object

call-seq: state(label)

Returns the current state of the category identified by the specified label.



270
271
272
273
274
275
276
277
278
# File 'ext/agoo/rlog.c', line 270

static VALUE
rlog_on_get(VALUE self, VALUE label) {
    LogCat	cat = log_cat_find(StringValuePtr(label));

    if (NULL == cat) {
	return Qfalse;
    }
    return cat->on ? Qtrue : Qfalse;
}

.warn(msg) ⇒ Object

call-seq: warn(msg)

Log a warn message.



191
192
193
194
195
# File 'ext/agoo/rlog.c', line 191

static VALUE
rlog_warn(VALUE self, VALUE msg) {
    log_cat(&warn_cat, "%s", StringValuePtr(msg));
    return Qnil;
}

.warn?Boolean

call-seq: warn?()

Returns true is warnings are being logged.

Returns:

  • (Boolean)


146
147
148
149
# File 'ext/agoo/rlog.c', line 146

static VALUE
rlog_warnp(VALUE self) {
    return warn_cat.on ? Qtrue : Qfalse;
}