Imported Upstream version 0.7.4
[platform/upstream/multipath-tools.git] / libdmmp / libdmmp_misc.c
1 /*
2  * Copyright (C) 2015 - 2017 Red Hat, Inc.
3  *
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.
8  *
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.
13  *
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/>.
16  *
17  * Author: Gris Ge <fge@redhat.com>
18  *         Todd Gill <tgill@redhat.com>
19  */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <stdarg.h>
25 #include <errno.h>
26 #include <limits.h>
27 #include <assert.h>
28 #include <json.h>
29
30 #include "libdmmp/libdmmp.h"
31 #include "libdmmp_private.h"
32
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.
37  */
38
39 static const struct _num_str_conv _DMMP_RC_MSG_CONV[] = {
40         {DMMP_OK, "OK"},
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"},
52 };
53
54 _dmmp_str_func_gen(dmmp_strerror, int, rc, _DMMP_RC_MSG_CONV);
55
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"},
61 };
62 _dmmp_str_func_gen(dmmp_log_priority_str, int, priority, _DMMP_PRI_CONV);
63
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)
67 {
68         int printed_bytes = 0;
69         void *userdata = NULL;
70
71         printed_bytes += fprintf(stderr, "libdmmp %s: ",
72                                  dmmp_log_priority_str(priority));
73         printed_bytes += vfprintf(stderr, format, args);
74
75         userdata = dmmp_context_userdata_get(ctx);
76         if (userdata != NULL)
77                 fprintf(stderr, "(userdata address: %p)",
78                         userdata);
79         /* ^ Just demonstrate how userdata could be used and
80          *   bypass clang static analyzer about unused ctx argument warning
81          */
82
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,
86                         func_name, line);
87         } else {
88                 fprintf(stderr, " # %s:%s():%d\n", file, func_name, line);
89         }
90 }