xref: /spdk/lib/log/log.c (revision 0ed85362c8132a2d1927757fbcade66b6660d26a)
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 	if (info == NULL) {
80 		snprintf(buf, buf_size, "[%s.%06ld] ", "unknown date", usec);
81 		return;
82 	}
83 
84 	strftime(date, sizeof(date), "%Y-%m-%d %H:%M:%S", info);
85 	snprintf(buf, buf_size, "[%s.%06ld] ", date, usec);
86 }
87 
88 void
89 spdk_log(enum spdk_log_level level, const char *file, const int line, const char *func,
90 	 const char *format, ...)
91 {
92 	va_list ap;
93 
94 	va_start(ap, format);
95 	spdk_vlog(level, file, line, func, format, ap);
96 	va_end(ap);
97 }
98 
99 void
100 spdk_vlog(enum spdk_log_level level, const char *file, const int line, const char *func,
101 	  const char *format, va_list ap)
102 {
103 	int severity = LOG_INFO;
104 	char buf[MAX_TMPBUF];
105 	char timestamp[64];
106 
107 	if (g_log) {
108 		g_log(level, file, line, func, format, ap);
109 		return;
110 	}
111 
112 	if (level > g_spdk_log_print_level && level > g_spdk_log_level) {
113 		return;
114 	}
115 
116 	switch (level) {
117 	case SPDK_LOG_ERROR:
118 		severity = LOG_ERR;
119 		break;
120 	case SPDK_LOG_WARN:
121 		severity = LOG_WARNING;
122 		break;
123 	case SPDK_LOG_NOTICE:
124 		severity = LOG_NOTICE;
125 		break;
126 	case SPDK_LOG_INFO:
127 	case SPDK_LOG_DEBUG:
128 		severity = LOG_INFO;
129 		break;
130 	case SPDK_LOG_DISABLED:
131 		return;
132 	}
133 
134 	vsnprintf(buf, sizeof(buf), format, ap);
135 
136 	if (level <= g_spdk_log_print_level) {
137 		get_timestamp_prefix(timestamp, sizeof(timestamp));
138 		if (file) {
139 			fprintf(stderr, "%s%s:%4d:%s: *%s*: %s", timestamp, file, line, func, spdk_level_names[level], buf);
140 		} else {
141 			fprintf(stderr, "%s%s", timestamp, buf);
142 		}
143 	}
144 
145 	if (level <= g_spdk_log_level) {
146 		if (file) {
147 			syslog(severity, "%s:%4d:%s: *%s*: %s", file, line, func, spdk_level_names[level], buf);
148 		} else {
149 			syslog(severity, "%s", buf);
150 		}
151 	}
152 }
153 
154 static void
155 fdump(FILE *fp, const char *label, const uint8_t *buf, size_t len)
156 {
157 	char tmpbuf[MAX_TMPBUF];
158 	char buf16[16 + 1];
159 	size_t total;
160 	unsigned int idx;
161 
162 	fprintf(fp, "%s\n", label);
163 
164 	memset(buf16, 0, sizeof buf16);
165 	total = 0;
166 	for (idx = 0; idx < len; idx++) {
167 		if (idx != 0 && idx % 16 == 0) {
168 			snprintf(tmpbuf + total, sizeof tmpbuf - total,
169 				 " %s", buf16);
170 			memset(buf16, 0, sizeof buf16);
171 			fprintf(fp, "%s\n", tmpbuf);
172 			total = 0;
173 		}
174 		if (idx % 16 == 0) {
175 			total += snprintf(tmpbuf + total, sizeof tmpbuf - total,
176 					  "%08x ", idx);
177 		}
178 		if (idx % 8 == 0) {
179 			total += snprintf(tmpbuf + total, sizeof tmpbuf - total,
180 					  "%s", " ");
181 		}
182 		total += snprintf(tmpbuf + total, sizeof tmpbuf - total,
183 				  "%2.2x ", buf[idx] & 0xff);
184 		buf16[idx % 16] = isprint(buf[idx]) ? buf[idx] : '.';
185 	}
186 	for (; idx % 16 != 0; idx++) {
187 		if (idx == 8) {
188 			total += snprintf(tmpbuf + total, sizeof tmpbuf - total,
189 					  " ");
190 		}
191 
192 		total += snprintf(tmpbuf + total, sizeof tmpbuf - total, "   ");
193 	}
194 	snprintf(tmpbuf + total, sizeof tmpbuf - total, "  %s", buf16);
195 	fprintf(fp, "%s\n", tmpbuf);
196 	fflush(fp);
197 }
198 
199 void
200 spdk_log_dump(FILE *fp, const char *label, const void *buf, size_t len)
201 {
202 	fdump(fp, label, buf, len);
203 }
204