]> git.proxmox.com Git - ceph.git/blame - ceph/src/spdk/lib/log/log.c
update sources to ceph Nautilus 14.2.1
[ceph.git] / ceph / src / spdk / lib / log / log.c
CommitLineData
7c673cae
FG
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
11fdf7f2 34#include "spdk/stdinc.h"
7c673cae 35
11fdf7f2 36#include "spdk_internal/log.h"
7c673cae 37
11fdf7f2
TL
38#ifdef SPDK_LOG_BACKTRACE_LVL
39#define UNW_LOCAL_ONLY
40#include <libunwind.h>
7c673cae 41#endif
7c673cae 42
11fdf7f2
TL
43static const char *const spdk_level_names[] = {
44 [SPDK_LOG_ERROR] = "ERROR",
45 [SPDK_LOG_WARN] = "WARNING",
46 [SPDK_LOG_NOTICE] = "NOTICE",
47 [SPDK_LOG_INFO] = "INFO",
48 [SPDK_LOG_DEBUG] = "DEBUG",
7c673cae
FG
49};
50
11fdf7f2 51#define MAX_TMPBUF 1024
7c673cae 52
11fdf7f2
TL
53void
54spdk_log_open(void)
7c673cae 55{
11fdf7f2 56 openlog("spdk", LOG_PID, LOG_LOCAL7);
7c673cae
FG
57}
58
59void
11fdf7f2 60spdk_log_close(void)
7c673cae 61{
11fdf7f2
TL
62 closelog();
63}
7c673cae 64
11fdf7f2
TL
65#ifdef SPDK_LOG_BACKTRACE_LVL
66static void
67spdk_log_unwind_stack(FILE *fp, enum spdk_log_level level)
68{
69 unw_error_t err;
70 unw_cursor_t cursor;
71 unw_context_t uc;
72 unw_word_t ip;
73 unw_word_t offp;
74 char f_name[64];
75 int frame;
76
77 if (level > g_spdk_log_backtrace_level) {
78 return;
79 }
80
81 unw_getcontext(&uc);
82 unw_init_local(&cursor, &uc);
83 fprintf(fp, "*%s*: === BACKTRACE START ===\n", spdk_level_names[level]);
84
85 unw_step(&cursor);
86 for (frame = 1; unw_step(&cursor) > 0; frame++) {
87 unw_get_reg(&cursor, UNW_REG_IP, &ip);
88 err = unw_get_proc_name(&cursor, f_name, sizeof(f_name), &offp);
89 if (err || strcmp(f_name, "main") == 0) {
90 break;
7c673cae 91 }
11fdf7f2
TL
92
93 fprintf(fp, "*%s*: %3d: %*s%s() at %#lx\n", spdk_level_names[level], frame, frame - 1, "", f_name,
94 (unsigned long)ip);
7c673cae 95 }
11fdf7f2 96 fprintf(fp, "*%s*: === BACKTRACE END ===\n", spdk_level_names[level]);
7c673cae
FG
97}
98
11fdf7f2
TL
99#else
100#define spdk_log_unwind_stack(fp, lvl)
101#endif
102
7c673cae 103void
11fdf7f2
TL
104spdk_log(enum spdk_log_level level, const char *file, const int line, const char *func,
105 const char *format, ...)
7c673cae 106{
11fdf7f2 107 int severity = LOG_INFO;
7c673cae
FG
108 char buf[MAX_TMPBUF];
109 va_list ap;
110
11fdf7f2
TL
111 switch (level) {
112 case SPDK_LOG_ERROR:
113 severity = LOG_ERR;
114 break;
115 case SPDK_LOG_WARN:
116 severity = LOG_WARNING;
117 break;
118 case SPDK_LOG_NOTICE:
119 severity = LOG_NOTICE;
120 break;
121 case SPDK_LOG_INFO:
122 case SPDK_LOG_DEBUG:
123 severity = LOG_INFO;
124 break;
125 case SPDK_LOG_DISABLED:
126 return;
7c673cae
FG
127 }
128
11fdf7f2 129 va_start(ap, format);
7c673cae 130
11fdf7f2 131 vsnprintf(buf, sizeof(buf), format, ap);
7c673cae 132
11fdf7f2
TL
133 if (level <= g_spdk_log_print_level) {
134 fprintf(stderr, "%s:%4d:%s: *%s*: %s", file, line, func, spdk_level_names[level], buf);
135 spdk_log_unwind_stack(stderr, level);
7c673cae 136 }
7c673cae 137
11fdf7f2
TL
138 if (level <= g_spdk_log_level) {
139 syslog(severity, "%s:%4d:%s: *%s*: %s", file, line, func, spdk_level_names[level], buf);
7c673cae 140 }
11fdf7f2 141
7c673cae
FG
142 va_end(ap);
143}
144
145static void
146fdump(FILE *fp, const char *label, const uint8_t *buf, size_t len)
147{
148 char tmpbuf[MAX_TMPBUF];
149 char buf16[16 + 1];
150 size_t total;
151 unsigned int idx;
152
153 fprintf(fp, "%s\n", label);
154
155 memset(buf16, 0, sizeof buf16);
156 total = 0;
157 for (idx = 0; idx < len; idx++) {
158 if (idx != 0 && idx % 16 == 0) {
159 snprintf(tmpbuf + total, sizeof tmpbuf - total,
160 " %s", buf16);
161 fprintf(fp, "%s\n", tmpbuf);
162 total = 0;
163 }
164 if (idx % 16 == 0) {
165 total += snprintf(tmpbuf + total, sizeof tmpbuf - total,
166 "%08x ", idx);
167 }
168 if (idx % 8 == 0) {
169 total += snprintf(tmpbuf + total, sizeof tmpbuf - total,
170 "%s", " ");
171 }
172 total += snprintf(tmpbuf + total, sizeof tmpbuf - total,
173 "%2.2x ", buf[idx] & 0xff);
174 buf16[idx % 16] = isprint(buf[idx]) ? buf[idx] : '.';
175 }
176 for (; idx % 16 != 0; idx++) {
177 total += snprintf(tmpbuf + total, sizeof tmpbuf - total, " ");
178 buf16[idx % 16] = ' ';
179 }
180 snprintf(tmpbuf + total, sizeof tmpbuf - total, " %s", buf16);
181 fprintf(fp, "%s\n", tmpbuf);
182 fflush(fp);
183}
184
185void
11fdf7f2 186spdk_trace_dump(FILE *fp, const char *label, const void *buf, size_t len)
7c673cae 187{
11fdf7f2 188 fdump(fp, label, buf, len);
7c673cae 189}