1/* -*- c-basic-offset: 2 -*- */
2/*
3 Copyright(C) 2014-2017 Brazil
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License version 2.1 as published by the Free Software Foundation.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17*/
18
19#include "../grn_ctx_impl.h"
20
21#ifdef GRN_WITH_MRUBY
22#include <mruby.h>
23#include <mruby/class.h>
24#include <mruby/data.h>
25#include <mruby/variable.h>
26#include <mruby/string.h>
27
28#include "../grn_mrb.h"
29#include "mrb_logger.h"
30
31static mrb_value
32logger_s_get_default_path(mrb_state *mrb, mrb_value self)
33{
34 return mrb_str_new_cstr(mrb, grn_default_logger_get_path());
35}
36
37static mrb_value
38logger_s_get_default_level(mrb_state *mrb, mrb_value self)
39{
40 mrb_value mrb_level_class;
41 mrb_value mrb_level;
42
43 mrb_level_class = mrb_const_get(mrb, self, mrb_intern_lit(mrb, "Level"));
44 mrb_level = mrb_fixnum_value(grn_default_logger_get_max_level());
45 return mrb_funcall(mrb, mrb_level_class, "find", 1, mrb_level);
46}
47
48static mrb_value
49logger_need_log_p(mrb_state *mrb, mrb_value self)
50{
51 grn_ctx *ctx = (grn_ctx *)mrb->ud;
52 mrb_int level;
53
54 mrb_get_args(mrb, "i", &level);
55
56 return mrb_bool_value(grn_logger_pass(ctx, level));
57}
58
59static mrb_value
60logger_log(mrb_state *mrb, mrb_value self)
61{
62 grn_ctx *ctx = (grn_ctx *)mrb->ud;
63 mrb_int level;
64 char *file;
65 mrb_int line;
66 char *method;
67 char *message;
68 mrb_int message_size;
69
70 mrb_get_args(mrb, "izizs",
71 &level, &file, &line, &method, &message, &message_size);
72 grn_logger_put(ctx, level, file, line, method,
73 "%.*s", (int)message_size, message);
74
75 return self;
76}
77
78void
79grn_mrb_logger_init(grn_ctx *ctx)
80{
81 grn_mrb_data *data = &(ctx->impl->mrb);
82 mrb_state *mrb = data->state;
83 struct RClass *module = data->module;
84 struct RClass *klass;
85
86 klass = mrb_define_class_under(mrb, module, "Logger", mrb->object_class);
87
88 mrb_define_singleton_method(mrb, (struct RObject *)klass, "default_path",
89 logger_s_get_default_path, MRB_ARGS_NONE());
90 mrb_define_singleton_method(mrb, (struct RObject *)klass, "default_level",
91 logger_s_get_default_level, MRB_ARGS_NONE());
92
93 mrb_define_method(mrb, klass, "need_log?", logger_need_log_p, MRB_ARGS_REQ(1));
94 mrb_define_method(mrb, klass, "log", logger_log, MRB_ARGS_REQ(5));
95
96 grn_mrb_load(ctx, "logger/level.rb");
97 grn_mrb_load(ctx, "logger.rb");
98}
99#endif
100