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 static logfunc *g_log = NULL; 49 50 void 51 spdk_log_open(logfunc *logf) 52 { 53 if (logf) { 54 g_log = logf; 55 } else { 56 openlog("spdk", LOG_PID, LOG_LOCAL7); 57 } 58 } 59 60 void 61 spdk_log_close(void) 62 { 63 if (!g_log) { 64 closelog(); 65 } 66 } 67 68 static void 69 get_timestamp_prefix(char *buf, int buf_size) 70 { 71 struct tm *info; 72 char date[24]; 73 struct timespec ts; 74 long usec; 75 76 clock_gettime(CLOCK_REALTIME, &ts); 77 info = localtime(&ts.tv_sec); 78 usec = ts.tv_nsec / 1000; 79 80 strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", info); 81 snprintf(buf, buf_size, "[%s.%06ld] ", date, usec); 82 } 83 84 void 85 spdk_log(enum spdk_log_level level, const char *file, const int line, const char *func, 86 const char *format, ...) 87 { 88 va_list ap; 89 90 va_start(ap, format); 91 spdk_vlog(level, file, line, func, format, ap); 92 va_end(ap); 93 } 94 95 void 96 spdk_vlog(enum spdk_log_level level, const char *file, const int line, const char *func, 97 const char *format, va_list ap) 98 { 99 int severity = LOG_INFO; 100 char buf[MAX_TMPBUF]; 101 char timestamp[64]; 102 103 if (g_log) { 104 g_log(level, file, line, func, format, ap); 105 return; 106 } 107 108 if (level > g_spdk_log_print_level && level > g_spdk_log_level) { 109 return; 110 } 111 112 switch (level) { 113 case SPDK_LOG_ERROR: 114 severity = LOG_ERR; 115 break; 116 case SPDK_LOG_WARN: 117 severity = LOG_WARNING; 118 break; 119 case SPDK_LOG_NOTICE: 120 severity = LOG_NOTICE; 121 break; 122 case SPDK_LOG_INFO: 123 case SPDK_LOG_DEBUG: 124 severity = LOG_INFO; 125 break; 126 case SPDK_LOG_DISABLED: 127 return; 128 } 129 130 vsnprintf(buf, sizeof(buf), format, ap); 131 132 if (level <= g_spdk_log_print_level) { 133 get_timestamp_prefix(timestamp, sizeof(timestamp)); 134 if (file) { 135 fprintf(stderr, "%s%s:%4d:%s: *%s*: %s", timestamp, file, line, func, spdk_level_names[level], buf); 136 } else { 137 fprintf(stderr, "%s%s", timestamp, buf); 138 } 139 } 140 141 if (level <= g_spdk_log_level) { 142 if (file) { 143 syslog(severity, "%s:%4d:%s: *%s*: %s", file, line, func, spdk_level_names[level], buf); 144 } else { 145 syslog(severity, "%s", buf); 146 } 147 } 148 } 149 150 static void 151 fdump(FILE *fp, const char *label, const uint8_t *buf, size_t len) 152 { 153 char tmpbuf[MAX_TMPBUF]; 154 char buf16[16 + 1]; 155 size_t total; 156 unsigned int idx; 157 158 fprintf(fp, "%s\n", label); 159 160 memset(buf16, 0, sizeof buf16); 161 total = 0; 162 for (idx = 0; idx < len; idx++) { 163 if (idx != 0 && idx % 16 == 0) { 164 snprintf(tmpbuf + total, sizeof tmpbuf - total, 165 " %s", buf16); 166 memset(buf16, 0, sizeof buf16); 167 fprintf(fp, "%s\n", tmpbuf); 168 total = 0; 169 } 170 if (idx % 16 == 0) { 171 total += snprintf(tmpbuf + total, sizeof tmpbuf - total, 172 "%08x ", idx); 173 } 174 if (idx % 8 == 0) { 175 total += snprintf(tmpbuf + total, sizeof tmpbuf - total, 176 "%s", " "); 177 } 178 total += snprintf(tmpbuf + total, sizeof tmpbuf - total, 179 "%2.2x ", buf[idx] & 0xff); 180 buf16[idx % 16] = isprint(buf[idx]) ? buf[idx] : '.'; 181 } 182 for (; idx % 16 != 0; idx++) { 183 if (idx == 8) { 184 total += snprintf(tmpbuf + total, sizeof tmpbuf - total, 185 " "); 186 } 187 188 total += snprintf(tmpbuf + total, sizeof tmpbuf - total, " "); 189 } 190 snprintf(tmpbuf + total, sizeof tmpbuf - total, " %s", buf16); 191 fprintf(fp, "%s\n", tmpbuf); 192 fflush(fp); 193 } 194 195 void 196 spdk_log_dump(FILE *fp, const char *label, const void *buf, size_t len) 197 { 198 fdump(fp, label, buf, len); 199 } 200