2 * Copyright (C) 2015 - 2017 Red Hat, Inc.
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 * Author: Gris Ge <fge@redhat.com>
18 * Todd Gill <tgill@redhat.com>
30 #include "libdmmp/libdmmp.h"
31 #include "libdmmp_private.h"
33 #define _DMMP_LOG_STRERR_ALIGN_WIDTH 80
34 /* ^ Only used in _dmmp_log_stderr() for pretty log output.
35 * When provided log message is less than 80 bytes, fill it with space, then
36 * print code file name, function name, line after the 80th bytes.
39 static const struct _num_str_conv _DMMP_RC_MSG_CONV[] = {
41 {DMMP_ERR_NO_MEMORY, "Out of memory"},
42 {DMMP_ERR_BUG, "BUG of libdmmp library"},
43 {DMMP_ERR_IPC_TIMEOUT, "Timeout when communicate with multipathd, "
44 "try to increase it via "
45 "dmmp_context_timeout_set()"},
46 {DMMP_ERR_IPC_ERROR, "Error when communicate with multipathd daemon"},
47 {DMMP_ERR_NO_DAEMON, "The multipathd daemon not started"},
48 {DMMP_ERR_INCOMPATIBLE, "Incompatible multipathd daemon version"},
49 {DMMP_ERR_MPATH_BUSY, "Specified multipath device map is in use"},
50 {DMMP_ERR_MPATH_NOT_FOUND, "Specified multipath not found"},
51 {DMMP_ERR_INVALID_ARGUMENT, "Invalid argument"},
54 _dmmp_str_func_gen(dmmp_strerror, int, rc, _DMMP_RC_MSG_CONV);
56 static const struct _num_str_conv _DMMP_PRI_CONV[] = {
57 {DMMP_LOG_PRIORITY_DEBUG, "DEBUG"},
58 {DMMP_LOG_PRIORITY_INFO, "INFO"},
59 {DMMP_LOG_PRIORITY_WARNING, "WARNING"},
60 {DMMP_LOG_PRIORITY_ERROR, "ERROR"},
62 _dmmp_str_func_gen(dmmp_log_priority_str, int, priority, _DMMP_PRI_CONV);
64 void _dmmp_log_stderr(struct dmmp_context *ctx, int priority,
65 const char *file, int line, const char *func_name,
66 const char *format, va_list args)
68 int printed_bytes = 0;
69 void *userdata = NULL;
71 printed_bytes += fprintf(stderr, "libdmmp %s: ",
72 dmmp_log_priority_str(priority));
73 printed_bytes += vfprintf(stderr, format, args);
75 userdata = dmmp_context_userdata_get(ctx);
77 fprintf(stderr, "(userdata address: %p)",
79 /* ^ Just demonstrate how userdata could be used and
80 * bypass clang static analyzer about unused ctx argument warning
83 if (printed_bytes < _DMMP_LOG_STRERR_ALIGN_WIDTH) {
84 fprintf(stderr, "%*s # %s:%s():%d\n",
85 _DMMP_LOG_STRERR_ALIGN_WIDTH - printed_bytes, "", file,
88 fprintf(stderr, " # %s:%s():%d\n", file, func_name, line);