]> git.proxmox.com Git - ceph.git/blame - ceph/src/spdk/lib/log/rpc/log_rpc.c
import 15.2.0 Octopus source
[ceph.git] / ceph / src / spdk / lib / log / rpc / log_rpc.c
CommitLineData
7c673cae
FG
1/*-
2 * BSD LICENSE
3 *
4 * Copyright (c) Intel Corporation.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34#include "spdk/rpc.h"
35#include "spdk/util.h"
36
37#include "spdk_internal/log.h"
38
9f95a23c 39struct rpc_log_flag {
7c673cae
FG
40 char *flag;
41};
42
11fdf7f2
TL
43struct rpc_log_level {
44 char *level;
45};
46
7c673cae 47static void
9f95a23c 48free_rpc_log_flag(struct rpc_log_flag *p)
7c673cae
FG
49{
50 free(p->flag);
51}
52
11fdf7f2
TL
53static void
54free_rpc_log_level(struct rpc_log_level *p)
55{
56 free(p->level);
57}
58
9f95a23c
TL
59static const struct spdk_json_object_decoder rpc_log_flag_decoders[] = {
60 {"flag", offsetof(struct rpc_log_flag, flag), spdk_json_decode_string},
7c673cae
FG
61};
62
11fdf7f2
TL
63static const struct spdk_json_object_decoder rpc_log_level_decoders[] = {
64 {"level", offsetof(struct rpc_log_level, level), spdk_json_decode_string},
65};
66
67static int
68_parse_log_level(char *level)
69{
70 if (!strcasecmp(level, "ERROR")) {
71 return SPDK_LOG_ERROR;
72 } else if (!strcasecmp(level, "WARNING")) {
73 return SPDK_LOG_WARN;
74 } else if (!strcasecmp(level, "NOTICE")) {
75 return SPDK_LOG_NOTICE;
76 } else if (!strcasecmp(level, "INFO")) {
77 return SPDK_LOG_INFO;
78 } else if (!strcasecmp(level, "DEBUG")) {
79 return SPDK_LOG_DEBUG;
80 }
81 return -1;
82}
83
84static const char *
85_get_log_level_name(int level)
86{
87 if (level == SPDK_LOG_ERROR) {
88 return "ERROR";
89 } else if (level == SPDK_LOG_WARN) {
90 return "WARNING";
91 } else if (level == SPDK_LOG_NOTICE) {
92 return "NOTICE";
93 } else if (level == SPDK_LOG_INFO) {
94 return "INFO";
95 } else if (level == SPDK_LOG_DEBUG) {
96 return "DEBUG";
97 }
98 return NULL;
99}
100
101static void
102spdk_rpc_set_log_print_level(struct spdk_jsonrpc_request *request,
103 const struct spdk_json_val *params)
104{
105 struct rpc_log_level req = {};
106 int level;
107 struct spdk_json_write_ctx *w;
108
109 if (spdk_json_decode_object(params, rpc_log_level_decoders,
110 SPDK_COUNTOF(rpc_log_level_decoders), &req)) {
111 SPDK_DEBUGLOG(SPDK_LOG_LOG, "spdk_json_decode_object failed\n");
112 goto invalid;
113 }
114
115 level = _parse_log_level(req.level);
116 if (level == -1) {
117 SPDK_DEBUGLOG(SPDK_LOG_LOG, "try to set invalid log level\n");
118 goto invalid;
119 }
120
121 spdk_log_set_print_level(level);
122 free_rpc_log_level(&req);
123
124 w = spdk_jsonrpc_begin_result(request);
125 if (w == NULL) {
126 return;
127 }
128
129 spdk_json_write_bool(w, true);
130 spdk_jsonrpc_end_result(request, w);
131 return;
132
133invalid:
134 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
135 free_rpc_log_level(&req);
136}
137SPDK_RPC_REGISTER("set_log_print_level", spdk_rpc_set_log_print_level,
138 SPDK_RPC_STARTUP | SPDK_RPC_RUNTIME)
139
140static void
141spdk_rpc_get_log_print_level(struct spdk_jsonrpc_request *request,
142 const struct spdk_json_val *params)
143{
144 struct spdk_json_write_ctx *w;
145 int level;
146 const char *name;
147
148 if (params != NULL) {
149 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
9f95a23c
TL
150 "get_log_print_level requires no parameters");
151 return;
152 }
153
154 level = spdk_log_get_print_level();
155 name = _get_log_level_name(level);
156 if (name == NULL) {
157 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
158 "Internal error");
11fdf7f2
TL
159 return;
160 }
161
162 w = spdk_jsonrpc_begin_result(request);
163 if (w == NULL) {
164 return;
165 }
166
11fdf7f2
TL
167 spdk_json_write_string(w, name);
168
11fdf7f2
TL
169 spdk_jsonrpc_end_result(request, w);
170}
171SPDK_RPC_REGISTER("get_log_print_level", spdk_rpc_get_log_print_level,
172 SPDK_RPC_STARTUP | SPDK_RPC_RUNTIME)
173
7c673cae 174static void
11fdf7f2
TL
175spdk_rpc_set_log_level(struct spdk_jsonrpc_request *request,
176 const struct spdk_json_val *params)
177{
178 struct rpc_log_level req = {};
179 int level;
180 struct spdk_json_write_ctx *w;
181
182 if (spdk_json_decode_object(params, rpc_log_level_decoders,
183 SPDK_COUNTOF(rpc_log_level_decoders), &req)) {
184 SPDK_DEBUGLOG(SPDK_LOG_LOG, "spdk_json_decode_object failed\n");
185 goto invalid;
186 }
187
188 level = _parse_log_level(req.level);
189 if (level == -1) {
190 SPDK_DEBUGLOG(SPDK_LOG_LOG, "try to set invalid log level\n");
191 goto invalid;
192 }
193
194
195 spdk_log_set_level(level);
196 free_rpc_log_level(&req);
197
198 w = spdk_jsonrpc_begin_result(request);
199 if (w == NULL) {
200 return;
201 }
202
203 spdk_json_write_bool(w, true);
204 spdk_jsonrpc_end_result(request, w);
205 return;
206
207invalid:
208 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
209 free_rpc_log_level(&req);
210}
211SPDK_RPC_REGISTER("set_log_level", spdk_rpc_set_log_level, SPDK_RPC_STARTUP | SPDK_RPC_RUNTIME)
212
213static void
214spdk_rpc_get_log_level(struct spdk_jsonrpc_request *request,
215 const struct spdk_json_val *params)
216{
217 struct spdk_json_write_ctx *w;
218 int level;
219 const char *name;
220
221 if (params != NULL) {
222 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
9f95a23c
TL
223 "get_log_level requires no parameters");
224 return;
225 }
226
227 level = spdk_log_get_level();
228 name = _get_log_level_name(level);
229 if (name == NULL) {
230 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INTERNAL_ERROR,
231 "Internal error");
11fdf7f2
TL
232 return;
233 }
234
235 w = spdk_jsonrpc_begin_result(request);
236 if (w == NULL) {
237 return;
238 }
239
11fdf7f2
TL
240 spdk_json_write_string(w, name);
241
242 spdk_jsonrpc_end_result(request, w);
243}
244SPDK_RPC_REGISTER("get_log_level", spdk_rpc_get_log_level, SPDK_RPC_STARTUP | SPDK_RPC_RUNTIME)
245
246static void
9f95a23c
TL
247spdk_rpc_set_log_flag(struct spdk_jsonrpc_request *request,
248 const struct spdk_json_val *params)
7c673cae 249{
9f95a23c 250 struct rpc_log_flag req = {};
7c673cae
FG
251 struct spdk_json_write_ctx *w;
252
9f95a23c
TL
253 if (spdk_json_decode_object(params, rpc_log_flag_decoders,
254 SPDK_COUNTOF(rpc_log_flag_decoders), &req)) {
11fdf7f2 255 SPDK_DEBUGLOG(SPDK_LOG_LOG, "spdk_json_decode_object failed\n");
7c673cae
FG
256 goto invalid;
257 }
258
259 if (req.flag == 0) {
11fdf7f2 260 SPDK_DEBUGLOG(SPDK_LOG_LOG, "flag was 0\n");
7c673cae
FG
261 goto invalid;
262 }
263
9f95a23c
TL
264 spdk_log_set_flag(req.flag);
265 free_rpc_log_flag(&req);
7c673cae 266
11fdf7f2
TL
267 w = spdk_jsonrpc_begin_result(request);
268 if (w == NULL) {
7c673cae
FG
269 return;
270 }
271
7c673cae 272 spdk_json_write_bool(w, true);
11fdf7f2 273 spdk_jsonrpc_end_result(request, w);
7c673cae
FG
274 return;
275
276invalid:
11fdf7f2 277 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
9f95a23c 278 free_rpc_log_flag(&req);
7c673cae 279}
9f95a23c
TL
280SPDK_RPC_REGISTER("set_log_flag", spdk_rpc_set_log_flag, SPDK_RPC_STARTUP | SPDK_RPC_RUNTIME)
281SPDK_RPC_REGISTER_ALIAS_DEPRECATED(set_log_flag, set_trace_flag)
7c673cae
FG
282
283static void
9f95a23c
TL
284spdk_rpc_clear_log_flag(struct spdk_jsonrpc_request *request,
285 const struct spdk_json_val *params)
7c673cae 286{
9f95a23c 287 struct rpc_log_flag req = {};
7c673cae
FG
288 struct spdk_json_write_ctx *w;
289
9f95a23c
TL
290 if (spdk_json_decode_object(params, rpc_log_flag_decoders,
291 SPDK_COUNTOF(rpc_log_flag_decoders), &req)) {
11fdf7f2 292 SPDK_DEBUGLOG(SPDK_LOG_LOG, "spdk_json_decode_object failed\n");
7c673cae
FG
293 goto invalid;
294 }
295
296 if (req.flag == 0) {
11fdf7f2 297 SPDK_DEBUGLOG(SPDK_LOG_LOG, "flag was 0\n");
7c673cae
FG
298 goto invalid;
299 }
300
9f95a23c
TL
301 spdk_log_clear_flag(req.flag);
302 free_rpc_log_flag(&req);
7c673cae 303
11fdf7f2
TL
304 w = spdk_jsonrpc_begin_result(request);
305 if (w == NULL) {
7c673cae
FG
306 return;
307 }
308
7c673cae 309 spdk_json_write_bool(w, true);
11fdf7f2 310 spdk_jsonrpc_end_result(request, w);
7c673cae
FG
311 return;
312
313invalid:
11fdf7f2 314 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
9f95a23c 315 free_rpc_log_flag(&req);
7c673cae 316}
9f95a23c 317SPDK_RPC_REGISTER("clear_log_flag", spdk_rpc_clear_log_flag,
11fdf7f2 318 SPDK_RPC_STARTUP | SPDK_RPC_RUNTIME)
9f95a23c 319SPDK_RPC_REGISTER_ALIAS_DEPRECATED(clear_log_flag, clear_trace_flag)
7c673cae
FG
320
321static void
9f95a23c
TL
322spdk_rpc_get_log_flags(struct spdk_jsonrpc_request *request,
323 const struct spdk_json_val *params)
7c673cae
FG
324{
325 struct spdk_json_write_ctx *w;
9f95a23c 326 struct spdk_log_flag *flag;
7c673cae
FG
327
328 if (params != NULL) {
11fdf7f2 329 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS,
9f95a23c 330 "get_log_flags requires no parameters");
7c673cae
FG
331 return;
332 }
333
11fdf7f2
TL
334 w = spdk_jsonrpc_begin_result(request);
335 if (w == NULL) {
7c673cae
FG
336 return;
337 }
338
7c673cae 339 spdk_json_write_object_begin(w);
9f95a23c 340 flag = spdk_log_get_first_flag();
7c673cae
FG
341 while (flag) {
342 spdk_json_write_name(w, flag->name);
343 spdk_json_write_bool(w, flag->enabled);
9f95a23c 344 flag = spdk_log_get_next_flag(flag);
7c673cae
FG
345 }
346 spdk_json_write_object_end(w);
11fdf7f2 347 spdk_jsonrpc_end_result(request, w);
7c673cae 348}
9f95a23c
TL
349SPDK_RPC_REGISTER("get_log_flags", spdk_rpc_get_log_flags, SPDK_RPC_STARTUP | SPDK_RPC_RUNTIME)
350SPDK_RPC_REGISTER_ALIAS_DEPRECATED(get_log_flags, get_trace_flags)