| 1 | /* |
| 2 | Copyright(C) 2009-2018 Brazil |
| 3 | Copyright(C) 2021 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.h" |
| 21 | #include "../grn_output.h" |
| 22 | #include "../grn_proc.h" |
| 23 | |
| 24 | #include <groonga/plugin.h> |
| 25 | |
| 26 | static grn_obj * |
| 27 | command_log_level(grn_ctx *ctx, |
| 28 | int n_args, |
| 29 | grn_obj **args, |
| 30 | grn_user_data *user_data) |
| 31 | { |
| 32 | grn_obj *level_name = grn_plugin_proc_get_var(ctx, |
| 33 | user_data, |
| 34 | "level", -1); |
| 35 | if (GRN_TEXT_LEN(level_name) > 0) { |
| 36 | grn_log_level max_level; |
| 37 | GRN_TEXT_PUTC(ctx, level_name, '\0'); |
| 38 | if (grn_log_level_parse(GRN_TEXT_VALUE(level_name), &max_level)) { |
| 39 | grn_logger_set_max_level(ctx, max_level); |
| 40 | } else { |
| 41 | ERR(GRN_INVALID_ARGUMENT, |
| 42 | "invalid log level: <%s>", GRN_TEXT_VALUE(level_name)); |
| 43 | } |
| 44 | } else { |
| 45 | ERR(GRN_INVALID_ARGUMENT, "log level is missing"); |
| 46 | } |
| 47 | GRN_OUTPUT_BOOL(ctx->rc == GRN_SUCCESS); |
| 48 | return NULL; |
| 49 | } |
| 50 | |
| 51 | void |
| 52 | grn_proc_init_log_level(grn_ctx *ctx) |
| 53 | { |
| 54 | grn_expr_var vars[1]; |
| 55 | grn_plugin_expr_var_init(ctx, &(vars[0]), "level", -1); |
| 56 | grn_plugin_command_create(ctx, |
| 57 | "log_level", -1, |
| 58 | command_log_level, |
| 59 | 1, |
| 60 | vars); |
| 61 | } |
| 62 | |
| 63 | static grn_obj * |
| 64 | command_log_put(grn_ctx *ctx, |
| 65 | int n_args, |
| 66 | grn_obj **args, |
| 67 | grn_user_data *user_data) |
| 68 | { |
| 69 | grn_obj *level_name = grn_plugin_proc_get_var(ctx, |
| 70 | user_data, |
| 71 | "level", -1); |
| 72 | grn_obj *message = grn_plugin_proc_get_var(ctx, |
| 73 | user_data, |
| 74 | "message", -1); |
| 75 | if (GRN_TEXT_LEN(level_name) > 0) { |
| 76 | grn_log_level level; |
| 77 | GRN_TEXT_PUTC(ctx, level_name, '\0'); |
| 78 | if (grn_log_level_parse(GRN_TEXT_VALUE(level_name), &level)) { |
| 79 | GRN_LOG(ctx, level, "%.*s", |
| 80 | (int)GRN_TEXT_LEN(message), |
| 81 | GRN_TEXT_VALUE(message)); |
| 82 | } else { |
| 83 | ERR(GRN_INVALID_ARGUMENT, |
| 84 | "invalid log level: <%s>", GRN_TEXT_VALUE(level_name)); |
| 85 | } |
| 86 | } else { |
| 87 | ERR(GRN_INVALID_ARGUMENT, "log level is missing"); |
| 88 | } |
| 89 | GRN_OUTPUT_BOOL(ctx->rc == GRN_SUCCESS); |
| 90 | return NULL; |
| 91 | } |
| 92 | |
| 93 | void |
| 94 | grn_proc_init_log_put(grn_ctx *ctx) |
| 95 | { |
| 96 | grn_expr_var vars[2]; |
| 97 | grn_plugin_expr_var_init(ctx, &(vars[0]), "level", -1); |
| 98 | grn_plugin_expr_var_init(ctx, &(vars[1]), "message", -1); |
| 99 | grn_plugin_command_create(ctx, |
| 100 | "log_put", -1, |
| 101 | command_log_put, |
| 102 | 2, |
| 103 | vars); |
| 104 | } |
| 105 | |
| 106 | static grn_obj * |
| 107 | command_log_reopen(grn_ctx *ctx, |
| 108 | int n_args, |
| 109 | grn_obj **args, |
| 110 | grn_user_data *user_data) |
| 111 | { |
| 112 | grn_log_reopen(ctx); |
| 113 | GRN_OUTPUT_BOOL(ctx->rc == GRN_SUCCESS); |
| 114 | return NULL; |
| 115 | } |
| 116 | |
| 117 | void |
| 118 | grn_proc_init_log_reopen(grn_ctx *ctx) |
| 119 | { |
| 120 | grn_plugin_command_create(ctx, |
| 121 | "log_reopen", -1, |
| 122 | command_log_reopen, |
| 123 | 0, |
| 124 | NULL); |
| 125 | } |