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