1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright (C) 2017 Intel Corporation. 3 * All rights reserved. 4 */ 5 6 #include "spdk/stdinc.h" 7 8 #include "spdk/log.h" 9 10 static TAILQ_HEAD(, spdk_log_flag) g_log_flags = TAILQ_HEAD_INITIALIZER(g_log_flags); 11 12 static struct spdk_log_flag * 13 get_log_flag(const char *name) 14 { 15 struct spdk_log_flag *flag; 16 17 TAILQ_FOREACH(flag, &g_log_flags, tailq) { 18 if (strcasecmp(name, flag->name) == 0) { 19 return flag; 20 } 21 } 22 23 return NULL; 24 } 25 26 void 27 spdk_log_register_flag(const char *name, struct spdk_log_flag *flag) 28 { 29 struct spdk_log_flag *iter; 30 31 if (name == NULL || flag == NULL) { 32 SPDK_ERRLOG("missing spdk_log_flag parameters\n"); 33 assert(false); 34 return; 35 } 36 37 if (get_log_flag(name)) { 38 SPDK_ERRLOG("duplicate spdk_log_flag '%s'\n", name); 39 assert(false); 40 return; 41 } 42 43 TAILQ_FOREACH(iter, &g_log_flags, tailq) { 44 if (strcasecmp(iter->name, flag->name) > 0) { 45 TAILQ_INSERT_BEFORE(iter, flag, tailq); 46 return; 47 } 48 } 49 50 TAILQ_INSERT_TAIL(&g_log_flags, flag, tailq); 51 } 52 53 bool 54 spdk_log_get_flag(const char *name) 55 { 56 struct spdk_log_flag *flag = get_log_flag(name); 57 58 if (flag && flag->enabled) { 59 return true; 60 } 61 62 return false; 63 } 64 65 static int 66 log_set_flag(const char *name, bool value) 67 { 68 struct spdk_log_flag *flag; 69 70 if (strcasecmp(name, "all") == 0) { 71 TAILQ_FOREACH(flag, &g_log_flags, tailq) { 72 flag->enabled = value; 73 } 74 return 0; 75 } 76 77 flag = get_log_flag(name); 78 if (flag == NULL) { 79 return -1; 80 } 81 82 flag->enabled = value; 83 84 return 0; 85 } 86 87 int 88 spdk_log_set_flag(const char *name) 89 { 90 return log_set_flag(name, true); 91 } 92 93 int 94 spdk_log_clear_flag(const char *name) 95 { 96 return log_set_flag(name, false); 97 } 98 99 struct spdk_log_flag * 100 spdk_log_get_first_flag(void) 101 { 102 return TAILQ_FIRST(&g_log_flags); 103 } 104 105 struct spdk_log_flag * 106 spdk_log_get_next_flag(struct spdk_log_flag *flag) 107 { 108 return TAILQ_NEXT(flag, tailq); 109 } 110 111 void 112 spdk_log_usage(FILE *f, const char *log_arg) 113 { 114 struct spdk_log_flag *flag; 115 fprintf(f, " %s, --logflag <flag> enable log flag (all", log_arg); 116 117 TAILQ_FOREACH(flag, &g_log_flags, tailq) { 118 fprintf(f, ", %s", flag->name); 119 } 120 121 fprintf(f, ")\n"); 122 } 123