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