| 1 | /* |
| 2 | Copyright(C) 2015-2018 Brazil |
| 3 | Copyright(C) 2022 Sutou Kouhei <[email protected]> |
| 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 as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) any later version. |
| 9 | |
| 10 | This library is distributed in the hope that it will be useful, |
| 11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | Lesser General Public License for more details. |
| 14 | |
| 15 | You should have received a copy of the GNU Lesser General Public |
| 16 | License along with this library; if not, write to the Free Software |
| 17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 18 | */ |
| 19 | |
| 20 | #include "../grn_ctx_impl.h" |
| 21 | |
| 22 | #ifdef GRN_WITH_MRUBY |
| 23 | #include <mruby.h> |
| 24 | #include <mruby/class.h> |
| 25 | #include <mruby/data.h> |
| 26 | #include <mruby/variable.h> |
| 27 | #include <mruby/string.h> |
| 28 | |
| 29 | #include "../grn_mrb.h" |
| 30 | #include "mrb_query_logger.h" |
| 31 | |
| 32 | #include "../grn_encoding.h" |
| 33 | |
| 34 | static mrb_value |
| 35 | query_logger_need_log_p(mrb_state *mrb, mrb_value self) |
| 36 | { |
| 37 | grn_ctx *ctx = (grn_ctx *)mrb->ud; |
| 38 | mrb_int flag; |
| 39 | |
| 40 | mrb_get_args(mrb, "i", &flag); |
| 41 | |
| 42 | return mrb_bool_value(grn_query_logger_pass(ctx, (unsigned int)flag)); |
| 43 | } |
| 44 | |
| 45 | static mrb_value |
| 46 | query_logger_log_raw(mrb_state *mrb, mrb_value self) |
| 47 | { |
| 48 | grn_ctx *ctx = (grn_ctx *)mrb->ud; |
| 49 | mrb_int flag; |
| 50 | char *mark; |
| 51 | char *utf8_message; |
| 52 | mrb_int utf8_message_size; |
| 53 | const char *message; |
| 54 | size_t message_size; |
| 55 | |
| 56 | mrb_get_args(mrb, "izs", &flag, &mark, &utf8_message, &utf8_message_size); |
| 57 | message = grn_encoding_convert_from_utf8(ctx, |
| 58 | utf8_message, |
| 59 | (ssize_t)utf8_message_size, |
| 60 | &message_size); |
| 61 | grn_query_logger_put(ctx, (unsigned int)flag, mark, |
| 62 | "%.*s", |
| 63 | (int)message_size, |
| 64 | message); |
| 65 | grn_encoding_converted_free(ctx, message); |
| 66 | |
| 67 | return self; |
| 68 | } |
| 69 | |
| 70 | void |
| 71 | grn_mrb_query_logger_init(grn_ctx *ctx) |
| 72 | { |
| 73 | grn_mrb_data *data = &(ctx->impl->mrb); |
| 74 | mrb_state *mrb = data->state; |
| 75 | struct RClass *module = data->module; |
| 76 | struct RClass *klass; |
| 77 | |
| 78 | klass = mrb_define_class_under(mrb, module, "QueryLogger", mrb->object_class); |
| 79 | |
| 80 | mrb_define_method(mrb, klass, "need_log?", query_logger_need_log_p, |
| 81 | MRB_ARGS_REQ(1)); |
| 82 | mrb_define_method(mrb, klass, "log_raw", query_logger_log_raw, |
| 83 | MRB_ARGS_REQ(3)); |
| 84 | |
| 85 | grn_mrb_load(ctx, "query_logger/flag.rb"); |
| 86 | grn_mrb_load(ctx, "query_logger.rb"); |
| 87 | } |
| 88 | #endif |