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