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/stdinc.h" 35 36 #include "spdk_internal/log.h" 37 38 static TAILQ_HEAD(, spdk_log_flag) g_log_flags = TAILQ_HEAD_INITIALIZER(g_log_flags); 39 40 enum spdk_log_level g_spdk_log_level = SPDK_LOG_NOTICE; 41 enum spdk_log_level g_spdk_log_print_level = SPDK_LOG_NOTICE; 42 43 SPDK_LOG_REGISTER_COMPONENT("log", SPDK_LOG_LOG) 44 45 #define MAX_TMPBUF 1024 46 47 void 48 spdk_log_set_level(enum spdk_log_level level) 49 { 50 assert(level >= SPDK_LOG_DISABLED); 51 assert(level <= SPDK_LOG_DEBUG); 52 g_spdk_log_level = level; 53 } 54 55 enum spdk_log_level 56 spdk_log_get_level(void) { 57 return g_spdk_log_level; 58 } 59 60 void 61 spdk_log_set_print_level(enum spdk_log_level level) 62 { 63 assert(level >= SPDK_LOG_DISABLED); 64 assert(level <= SPDK_LOG_DEBUG); 65 g_spdk_log_print_level = level; 66 } 67 68 enum spdk_log_level 69 spdk_log_get_print_level(void) { 70 return g_spdk_log_print_level; 71 } 72 73 static struct spdk_log_flag * 74 get_log_flag(const char *name) 75 { 76 struct spdk_log_flag *flag; 77 78 TAILQ_FOREACH(flag, &g_log_flags, tailq) { 79 if (strcasecmp(name, flag->name) == 0) { 80 return flag; 81 } 82 } 83 84 return NULL; 85 } 86 87 void 88 spdk_log_register_flag(const char *name, struct spdk_log_flag *flag) 89 { 90 struct spdk_log_flag *iter; 91 92 if (name == NULL || flag == NULL) { 93 SPDK_ERRLOG("missing spdk_log_flag parameters\n"); 94 assert(false); 95 return; 96 } 97 98 if (get_log_flag(name)) { 99 SPDK_ERRLOG("duplicate spdk_log_flag '%s'\n", name); 100 assert(false); 101 return; 102 } 103 104 TAILQ_FOREACH(iter, &g_log_flags, tailq) { 105 if (strcasecmp(iter->name, flag->name) > 0) { 106 TAILQ_INSERT_BEFORE(iter, flag, tailq); 107 return; 108 } 109 } 110 111 TAILQ_INSERT_TAIL(&g_log_flags, flag, tailq); 112 } 113 114 bool 115 spdk_log_get_flag(const char *name) 116 { 117 struct spdk_log_flag *flag = get_log_flag(name); 118 119 if (flag && flag->enabled) { 120 return true; 121 } 122 123 return false; 124 } 125 126 static int 127 log_set_flag(const char *name, bool value) 128 { 129 struct spdk_log_flag *flag; 130 131 if (strcasecmp(name, "all") == 0) { 132 TAILQ_FOREACH(flag, &g_log_flags, tailq) { 133 flag->enabled = value; 134 } 135 return 0; 136 } 137 138 flag = get_log_flag(name); 139 if (flag == NULL) { 140 return -1; 141 } 142 143 flag->enabled = value; 144 145 return 0; 146 } 147 148 int 149 spdk_log_set_flag(const char *name) 150 { 151 return log_set_flag(name, true); 152 } 153 154 int 155 spdk_log_clear_flag(const char *name) 156 { 157 return log_set_flag(name, false); 158 } 159 160 struct spdk_log_flag * 161 spdk_log_get_first_flag(void) 162 { 163 return TAILQ_FIRST(&g_log_flags); 164 } 165 166 struct spdk_log_flag * 167 spdk_log_get_next_flag(struct spdk_log_flag *flag) 168 { 169 return TAILQ_NEXT(flag, tailq); 170 } 171 172 void 173 spdk_log_usage(FILE *f, const char *log_arg) 174 { 175 #ifdef DEBUG 176 struct spdk_log_flag *flag; 177 fprintf(f, " %s, --logflag <flag> enable debug log flag (all", log_arg); 178 179 TAILQ_FOREACH(flag, &g_log_flags, tailq) { 180 fprintf(f, ", %s", flag->name); 181 } 182 183 fprintf(f, ")\n"); 184 #else 185 fprintf(f, " %s, --logflag <flag> enable debug log flag (not supported" 186 " - must reconfigure with --enable-debug)\n", log_arg); 187 #endif 188 } 189