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 const char *const spdk_level_names[] = { 39 [SPDK_LOG_ERROR] = "ERROR", 40 [SPDK_LOG_WARN] = "WARNING", 41 [SPDK_LOG_NOTICE] = "NOTICE", 42 [SPDK_LOG_INFO] = "INFO", 43 [SPDK_LOG_DEBUG] = "DEBUG", 44 }; 45 46 #define MAX_TMPBUF 1024 47 48 void 49 spdk_log_open(void) 50 { 51 openlog("spdk", LOG_PID, LOG_LOCAL7); 52 } 53 54 void 55 spdk_log_close(void) 56 { 57 closelog(); 58 } 59 60 void 61 spdk_log(enum spdk_log_level level, const char *file, const int line, const char *func, 62 const char *format, ...) 63 { 64 int severity = LOG_INFO; 65 char buf[MAX_TMPBUF]; 66 va_list ap; 67 68 switch (level) { 69 case SPDK_LOG_ERROR: 70 severity = LOG_ERR; 71 break; 72 case SPDK_LOG_WARN: 73 severity = LOG_WARNING; 74 break; 75 case SPDK_LOG_NOTICE: 76 severity = LOG_NOTICE; 77 break; 78 case SPDK_LOG_INFO: 79 case SPDK_LOG_DEBUG: 80 severity = LOG_INFO; 81 break; 82 } 83 84 va_start(ap, format); 85 86 vsnprintf(buf, sizeof(buf), format, ap); 87 88 if (level <= g_spdk_log_print_level) { 89 fprintf(stderr, "%s:%4d:%s: *%s*: %s", file, line, func, spdk_level_names[level], buf); 90 } 91 92 if (level <= g_spdk_log_level) { 93 syslog(severity, "%s:%4d:%s: *%s*: %s", file, line, func, spdk_level_names[level], buf); 94 } 95 96 va_end(ap); 97 } 98 99 static void 100 fdump(FILE *fp, const char *label, const uint8_t *buf, size_t len) 101 { 102 char tmpbuf[MAX_TMPBUF]; 103 char buf16[16 + 1]; 104 size_t total; 105 unsigned int idx; 106 107 fprintf(fp, "%s\n", label); 108 109 memset(buf16, 0, sizeof buf16); 110 total = 0; 111 for (idx = 0; idx < len; idx++) { 112 if (idx != 0 && idx % 16 == 0) { 113 snprintf(tmpbuf + total, sizeof tmpbuf - total, 114 " %s", buf16); 115 fprintf(fp, "%s\n", tmpbuf); 116 total = 0; 117 } 118 if (idx % 16 == 0) { 119 total += snprintf(tmpbuf + total, sizeof tmpbuf - total, 120 "%08x ", idx); 121 } 122 if (idx % 8 == 0) { 123 total += snprintf(tmpbuf + total, sizeof tmpbuf - total, 124 "%s", " "); 125 } 126 total += snprintf(tmpbuf + total, sizeof tmpbuf - total, 127 "%2.2x ", buf[idx] & 0xff); 128 buf16[idx % 16] = isprint(buf[idx]) ? buf[idx] : '.'; 129 } 130 for (; idx % 16 != 0; idx++) { 131 total += snprintf(tmpbuf + total, sizeof tmpbuf - total, " "); 132 buf16[idx % 16] = ' '; 133 } 134 snprintf(tmpbuf + total, sizeof tmpbuf - total, " %s", buf16); 135 fprintf(fp, "%s\n", tmpbuf); 136 fflush(fp); 137 } 138 139 void 140 spdk_trace_dump(FILE *fp, const char *label, const void *buf, size_t len) 141 { 142 fdump(fp, label, buf, len); 143 } 144