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