| 1 | /* -*- c-basic-offset: 2 -*- */ |
| 2 | /* |
| 3 | Copyright(C) 2015 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_query_logger.h" |
| 30 | |
| 31 | static mrb_value |
| 32 | query_logger_need_log_p(mrb_state *mrb, mrb_value self) |
| 33 | { |
| 34 | grn_ctx *ctx = (grn_ctx *)mrb->ud; |
| 35 | mrb_int flag; |
| 36 | |
| 37 | mrb_get_args(mrb, "i" , &flag); |
| 38 | |
| 39 | return mrb_bool_value(grn_query_logger_pass(ctx, flag)); |
| 40 | } |
| 41 | |
| 42 | static mrb_value |
| 43 | query_logger_log_raw(mrb_state *mrb, mrb_value self) |
| 44 | { |
| 45 | grn_ctx *ctx = (grn_ctx *)mrb->ud; |
| 46 | mrb_int flag; |
| 47 | char *mark; |
| 48 | char *message; |
| 49 | mrb_int message_size; |
| 50 | |
| 51 | mrb_get_args(mrb, "izs" , &flag, &mark, &message, &message_size); |
| 52 | grn_query_logger_put(ctx, flag, mark, |
| 53 | "%.*s" , (int)message_size, message); |
| 54 | |
| 55 | return self; |
| 56 | } |
| 57 | |
| 58 | void |
| 59 | grn_mrb_query_logger_init(grn_ctx *ctx) |
| 60 | { |
| 61 | grn_mrb_data *data = &(ctx->impl->mrb); |
| 62 | mrb_state *mrb = data->state; |
| 63 | struct RClass *module = data->module; |
| 64 | struct RClass *klass; |
| 65 | |
| 66 | klass = mrb_define_class_under(mrb, module, "QueryLogger" , mrb->object_class); |
| 67 | |
| 68 | mrb_define_method(mrb, klass, "need_log?" , query_logger_need_log_p, |
| 69 | MRB_ARGS_REQ(1)); |
| 70 | mrb_define_method(mrb, klass, "log_raw" , query_logger_log_raw, |
| 71 | MRB_ARGS_REQ(3)); |
| 72 | |
| 73 | grn_mrb_load(ctx, "query_logger/flag.rb" ); |
| 74 | grn_mrb_load(ctx, "query_logger.rb" ); |
| 75 | } |
| 76 | #endif |
| 77 | |