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/log.h" 35 36 #include <stdarg.h> 37 #include <stdio.h> 38 #include <stdlib.h> 39 #include <string.h> 40 #include <strings.h> 41 #include <syslog.h> 42 #include <ctype.h> 43 #include <errno.h> 44 45 struct spdk_trace_flag { 46 const char *name; 47 bool *enabled; 48 }; 49 50 static size_t g_num_trace_flags = 0; 51 static struct spdk_trace_flag *g_trace_flags = NULL; 52 53 unsigned int spdk_g_notice_stderr_flag = 1; 54 unsigned int spdk_g_log_facility = LOG_DAEMON; 55 unsigned int spdk_g_log_priority = LOG_NOTICE; 56 57 SPDK_LOG_REGISTER_TRACE_FLAG("debug", SPDK_TRACE_DEBUG) 58 59 #define MAX_TMPBUF 1024 60 61 int 62 spdk_set_log_facility(const char *facility) 63 { 64 if (strcasecmp(facility, "daemon") == 0) { 65 spdk_g_log_facility = LOG_DAEMON; 66 } else if (strcasecmp(facility, "auth") == 0) { 67 spdk_g_log_facility = LOG_AUTH; 68 } else if (strcasecmp(facility, "authpriv") == 0) { 69 spdk_g_log_facility = LOG_AUTHPRIV; 70 } else if (strcasecmp(facility, "local1") == 0) { 71 spdk_g_log_facility = LOG_LOCAL1; 72 } else if (strcasecmp(facility, "local2") == 0) { 73 spdk_g_log_facility = LOG_LOCAL2; 74 } else if (strcasecmp(facility, "local3") == 0) { 75 spdk_g_log_facility = LOG_LOCAL3; 76 } else if (strcasecmp(facility, "local4") == 0) { 77 spdk_g_log_facility = LOG_LOCAL4; 78 } else if (strcasecmp(facility, "local5") == 0) { 79 spdk_g_log_facility = LOG_LOCAL5; 80 } else if (strcasecmp(facility, "local6") == 0) { 81 spdk_g_log_facility = LOG_LOCAL6; 82 } else if (strcasecmp(facility, "local7") == 0) { 83 spdk_g_log_facility = LOG_LOCAL7; 84 } else { 85 spdk_g_log_facility = LOG_DAEMON; 86 return -1; 87 } 88 return 0; 89 } 90 91 int 92 spdk_set_log_priority(const char *priority) 93 { 94 if (strcasecmp(priority, "emerg") == 0) { 95 spdk_g_log_priority = LOG_EMERG; 96 } else if (strcasecmp(priority, "alert") == 0) { 97 spdk_g_log_priority = LOG_ALERT; 98 } else if (strcasecmp(priority, "crit") == 0) { 99 spdk_g_log_priority = LOG_CRIT; 100 } else if (strcasecmp(priority, "err") == 0) { 101 spdk_g_log_priority = LOG_ERR; 102 } else if (strcasecmp(priority, "warning") == 0) { 103 spdk_g_log_priority = LOG_WARNING; 104 } else if (strcasecmp(priority, "notice") == 0) { 105 spdk_g_log_priority = LOG_NOTICE; 106 } else if (strcasecmp(priority, "info") == 0) { 107 spdk_g_log_priority = LOG_INFO; 108 } else if (strcasecmp(priority, "debug") == 0) { 109 spdk_g_log_priority = LOG_DEBUG; 110 } else { 111 spdk_g_log_priority = LOG_NOTICE; 112 return -1; 113 } 114 return 0; 115 } 116 117 void 118 spdk_noticelog(const char *file, const int line, const char *func, 119 const char *format, ...) 120 { 121 char buf[MAX_TMPBUF]; 122 va_list ap; 123 124 va_start(ap, format); 125 vsnprintf(buf, sizeof buf, format, ap); 126 if (file != NULL) { 127 if (func != NULL) { 128 if (spdk_g_notice_stderr_flag) { 129 fprintf(stderr, "%s:%4d:%s: %s", file, line, func, buf); 130 } 131 syslog(LOG_NOTICE, "%s:%4d:%s: %s", file, line, func, buf); 132 } else { 133 if (spdk_g_notice_stderr_flag) { 134 fprintf(stderr, "%s:%4d: %s", file, line, buf); 135 } 136 syslog(LOG_NOTICE, "%s:%4d: %s", file, line, buf); 137 } 138 } else { 139 if (spdk_g_notice_stderr_flag) { 140 fprintf(stderr, "%s", buf); 141 } 142 syslog(LOG_NOTICE, "%s", buf); 143 } 144 va_end(ap); 145 } 146 147 void 148 spdk_warnlog(const char *file, const int line, const char *func, 149 const char *format, ...) 150 { 151 char buf[MAX_TMPBUF]; 152 va_list ap; 153 154 va_start(ap, format); 155 vsnprintf(buf, sizeof buf, format, ap); 156 if (file != NULL) { 157 if (func != NULL) { 158 fprintf(stderr, "%s:%4d:%s: %s", file, line, func, buf); 159 syslog(LOG_WARNING, "%s:%4d:%s: %s", 160 file, line, func, buf); 161 } else { 162 fprintf(stderr, "%s:%4d: %s", file, line, buf); 163 syslog(LOG_WARNING, "%s:%4d: %s", file, line, buf); 164 } 165 } else { 166 fprintf(stderr, "%s", buf); 167 syslog(LOG_WARNING, "%s", buf); 168 } 169 170 va_end(ap); 171 } 172 173 void 174 spdk_tracelog(const char *file, const int line, const char *func, 175 const char *format, ...) 176 { 177 char buf[MAX_TMPBUF]; 178 va_list ap; 179 180 va_start(ap, format); 181 vsnprintf(buf, sizeof buf, format, ap); 182 if (func != NULL) { 183 fprintf(stderr, "%s:%4d:%s: %s", file, line, func, buf); 184 //syslog(LOG_INFO, "%s:%4d:%s: %s", file, line, func, buf); 185 } else { 186 fprintf(stderr, "%s:%4d: %s", file, line, buf); 187 //syslog(LOG_INFO, "%s:%4d: %s", file, line, buf); 188 } 189 va_end(ap); 190 } 191 192 void 193 spdk_errlog(const char *file, const int line, const char *func, 194 const char *format, ...) 195 { 196 char buf[MAX_TMPBUF]; 197 va_list ap; 198 199 va_start(ap, format); 200 vsnprintf(buf, sizeof buf, format, ap); 201 if (func != NULL) { 202 fprintf(stderr, "%s:%4d:%s: ***ERROR*** %s", file, line, func, buf); 203 syslog(LOG_ERR, "%s:%4d:%s: ***ERROR*** %s", file, line, func, buf); 204 } else { 205 fprintf(stderr, "%s:%4d: ***ERROR*** %s", file, line, buf); 206 syslog(LOG_ERR, "%s:%4d: ***ERROR*** %s", file, line, buf); 207 } 208 va_end(ap); 209 } 210 211 static void 212 fdump(FILE *fp, const char *label, const uint8_t *buf, size_t len) 213 { 214 char tmpbuf[MAX_TMPBUF]; 215 char buf8[8 + 1]; 216 size_t total; 217 size_t idx; 218 219 fprintf(fp, "%s\n", label); 220 221 memset(buf8, 0, sizeof buf8); 222 total = 0; 223 for (idx = 0; idx < len; idx++) { 224 if (idx != 0 && idx % 8 == 0) { 225 snprintf(tmpbuf + total, sizeof tmpbuf - total, 226 "%s", buf8); 227 fprintf(fp, "%s\n", tmpbuf); 228 total = 0; 229 } 230 total += snprintf(tmpbuf + total, sizeof tmpbuf - total, 231 "%2.2x ", buf[idx] & 0xff); 232 buf8[idx % 8] = isprint(buf[idx]) ? buf[idx] : '.'; 233 } 234 for (; idx % 8 != 0; idx++) { 235 total += snprintf(tmpbuf + total, sizeof tmpbuf - total, " "); 236 buf8[idx % 8] = ' '; 237 } 238 snprintf(tmpbuf + total, sizeof tmpbuf - total, "%s", buf8); 239 fprintf(fp, "%s\n", tmpbuf); 240 fflush(fp); 241 } 242 243 void 244 spdk_trace_dump(const char *label, const uint8_t *buf, size_t len) 245 { 246 fdump(stderr, label, buf, len); 247 } 248 249 static int compare_trace_flags(const void *key, const void *p) 250 { 251 const struct spdk_trace_flag *flag = p; 252 253 return strcasecmp(key, flag->name); 254 } 255 256 static struct spdk_trace_flag * 257 get_trace_flag(const char *name) 258 { 259 return bsearch(name, g_trace_flags, g_num_trace_flags, sizeof(struct spdk_trace_flag), 260 compare_trace_flags); 261 } 262 263 void 264 spdk_log_register_trace_flag(const char *name, bool *enabled) 265 { 266 struct spdk_trace_flag *flag, *new_flags; 267 268 if (name == NULL || enabled == NULL) { 269 fprintf(stderr, "missing spdk_trace_flag parameters\n"); 270 abort(); 271 } 272 273 if (get_trace_flag(name)) { 274 fprintf(stderr, "duplicate spdk_trace_flag '%s'\n", name); 275 abort(); 276 } 277 278 new_flags = realloc(g_trace_flags, (g_num_trace_flags + 1) * sizeof(struct spdk_trace_flag)); 279 if (new_flags == NULL) { 280 fprintf(stderr, "spdk_trace_flag allocation error\n"); 281 abort(); 282 } 283 284 g_trace_flags = new_flags; 285 286 /* Find slot so that new flag is inserted in sorted order */ 287 for (flag = g_trace_flags; flag != g_trace_flags + g_num_trace_flags; flag++) { 288 if (strcasecmp(name, flag->name) < 0) { 289 size_t to_move = g_num_trace_flags - (flag - g_trace_flags); 290 291 memmove(flag + 1, flag, to_move * sizeof(struct spdk_trace_flag)); 292 break; 293 } 294 } 295 296 flag->name = name; 297 flag->enabled = enabled; 298 g_num_trace_flags++; 299 } 300 301 bool 302 spdk_log_get_trace_flag(const char *name) 303 { 304 struct spdk_trace_flag *flag = get_trace_flag(name); 305 306 if (flag && *flag->enabled) { 307 return true; 308 } 309 310 return false; 311 } 312 313 static int 314 set_trace_flag(const char *name, bool value) 315 { 316 struct spdk_trace_flag *flag; 317 size_t i; 318 319 if (strcasecmp(name, "all") == 0) { 320 for (i = 0; i < g_num_trace_flags; i++) { 321 *g_trace_flags[i].enabled = value; 322 } 323 return 0; 324 } 325 326 flag = get_trace_flag(name); 327 if (flag == NULL) { 328 return -1; 329 } 330 331 *flag->enabled = value; 332 333 return 0; 334 } 335 336 int 337 spdk_log_set_trace_flag(const char *name) 338 { 339 return set_trace_flag(name, true); 340 } 341 342 int 343 spdk_log_clear_trace_flag(const char *name) 344 { 345 return set_trace_flag(name, false); 346 } 347 348 349 size_t spdk_log_get_num_trace_flags(void) 350 { 351 return g_num_trace_flags; 352 } 353 354 const char *spdk_log_get_trace_flag_name(size_t idx) 355 { 356 if (idx >= g_num_trace_flags) { 357 return NULL; 358 } 359 360 return g_trace_flags[idx].name; 361 } 362 363 void 364 spdk_open_log(void) 365 { 366 if (spdk_g_log_facility != 0) { 367 openlog("spdk", LOG_PID, spdk_g_log_facility); 368 } else { 369 openlog("spdk", LOG_PID, LOG_DAEMON); 370 } 371 } 372 373 void 374 spdk_close_log(void) 375 { 376 closelog(); 377 } 378